hibernate之多對一單向關聯
一個工作組(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; } }
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; } }
配置文件:
<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; [email protected] 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); } [email protected] 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),共同交流編程經驗,掃描下方二維碼就可以;
hibernate之多對一單向關聯