我正在通过NetBeans 8.1与Java DB作为数据库和EclipseLink(JPA 2.1)提供程序来探索JPA。我正在尝试OneToMany双向关系。我有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; }
@OneToMany(mappedBy = "student", cascade = CascadeType.ALL, orphanRemoval=true)
List<Subject> subject;
public List<Subject> getSubject() { return subject; }
public void setSubject(List<Subject> subject) { this.subject = subject; }
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\tSubjectId");
for( Student e:list ) { System.out.print(e.getId()+"\t"+e.getName()+"\t"); for(Subject s : e.subject) System.out.print(s.getId()+",");System.out.println();}
}
}
Subject.java
@Entity
public class Subject 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 Subject() {}
public Subject(String name) { this.name = name; }
@ManyToOne
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);
LinkedList<Subject> pcm = new LinkedList<Subject>();
Subject maths = new Subject();
maths.setName("Maths");
entitymanager.persist(maths);
pcm.add(maths);
Subject science = new Subject();
science.setName("Science");
entitymanager.persist(science);
pcm.add(science);
Student archit = new Student();
archit.setName("Archit");
archit.setSubject(pcm);
entitymanager.flush();
entitymanager.persist(archit);
entitymanager.flush();
System.out.println("\nCREATE : Archit selected Maths Science");
Student.print(entitymanager); Subject.print(entitymanager);
// Commit and close entity manager
entitymanager.getTransaction().commit();
entitymanager.close();
emfactory.close();
}
首次执行main()
READ :
Student
Id Name SubjectId
Subject
Id Name Student
CREATE : Archit selected Maths Science
Student
Id Name SubjectId
1 Archit 2,1,
Subject
Id Name Student
1 Science null
2 Maths null
第二次执行main()
READ :
Student
Id Name SubjectId
1 Archit
Subject
Id Name Student
1 Science null
2 Maths null
CREATE : Archit selected Maths Science
Student
Id Name SubjectId
1 Archit
2 Archit 3,4,
Subject
Id Name Student
1 Science null
2 Maths null
3 Maths null
4 Science null
我看到学生保存后,科目也会被保存。 SUBJECT表包含STUDENT_ID列作为FK。但是此列设置为NULL而不是Student对象的Id值。同时在Subject的Memory对象中不包含Student对象的有效引用,但它被设置为NULL。
任何人都可以帮我确定我是否做错了。