package com.bjsxt.hibernate; public class Group { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.bjsxt.hibernate; public class User { private int id; private String name; private Group group; public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } [color=red]User(many)---Group(one)[/color]
Group.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.Group" table="t_group"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping>
User.hbm.xml <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.bjsxt.hibernate.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <many-to-one name="group" column="groupId" /> </class> </hibernate-mapping>
14:27:21,165 INFO org.hibernate.tool.hbm2ddl.SchemaExport:154 - Running hbm2ddl schema export 14:27:21,168 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:170 - import file not found: /import.sql 14:27:21,168 INFO org.hibernate.tool.hbm2ddl.SchemaExport:179 - exporting generated schema to database 14:27:21,323 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - alter table t_user drop foreign key FKCB63CCB6C3D18669 14:27:21,332 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:288 - Unsuccessful: alter table t_user drop foreign key FKCB63CCB6C3D18669 14:27:21,333 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:289 - Table 'hibernate.t_user' doesn't exist 14:27:21,333 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - drop table if exists t_group 14:27:21,337 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - drop table if exists t_user 14:27:21,340 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - create table t_group ( id integer not null auto_increment, name varchar(255), primary key (id) ) 14:27:21,479 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - create table t_user ( id integer not null auto_increment, name varchar(255), groupId integer, primary key (id) ) 14:27:21,589 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:303 - alter table t_user add index FKCB63CCB6C3D18669 (groupId), add constraint FKCB63CCB6C3D18669 foreign key (groupId) references t_group (id) 14:27:21,884 INFO org.hibernate.tool.hbm2ddl.SchemaExport:196 - schema export complete
inverse知识点
http://lijiejava.iteye.com/blog/776587