一个工作组(Group)里能够有多个用户(User),一个User仅仅属于一个Group,这是典型的多对一的关系。
在多对一的关系中正确的数据库设计是在多的这方(在这里是User这方)加一个Group的外键。假设数据库设计的与之相反就会产生冗余。请看以下这个样例:
友情提示:这是错误的设计方法:
GroupId |
GroupName |
UserId |
1 |
Group_1 |
1 |
1 |
Group_1 |
2 |
UserId |
UserName |
1 |
moluo |
2 |
xingzhe |
先写Annotation版本号的多对一单向关联:
先建Group类:
package com.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="_Group") public class Group { private int id; private String name; @Id @GeneratedValue 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; } }再建User类:
package com.hibernate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="_User") public class User { private int id; private String name; private Group group; @ManyToOne public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } @Id @GeneratedValue 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; } }在多的这方加group的外键,并在get方法上注明:@manyToOne
配置文件:
<mapping class="com.hibernate.model.User"/> <mapping class="com.hibernate.model.Group"/>測试用例:
package com.hibernate.model; import static org.junit.Assert.*; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class ORMappingTest { public static SessionFactory sf = null; //@BeforeClass public static void beforeClass(){ try{ sf = new AnnotationConfiguration().configure().buildSessionFactory(); } catch(Exception e) { e.printStackTrace(); } finally{ } } @Test public void testSchemaExport(){ new SchemaExport(new AnnotationConfiguration().configure()).create(false, true); } //@AfterClass public static void afterClass(){ sf.close(); } }先写xml版本号的多对一单向关联:
Group和User类以及測试用例同上。不再赘述
我把映射文件贴出来:
User.hbm.xml:
<?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 package="com.hibernate.model"> <class name="User" table="_User"> <id name="id"> <generator class="native"/> </id> <property name="name"></property> <many-to-one name="group" column="groupId"></many-to-one> </class> </hibernate-mapping>
<?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 package="com.hibernate.model"> <class name="Group" table="_Group"> <id name="id"> <generator class="native"/> </id> <property name="name"></property> </class> </hibernate-mapping>
尊重版权,转载请注明本文链接
欢迎关注行者摩罗微信公众号(xingzhemoluo),共同交流编程经验,扫描下方二维码就可以;