我有一个Student表,其中一个自动生成的id作为主键,一对多映射到Phone表。
My Phone表有一个复合键PhonePK,其中包含电话号码和Student表的外键ID作为主键。
如果我使用phonePk.setStudent它可以工作,但我真的不想这样做。我怎样才能做到这一点?如果它无法实现,我想安排设置学生ID但不是整个学生对象。我该怎么做呢?如果有人能够对此有所了解,我们将非常感激。
student.java
import java.io.Serializable;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
@Entity
@SuppressWarnings("serial")
public class Student implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String fName;
private String lName;
private String mName;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "id")
private Set<Phone> phones;
/**
* @return the fName
*/
public String getfName() {
return fName;
}
/**
* @return the id
*/
public int getId() {
return id;
}
/**
* @return the lName
*/
public String getlName() {
return lName;
}
/**
* @return the mName
*/
public String getmName() {
return mName;
}
/**
* @return the phones
*/
public Set<Phone> getPhones() {
return phones;
}
/**
* @param fName
* the fName to set
*/
public void setfName(final String fName) {
this.fName = fName;
}
/**
* @param id
* the id to set
*/
public void setId(final int id) {
this.id = id;
}
/**
* @param lName
* the lName to set
*/
public void setlName(final String lName) {
this.lName = lName;
}
/**
* @param mName
* the mName to set
*/
public void setmName(final String mName) {
this.mName = mName;
}
/**
* @param phones
* the phones to set
*/
public void setPhones(final Set<Phone> phones) {
this.phones = phones;
}
}
Phone.java
import java.io.Serializable;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
@Entity
@SuppressWarnings("serial")
public class Phone implements Serializable {
@EmbeddedId
private PhonePK PK;
private String color;
/**
* @return the color
*/
public String getColor() {
return color;
}
public PhonePK getPK() {
return PK;
}
/**
* @param color
* the color to set
*/
public void setColor(final String color) {
this.color = color;
}
public void setPK(final PhonePK pK) {
PK = pK;
}
}
PhonePK.java
import java.io.Serializable;
import javax.persistence.Embeddable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Embeddable
@SuppressWarnings({ "serial" })
public class PhonePK implements Serializable {
@ManyToOne
@JoinColumn(name = "id", insertable = false, updatable = false)
private Student student;
private String phoneNumber;
public String getPhoneNumber() {
return phoneNumber;
}
public Student getStudent() {
return student;
}
public void setPhoneNumber(final String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public void setStudent(final Student student) {
this.student = student;
}
}
Main.java
import java.util.LinkedHashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(final String args[]) {
Configuration configuration = new Configuration();
Transaction transaction = null;
configuration.addAnnotatedClass(Student.class);
configuration.addAnnotatedClass(Phone.class);
configuration.configure("hibernate.cfg.xml");
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Student student = new Student();
student.setfName("Bob");
student.setlName("Buster");
Set<Phone> phones = new LinkedHashSet<Phone>();
Phone phone = new Phone();
phone.setColor("Black");
PhonePK phonePK = new PhonePK();
phonePK.setPhoneNumber("1111111111");
phonePK.setStudent(student); // Do not do this? But won't work (id cannot be null
error) if commented out??
phone.setPK(phonePK);
phones.add(phone);
student.setPhones(phones);
try {
transaction = session.beginTransaction();
session.save(student);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
}
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.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">cosmo99</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/ops_suite</property>
<property name="hibernate.connection.username">cosmo-security</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
</session-factory>
</hibernate-configuration>