`
kylinsoong
  • 浏览: 240701 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate 备忘录-续

阅读更多

     本文记录Hibernate关联映射中的:组件映射、多对一单向、一对多单向、多对多单向、一对多或多对一双向、多对多双向。所以本文分为6个部分,每部分包括.1 简单说明、.2 Annotation配置及代码明细、.3 结果分析及相关测试、.4 XML配置下的XML配置。

1 组件映射

1.1 如下图所示



 上图可以表示Laptop是Person的一个组件,这种情况下,我们可以这样设计,代码如下

1.2 Annotation配置及代码

package com.kylin.test;

import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="t_person")
public class Person {
	private int id;
	private String name;
	private Laptop laptop;
	@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;
	}
	@Embedded
	public Laptop getLaptop() {
		return laptop;
	}
	public void setLaptop(Laptop laptop) {
		this.laptop = laptop;
	}
}

 

package com.kylin.test;

public class Laptop {
	private String brand;
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
}

 

如上就为全部配置代码,下面为结果及测试

1.3 结果及测试

先给出测试代码。以后2.3等久不给出,因为大同小异,测试代码:

package com.kylin.test;

import org.hibernate.Session;
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 ComponentTest {
private static SessionFactory sessionFactory;
	@BeforeClass
	public static void beforeClass() {
		sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
	}
	@AfterClass
	public static void afterClass() {
		sessionFactory.close();
	}
	@Test
	public void testExportTable() {
		new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
	}
	@Test
	public void testSaveAndLoad() {
		Person person = new Person();
		person.setName("Kylin Soong");
		Laptop l = new Laptop();
		l.setBrand("DELL");
		person.setLaptop(l);
		Session s = null;
		s = sessionFactory.getCurrentSession();
		s.beginTransaction();
		s.save(person);
		s.getTransaction().commit();
		
		s = sessionFactory.getCurrentSession();
		s.beginTransaction();
		Person p = (Person) s.load(Person.class, 1);
		assert("DELL".equals(p.getLaptop().getBrand()));
		s.getTransaction().commit();
	}
}

 在给出数据库中所建表结构,如下图:



 1.4给出老版本HibernateXML配置

<hibernate-mapping>
	<class name="com.kylin.test.Person" dynamic-update="true">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<component name="laptop" class="com.kylin.test.Laptop">
			<property name="brand"/>
		</component>
    </class>
</hibernate-mapping>

 2 多对一单向

2.1此处模拟多个User对一个Group

2.2相关Annotation配置及代码

@Entity
@Table(name="t_user")
public class User {
	private int id;
	private String name;
	private 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;
	}
	@ManyToOne
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
}

 

@Entity
@Table(name="t_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;
	}
}

 2.3给出数据库中产生表结构,如下图:



 2.4老版本XML配置

<hibernate-mapping>
	<class name="com.kylin.test.Group" dynamic-update="true" table="t_group">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.User" dynamic-update="true" table="t_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<many-to-one name="group" column="groupId" />
    </class>
</hibernate-mapping>

 3 一对多单向

3.1  同样还是刚才User和Group,现在一个Group对应多个User

3.2 相关Annotation配置及代码

@Entity
@Table(name="t_group")
public class Group {
	private int id;
	private String name;
	private Set<User> users;
	public Group(String name) {
		this.name = 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;
	}
	@OneToMany
	@JoinColumn(name="groupId")
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
}

 

@Entity
@Table(name="t_user")
public class User {
	private int id;
	private String name;
	public User(String name) {
		this.name = 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;
	}
	
}

 3.3数据库中生成表结构



 3.4XML配置

<hibernate-mapping>
	<class name="com.kylin.test.Group" dynamic-update="true" table="t_group">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<set name="users">
			<key column="groupId"></key>
			<one-to-many class="com.kylin.test.User"></one-to-many>
		</set>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.User" dynamic-update="true" table="t_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
    </class>
</hibernate-mapping>

  

4. 多对多单向关联映射

4.1 描述:如下图所示:



 学生和课程是典型的多对多的关系,这部分就描述一个学生和课程的多对多单向映射实例

4.2 直接贴出代码

@Entity
@Table(name="tt_course")
public class Course {
	private int id;
	private String name;
	private Set<Student> students = new HashSet<Student>();
	@Id
	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;
	}
	@ManyToMany
	@JoinTable(name="tt_cs")
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
}

 

@Entity
@Table(name="tt_student")
public class Student {
	private int id;
	private String name;
	@Id
	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;
	}
}

4.3给出数据库中生成的表结构,如下图:



 4.4同样给出老版本HibernateXML配置

<hibernate-mapping>
	<class name="com.kylin.test.Course">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<set name="students" table="t_s">
			<key column="course_id"></key>
			<many-to-many class="com.kylin.test.Student" column="student_id"/>
		</set>
    </class>
	
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.Student">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
    </class>
	
</hibernate-mapping>

 

5 一对多或多对一双向

5.1简单描述,以User和Group为例说明一对多或多对一双向映射

5.2,贴出代码

@Entity
@Table(name="t_group")
public class Group {
	private int id;
	private String name;
	private Set<User> users = new HashSet<User>();
	@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;
	}
	@OneToMany(mappedBy="group",
			cascade={CascadeType.ALL},
			fetch=FetchType.LAZY
			)
	public Set<User> getUsers() {
		return users;
	}
	public void setUsers(Set<User> users) {
		this.users = users;
	}
}

 

@Entity
@Table(name="t_user")
public class User {
	private int id;
	private String name;
	private 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;
	}
	@ManyToOne(
				cascade={CascadeType.REMOVE},
				fetch=FetchType.LAZY)
	public Group getGroup() {
		return group;
	}
	public void setGroup(Group group) {
		this.group = group;
	}
}

 5.3给出数据库中生成表结构



 5.4,老版本XML配置

<hibernate-mapping>
	<class name="com.kylin.test.Group" dynamic-update="true" table="t_group">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<set name="users">
			<key column="groupId"></key>
			<one-to-many class="com.kylin.test.User"></one-to-many>
		</set>
    </class>
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.User" dynamic-update="true" table="t_user">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"/>
		<many-to-one name="group" column="groupId"></many-to-one>
    </class>
</hibernate-mapping>

 

6 多对多双向映射

6.1 同多对多单向一样,以Student,Course来说明多对多双向映射

6.2,贴出代码

@Entity
@Table(name="tt_course")
public class Course {
	private int id;
	private String name;
	private Set<Student> students = new HashSet<Student>();
	@Id
	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;
	}
	@ManyToMany
	@JoinTable(name="tt_cs")
	public Set<Student> getStudents() {
		return students;
	}
	public void setStudents(Set<Student> students) {
		this.students = students;
	}
}

 

@Entity
@Table(name="tt_student")
public class Student {
	private int id;
	private String name;
	private Set<Course> courses = new HashSet<Course>();
	@Id
	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;
	}
	@ManyToMany(mappedBy="students")
	public Set<Course> getCourses() {
		return courses;
	}
	public void setCourses(Set<Course> courses) {
		this.courses = courses;
	}
}

 6.3给出数据库中表结构



 6.4老版本XML配置

<hibernate-mapping>
	<class name="com.kylin.test.Course">
		<id name="id">
			<generator class="native"></generator>
		</id>
		
		<property name="name"></property>
		<set name="students" table="t_s">
			<key column="course_id"></key>
			<many-to-many class="com.kylin.test.Student" column="student_id"/>
		</set>
    </class>
	
</hibernate-mapping>

 

<hibernate-mapping>
	<class name="com.kylin.test.Student">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<set name="courses" table="tt_cs">
			<key column="student_id"></key>
			<many-to-many class="com.kylin.test.Course" column="course_id"/>
		</set>
		<property name="name"></property>
    </class>
	
</hibernate-mapping>

 

本章结束 

  • 大小: 2.8 KB
  • 大小: 4.6 KB
  • 大小: 10 KB
  • 大小: 9.7 KB
  • 大小: 2.9 KB
  • 大小: 11.3 KB
  • 大小: 9.1 KB
  • 大小: 11.4 KB
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics