Hibernate One-to-Many using Annotations

本文由尤慕译自这里,转载请保留此信息。

Hibernate Annotation配置 one-to-many。

考虑以下 Student 和 Phone 实体。

relationship between student and phone

一个Student可能有多个Phone,要模拟这种关系,我们需要三张表 STUDENTPHONE 和 STUDENT_PHONE . 如下:



下面是Student和Phone的实体类:

Student.java

package org.aaron.hibernate.entity.one2many;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "STUDENT")
public class Student {
	private long studentId;
	private String studentName;
	private Set<Phone> phones = new HashSet<Phone>();

	public Student() {
	}

	public Student(String studentName, Set<Phone> phones) {
		this.studentName = studentName;
		this.phones = phones;
	}

	@Id
	@GeneratedValue
	@Column(name = "STUDENT_ID")
	public long getStudentId() {
		return studentId;
	}

	public void setStudentId(long studentId) {
		this.studentId = studentId;
	}

	@Column(name = "STUDNET_NAME", nullable = false, length = 100)
	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	@OneToMany(cascade = CascadeType.ALL)
	@JoinTable(name = "STUDENT_PHONE", joinColumns = { @JoinColumn(name = "STUDENT_ID") }, inverseJoinColumns = { @JoinColumn(name = "PHONE_ID") })
	public Set<Phone> getPhones() {
		return phones;
	}

	public void setPhones(Set<Phone> phones) {
		this.phones = phones;
	}

}

Phone.java

package org.aaron.hibernate.entity.one2many;

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

@Entity
@Table(name = "PHONE")
public class Phone {
	private long phoneId;
	private String phoneType;
	private String phoneNumber;

	public Phone() {
	}

	public Phone(String phoneType, String phoneNumber) {
		this.phoneType = phoneType;
		this.phoneNumber = phoneNumber;
	}

	@Id
	@GeneratedValue
	@Column(name = "PHONE_ID")
	public long getPhoneId() {
		return phoneId;
	}

	public void setPhoneId(long phoneId) {
		this.phoneId = phoneId;
	}

	@Column(name = "PHONE_TYPE", nullable = false, length = 10)
	public String getPhoneType() {
		return phoneType;
	}

	public void setPhoneType(String phoneType) {
		this.phoneType = phoneType;
	}

	@Column(name = "PHONE_NUMBER", nullable = false, length = 15)
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

}


在Hibernate配置文件进行配置:

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hibernate.connection.driver_class">
			com.microsoft.sqlserver.jdbc.SQLServerDriver
	</property>
		<property name="hibernate.connection.password">test</property>
		<property name="hibernate.connection.url">
			jdbc:sqlserver://192.168.1.168:1433;
			DatabaseName=hibernate
	</property>
		<property name="hibernate.connection.username">test</property>
		<property name="hibernate.dialect">
			org.hibernate.dialect.SQLServerDialect
	</property>
		<property name="show_sql">true</property>
		<property name="hbm2ddl.auto">create</property>
		<mapping class="org.aaron.hibernate.entity.one2many.Phone" />
		<mapping class="org.aaron.hibernate.entity.one2many.Student" />
	</session-factory>
</hibernate-configuration>


测试类:

Main.java

package org.aaron.hibernate.entity.one2many;

import java.util.HashSet;
import java.util.Set;

import org.aaron.hibernate.util.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Main {

	public static void main(String[] args) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			Set<Phone> phoneNumbers = new HashSet<Phone>();
			phoneNumbers.add(new Phone("house","32354353"));
			phoneNumbers.add(new Phone("mobile","9889343423"));
			
			Student student = new Student("Eswar", phoneNumbers);
			session.save(student);
			
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

}

Okay。

阅读更多
个人分类: Hibernate
上一篇清空 HTML File Input
下一篇MyEclipse 优化配置
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭