1. 程式人生 > >hibernate之多對一單向關聯

hibernate之多對一單向關聯

als exce 方法 試用 size code 產生 配置文件 sse

一個工作組(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;
	[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:

<?

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),共同交流編程經驗,掃描下方二維碼就可以;

技術分享



hibernate之多對一單向關聯