gpt4 book ai didi

java - 线程中的异常 "main"org.hibernate.AnnotationException : @OneToOne or @ManyToOne on

转载 作者:行者123 更新时间:2023-11-29 16:11:21 24 4
gpt4 key购买 nike

嗨,我刚刚创建了一个应用程序,它通过一对一映射保存表中的数据

onetoone database contains table instructor and instructor_detail每当我尝试将数据保存在表中时,我都会收到以下错误。

`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 注释的代码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 + "]";
}

CreateInstructor.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", "elonmusk@hotmail.com");

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();
}

我认为你应该启动所有属性。

关于java - 线程中的异常 "main"org.hibernate.AnnotationException : @OneToOne or @ManyToOne on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55227182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com