hibernate之多對一單向關聯

        一個工作組(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),共同交流編程經驗,掃描下方二維碼即可;





發佈了41 篇原創文章 · 獲贊 14 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章