【发布时间】:2014-10-01 15:07:35
【问题描述】:
我正在尝试开发一个 Web 应用程序,我想知道是否有一种方法可以在不编写大量代码的情况下使用外键。
我的学员.java
@Entity
public class Trainees {
@Id
@GeneratedValue
private int traineesID;
private int groupsID;
@ManyToOne
@JoinColumn(name = "status_trainee")
private String status_TraineeID;
private int customersID;
private String name;
private String surname;
private String phoneDetails;
private String email;
public Trainees(){
}
public Trainees(String name, String surname, String phoneDetails, String email, int id, int groupsID, String status_TraineeID, int customersID) {
super();
this.name = name;
this.surname = surname;
this.email = email;
this.phoneDetails = phoneDetails;
this.groupsID = groupsID;
this.status_TraineeID = status_TraineeID;
this.customersID = customersID;
}
//getters and setters
@Override
public boolean equals(Object object) {
if (object instanceof Trainees){
Trainees contact = (Trainees) object;
return contact.traineesID == traineesID;
}
return false;
}
@Override
public int hashCode() {
return traineesID;
}
}
Status_Trainee.java
@Entity
public class Status_Trainee {
@Id
@GeneratedValue
private int status_traineeID;
private String value;
public Status_Trainee(){
}
public Status_Trainee(String value, int id) {
super();
this.value = value;
}
//getters and setters
@Override
public boolean equals(Object object) {
if (object instanceof Status_Trainee){
Status_Trainee value = (Status_Trainee) object;
return value.status_traineeID == status_traineeID;
}
return false;
}
@Override
public int hashCode() {
return status_traineeID;
}
}
错误:原因:org.hibernate.AnnotationException:uaiContacts.model.Trainees.status_TraineeID 上的@OneToOne 或@ManyToOne 引用了未知实体:字符串
所以我的目标是使用 Trainees 表和类,我可以使用外键检索 Status_Trainee 表的值。例如:如果外键 ID 为 2,那么它将从 status_trainee 表中检索一个字符串,其中主键将与外键 ID 匹配。
我正在使用模型、控制器、hibernate 和 angularjs 来显示到视图中,我真的不想通过所有这些来传递表格,我认为使用 ManyToOne 或 JoinColumns 之类的东西会检索值?
感谢大家的帮助!
【问题讨论】:
-
@NoComments 您好,感谢您提供的信息,我已更新问题以更多地反映我的问题。我想知道是结构问题吗?
标签: java mysql sql angularjs hibernate