package com.oneToMany;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
@Entity
@Table(name="Student")
public class Student {
private long id;
private String studentName;
private Set<Phone> studentPhoneNumbers=new HashSet<Phone>(0);
public Student(){
//Hibernate调用
}
public Student(String studentName, Set<Phone> studentPhoneNumbers){
this.studentName=studentName;
this.studentPhoneNumbers=studentPhoneNumbers;
}
public void setId(long id) {
this.id = id;
}
@Id//主键
@GeneratedValue
@Column(name="Student_id")
public long getId() {
return id;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
/**
* @Column列字段名
*/
@Column(name="Student_name",length=100,nullable=false)
public String getStudentName() {
return studentName;
}
public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) {
this.studentPhoneNumbers = studentPhoneNumbers;
}
@OneToMany//在Student实体类和Phone实体类之间创建一对多的关系(一个学生有多个号码)
@Cascade({CascadeType.ALL})
/**
* @JoinTable 创建关联的表Student_phone
* @JoinColumn 创建在关联的表中关联的字段(列)
* joinColumns “一端”和“多端”关联的字段
* inverseJoinColumns “多端”和“一端”关联的字段
*/
@JoinTable(name="Student_phone",joinColumns={@JoinColumn(name="Student_id")},
inverseJoinColumns={@JoinColumn(name="Phone_id")})
public Set<Phone> getStudentPhoneNumbers() {
return studentPhoneNumbers;
}
}
package com.oneToMany;
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 phoneNumber;
private String phoneType;
public Phone() {
//Hibernate调用
}
public Phone(String phoneNumber,String phoneType) {
this.phoneNumber=phoneNumber;
this.phoneType=phoneType;
}
public void setPhoneId(long phoneId) {
this.phoneId = phoneId;
}
@Id
@GeneratedValue
@Column(name="Phone_id")
public long getPhoneId() {
return phoneId;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
@Column(name="number",nullable=false,length=15)
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneType(String phoneType) {
this.phoneType = phoneType;
}
@Column(name="type",nullable=false,length=10)
public String getPhoneType() {
return phoneType;
}
}
package com.oneToMany;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class TestDemo {
public static void main(String[] args) {
StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure("hibernate.cfg.xml").build();
SessionFactory factory = new MetadataSources(registry).buildMetadata()
.buildSessionFactory();
// 获取Session
Session session = factory.openSession();
Transaction transaction = session.beginTransaction();
try {
Set<Phone> phoneNumbers = new HashSet<Phone>();
phoneNumbers.add(new Phone("123345","house"));
phoneNumbers.add(new Phone("456722454","mobile"));
Student student = new Student("Jeff", phoneNumbers);
session.save(student);
transaction.commit();
} catch (Exception e) {
transaction.rollback();// 回滚事务
e.printStackTrace();
} finally {
if (session != null){
session.close();
}
}
}
}
效果图