我正在通过NetBeans 8.1与Java DB作为数据库和EclipseLink(JPA 2.1)提供程序来探索JPA。我正在尝试OneToOne双向关系。我有2个班级学生和科目。
Student.java
@Entity
public class Student implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Student() {}
public Student(String name) { this.name = name; }
public static void print(EntityManager entitymanager)
{
//Print the database
List<Student> list=(List<Student>)entitymanager.createQuery("select e from Student e").getResultList( );
System.out.println("Student");
System.out.println("Id\tName\tSubject");
for( Student e:list ) System.out.println(e.getId()+"\t"+e.getName()+"\t"+e.getSubject().getId());
}
@OneToOne(cascade = CascadeType.ALL, orphanRemoval=true)
Subject subject;
public Subject getSubject() { return subject; }
public void setSubject(Subject subject) { this.subject = subject; }
}
Subject.java
@Entity
public class Subject implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // Increment of 1
private Long id;
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Subject() {}
public Subject(String name) { this.name = name; }
@OneToOne(mappedBy = "subject", cascade = CascadeType.ALL, orphanRemoval=true)
private Student student;
public Student getStudent() { return student; }
public void setStudent(Student student) { this.student = student; }
public static void print(EntityManager entitymanager)
{
//Print the database
List<Subject> list=(List<Subject>)entitymanager.createQuery("select e from Subject e").getResultList( );
System.out.println("Subject");
System.out.println("Id\tName\tStudent");
for( Subject e:list ) {System.out.println(e.getId()+"\t"+e.getName()+"\t"+(e.student == null ? "null":e.student.getId()));}
}
}
main()函数
public static void main(String[] args)
{
// Get entity manager and begin the transaction
EntityManagerFactory emfactory = javax.persistence.Persistence.createEntityManagerFactory("JPA_TestPU");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction().begin();
System.out.println("\nREAD : ");
Student.print(entitymanager); Subject.print(entitymanager);
Subject maths = new Subject();
maths.setName("Maths");
Student archit = new Student();
archit.setName("Archit");
archit.setSubject(maths);
entitymanager.persist(archit);
entitymanager.flush();
System.out.println("\nCREATE : Archit selected Maths");
// I want to print maths.getStudent(), which does not work
System.out.println("-----------"+maths.getId()+"---"+maths.getStudent());
Student.print(entitymanager); Subject.print(entitymanager);
Subject science = new Subject();
science.setName("Science");
archit.setSubject(science);
entitymanager.persist(archit);
entitymanager.flush();
System.out.println("\nUPDATE : Archit selected Science");
Student.print(entitymanager); Subject.print(entitymanager);
// Commit and close entity manager
entitymanager.getTransaction().commit();
entitymanager.close();
emfactory.close();
}
我希望Subject.print()应该获得成员“student”的良好价值,但它是null。
main()的输出
READ :
Student
Id Name Subject
Subject
Id Name Student
CREATE : Archit selected Maths
-----------1---null
Student
Id Name Subject
1 Archit 1
Subject
Id Name Student
1 Maths null
UPDATE : Archit selected Science
Student
Id Name Subject
1 Archit 2
Subject
Id Name Student
2 Science null
我发现数据库已正确更新且Student表具有SubjectID但Subject表没有StudentID。
有人能解释我为什么我没有在Subject对象中获得学生价值吗?