一个工作组(Group)里可以有多个用户(User),一个User只属于一个Group,这是典型的多对一的关系。在多对一的关系中正确的数据库设计是在多的这方(在这里是User这方)加一个Group的外键。如果数据库设计的与之相反就会产生冗余,请看下面这个例子:
友情提示:这是错误的设计方法:
GroupId |
GroupName |
UserId |
1 |
Group_1 |
1 |
1 |
Group_1 |
2 |
UserId |
UserName |
1 |
moluo |
2 |
xingzhe |
这样在一的这方(也就是Group这方)设置外键关联,就会产生冗余(一个Group有N名User,这样设计就得重复N次冗余的GroupId和GroupName),因此在多对一时,数据库的设计是必须在多的这方加一的那方的外键!
先写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:
<?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>
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="Group" table="_Group"> <id name="id"> <generator class="native"/> </id> <property name="name"></property> </class> </hibernate-mapping>
尊重版权,转载请注明本文链接
欢迎关注行者摩罗微信公众号(xingzhemoluo),共同交流编程经验,扫描下方二维码即可;
时间: 2024-10-13 01:50:52