我在这个问题上经历过多个问题,以下是我采取的一些问题:Hibernate Access Exception。 Wikibooks和Another ManyToMany extra column on Hibernate
现在,我的问题是:
在进行MayToOne映射时,我们是否需要遵循OneToMany端的内部类语法?在Wikibooks链接上说,为什么这个链接不使用OneToMany端的内部类引用?请注意,这使用IdClass而不是Embeddable方法。这有区别吗?关于Embeddable方法的任何指示?
坚持时,确切的顺序是什么?首先创建PK,然后在外部类中设置成员变量,然后持久化映射类实例?
在确切的集合实现归零时,选择的依据是什么?例如,我知道当我们需要避免重复时,HashSet实现是可选的。其次,选择ArrayList与LinkedList是附加域要求所强加的选择。我需要考虑哪些其他标准?
在编写Pk类时,我所知道的是成员变量是相应的基本数据类型而不是实体类。例如,在this link上,PK类直接使用Student和TeachingClass。这有解决方案 - 答案适用于Hibernate。 (这是否符合规范?阅读JSR后无法推断。)
我在EclipseLink 2.6上。任何人都可以用一个单一的事实来帮助我吗?
提前致谢
编辑1:这是带有问题的示例sourceCode。并感谢评论。它实际上让我提出更多问题(内联提到)。
@Entity
@Table(name="Student")
public class Student{
@Id
@Column(name="roll_number")
private String rollNumber;
private String firstName;
private String lastName;
//Which one is right? The immediate line or the one after?
@OneToMany(mappedBy="student")
@OneToMany(mappedBy="StudentSubjectPK.studentRollNumber")
Collection<StudentSubjectMap> subjecttsForThisStudent;
//Now, when I drill down to the actual Collection implementation to select,
//what are the criteria(twomentioned already. Anymore??)
public Collection<StudentSubjectMap> getSubjectsForThisStudent(){
if(subjectsForThisStudent==null) !CollectionImplementation! subjectsForThisStudent = new !CollectionImplementation!();
return subjectsForThisStudent;
}
....
}
@Entity
@Table(name="Subject")
public class Subject{
@Id
@Column(name="subject_name")
private String subject;
private String teacherName;
private boolean isElective;
private boolean isGraded;
//Which one is right? The immediate line or the one after?
@OneToMany(mappedBy="subject")
@OneToMany(mappedBy="StudentSubjectPK.subjectName")
Collection<StudentSubjectMap> studentsEnrolledInThisSubject
....
}
@Entity
public class StudentSubjectMap{
@EmbeddedId
private StudentSubjectMapPk pk;
//Should the mapping be here or inside the StudentSubjectPK class?
@ManyToOne(targetEntity="Student.class")
//Is the PrimaryKeyJoinColumn annotation column in the next line redundant?
@PrimaryKeyJoinColumn(name="roll_number",referencedColumn="roll_number")
private Student student;
@ManyToOne(targetEntity="Subject.class")
@PrimaryKeyJoinColumn(name="subject_name",referencedColumn="subject_name")
private Subject subject;
//Do I need the setters and getters for the student and subject fields above?
private float finalGrade;
private float attendancePercentage;
@Embeddable
public static class StudentSubjectMapPK{
@Column(name="roll_number")
private String studentRollNumber;
@Column(name="subject_name")
private String subjectName;
//Are getters and setters necessary here? Why?
}
}
答案 0 :(得分:0)
对于我遇到的一些问题,这里是算法(基本上是一系列JPA步骤)
我会在几天内发布学生和主题课程的确切工作代码,并尝试回答其他一些问题,这些问题会在随后的评论中陷入困境。