2014-10-02 32 views
1

我有两个模型,他们都使用hibernate并使用mySQL,他们有一个ManyToOne关系,我使用AngularJS来显示视图。我想知道是否有办法从TraineeStatus模型中检索对象列表并将其存储在Trainee模型中?如何从同一模型中的其他表中检索对象列表?

TraineeStatus.java

@Entity 
public class Trainees { 

    @Id 
    @GeneratedValue 
    private int traineesID; 
    @ManyToOne 
    private Technologies technologies; 
    @ManyToOne 
    private TraineeStatus traineestatus; 
    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, Technologies technologies, TraineeStatus traineestatus, int customersID) { 
     super(); 
     this.name = name; 
     this.surname = surname; 
     this.email = email; 
     this.phoneDetails = phoneDetails; 
     this.technologies = technologies; 
     this.traineestatus = traineestatus; 
     this.customersID = customersID; 
    } 
//getters and setters 

TraineeStatus.java

@Entity 
@Table(name="traineeStatus") 
public class TraineeStatus { 

    @Id 
    @GeneratedValue 
    private int traineeStatusId; 
    private String value; 

    public TraineeStatus(){ 

    } 

我的目标是从TraineeStatus与物体内部的ID和价值,在我的学员模型使用它检索对象的数组。可以做到吗?谢谢你的问候。

回答

0

试试吧

@Entity 
@Table(name="traineeStatus") 
public class TraineeStatus { 


    @OneToMany(mappedBy = "traineestatus") 
    private List<Trainees> orderItems; 

    ... 

    // GETTERS and SETTERS 

} 
+0

好主意感谢,只是我需要相反的,我需要检索TraineeStatuses的名单,我已经实现@ManyToOne(取= FetchType.LAZY) @JoinColumn(name = “traineestatus” ) private List traineestatus;但它表示目标实体未定义。 – Spinxas 2014-10-02 12:26:57

+0

/@ ManyToOne注解不能应用于List字段,如果你有“一对多”关联使用@OneToMany annnotation – kemenov 2014-10-02 12:32:17

+0

我的逻辑是一个或多个学员只能拥有一个状态,控制器坐在学员身上模型,所以我可以从traineestatus模型中检索数据,仅从实习生那里,我认为可以在实习生模型中检索整个表格,而不是为traineestatus模型创建另一个控制器。 – Spinxas 2014-10-02 12:34:13

相关问题