嗨,我刚刚创建了一个应用程序,用一对一的映射保存表中的数据
`Exception in thread "main" org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.mapping.onetoone.Instructor.theInstructorDetail references an unknown entity: com.mapping.onetoone.InstructorDetail`
这是我的代码@Entity和OneToOne annotations Instructor.java
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@OneToOne(cascade=CascadeType.ALL )
@JoinColumn(name="instructor_detail_id")
private InstructorDetail theInstructorDetail;
public Instructor(){}
public Instructor(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public InstructorDetail getTheInstructorDetail() {
return theInstructorDetail;
}
public void setTheInstructorDetail(InstructorDetail theInstructorDetail) {
this.theInstructorDetail = theInstructorDetail;
}
@Override
public String toString() {
return "Instructor [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", email=" + email
+ ", theInstructorDetail=" + theInstructorDetail + "]";
}
这是我的InstructorDetail.java
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="youtube_channel")
private String youTubeChannel;
@Column(name="hobby")
private String hobby;
public InstructorDetail(){}
public InstructorDetail(String youTubeChannel, String hobby) {
super();
this.youTubeChannel = youTubeChannel;
this.hobby = hobby;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getYouTubeChannel() {
return youTubeChannel;
}
public void setYouTubeChannel(String youTubeChannel) {
this.youTubeChannel = youTubeChannel;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
@Override
public String toString() {
return "InstructorDetail [id=" + id + ", youTubeChannel=" + youTubeChannel + ", hobby=" + hobby + "]";
}
create instructor.Java
public static void main(String[] args) {
SessionFactory factory=new Configuration().configure("hibernate1.cfg.xml")
.addAnnotatedClass(Instructor.class).buildSessionFactory();
Session session = factory.getCurrentSession();
try
{
System.out.println("Object created. Now creating Instructor object...");
Instructor ins=new Instructor("elon", "musk", "[email protected]");
System.out.println("Creating InstructorDetail object...");
InstructorDetail theInstructorDetail=new InstructorDetail("vella Panti Adda", "Acting");
ins.setTheInstructorDetail(theInstructorDetail);
session.beginTransaction();
System.out.println("Saving data....");
session.save(ins);
session.getTransaction().commit();
System.out.println("Data saved!");
}
finally
{
factory.close();
}
}
谁能帮我。
你试过这个。
public Instructor(String firstName, String lastName, String email) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.theInstructorDetail= new InstructorDetail();
}
我认为你应该开始所有的属性。
您的代码设置指向OneToOne Unidirectional Association
,其中InstructorDetail是父级,而讲师是子级关联。为此,您应该在持久保存/保存Instructor实体之前保留/保存InstructorDetail。
session.beginTransaction(); session.save(theInstructorDetail); session.save(ins); session.getTransaction().commit();
如果您不想在数据库中关联FK,那么在java hibernate中创建双向关联:
instructor.Java
@OneToOne
@JoinColumn(name = "instructor_detail_id")
private InstructorDetail theInstructorDetail;
instructor detail.Java
@OneToOne(mappedBy="instructor_detail")
private Instructor theInstructor;
坚持逻辑
Instructor ins=new Instructor("elon", "musk", "[email protected]");
InstructorDetail theInstructorDetail=new InstructorDetail("vella Panti Adda", "Acting");
ins.setTheInstructorDetail(theInstructorDetail);
theInstructorDetail.setTheInstructor(ins);
session.beginTransaction();
session.save(theInstructorDetail);
session.getTransaction().commit();
如果您可以对数据库进行更改,我建议您执行以下操作之一:
将Instructor
作为主要表格和InstructorDetail
作为次要表格,逻辑上更有意义。也就是说,从instructor_detail_id
表中删除Instructor
列并在InstructorDetail表中添加instructor_id
列。然后,只需在java类中翻转hibernate注释配置(与上面描述的相反)。
由于它的OnetoOne关系,为了减少内存上的索引占用量,对这两个表使用相同的PK Instructor_Id
。然后你可以使用@MapsId
注释而不必使用Bi-directional association
。
instructor detail.Java
@OneToOne
@MapsId
private Instructor theInstructor;