2014-10-01 63 views
0

我想开发一个web应用程序,我想知道是否有一种方法可以在不编写大量代码的情况下利用外键。用Hibernate的Java中的外键使用

我Trainees.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一个未知的实体:字符串

所以我的目标是使用受训者表和类,我可以检索Status_Trainee表的值使用外国科目年。例如:如果外键ID是2,那么它将从status_trainee表中检索一个字符串,其中主键将与外键ID匹配。

我正在使用模型,controlers,hibernate和angularjs来显示视图,我真的不想通过这一切通过表,我认为使用像ManyToOne或JoinColumns会检索值?

感谢您的帮助!

+0

可能的重复[如何使用hibernate注释创建外键约束?](http://stackoverflow.com/questions/15426736/how-can-i-create-a-foreign-key -constraint-using-hibernate-annotations) – 2014-10-01 15:16:09

+0

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/associations.html – 2014-10-01 15:16:46

+0

@NoComments您好,感谢您的信息,我已将问题更新到更多地反映我的问题。我想知道它是结构问题吗? – Spinxas 2014-10-01 15:29:48

回答

0

您应该在培训生中添加对StatusTrainee的参考,并使用OneToMany,ManyToOne或OneToOne对其进行注释。根据您的关系类型,您需要一份StatusTrainee或一份StatusTrainee的清单。

提示:不要在类名中使用下划线。

+0

你好,谢谢你的回复,我包括@ ManyToOne @ JoinColumn(name =“status_trainee”),而不是int我实现了字符串,但问题在于它不能识别实体为String或Int,是否需要不同的结构体? – Spinxas 2014-10-01 15:27:21

+0

您不应该使用id的字段作为参考,而是使用整个对象。 Hibernate会知道使用外键。因此请使用私人Status_Trainee statusTrainee;作为变量并对其进行注释。注释像int这样的基本类型是没有意义的。 – Juru 2014-10-01 15:29:52

+0

谢谢解决了这个问题。 :) – Spinxas 2014-10-01 15:35:31

1

首先,建议不要在使用hibernate时在类名中使用“_”。访问foreignKeys时,Hibernate使用下划线。因此,可以说,你的类重命名为:TraineeStatus和ID将其更改为traineeStatusId ..

其次,您可以使用Hibernate注解,你需要什么。但首先你需要知道的关系是怎样:

@OneToMany:一是培训生可以有很多状态

@ManyToOne的:很多学员可以有相同的地位

@OneToOne:一个学员只能有一个状态,而另一个方向。

试试这个:

@Entity 公共类学员{

@Id 
@GeneratedValue 
private int traineesID; 
private int groupsID; 

@OneToOne 
private TraineeStatus status; 
private int customersID; 
private String name; 
private String surname; 
private String phoneDetails; 
private String email; 

... 

您可以更改您需要的一个@OneToOne ..

请记住,Hibernate会尝试映射此在你的受训者mysql表中作为status_traineeStatusId,所以如果你在你的trainess表中有这个列(作为整数),你就完成了:) ..

就是这样.. 希望它有帮助