2011-12-07 86 views
0

JPA @OneToMany取空

APP_USER 
    ROLE_GROUP_ID 
    APP_USER_ID (PK) 
    USERNAME 
    ROLE_GROUP_ID  

ROLE_GROUP 
    ROLE_GROUP_ID  
    ROLE_ID 
....  

ROLE_STATE (this table has composite PK) 
ROLE_ID (PK) 
STATE_ID (PK) 
.... 

我能爲APP_USER檢索結果OnetoOne ROLE_GROUP,但ROLE_GROUP一對多ROLE_STATE是空的。我使用org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean來映射對象。


@Entity 
    @Table(name = "APP_USER") 
    public class AppUser implements Serializable{ 

     private static final long serialVersionUID = 1L; 

     @Column(name = "ROLE_GROUP_ID") 
     private Integer roleGroupIdAppuser; 

     @Id 
     @Column(name="APP_USER_ID") 
     Integer appUserID; 

     @Column(name = "USERNAME") 
     private String userName; 

     @OneToOne(fetch=FetchType.EAGER) 
     @JoinColumn(name="ROLE_GROUP_ID",insertable=false,updatable=false) 
     private RoleGroup roleGroup;  


    @Entity 
    @Table(name = "ROLE_GROUP") 
    public class RoleGroup implements Serializable{ 

     @Id 
     @Column(name="ROLE_GROUP_ID") 
     private Integer roleGroupId; 

     @Column(name="ROLE_ID") 
     private Integer roleID; 

     @Column(name="APP_GROUP_ID") 
     private Integer appGroupId; 

     @OneToMany(fetch=FetchType.EAGER) 
     @JoinColumn(name="ROLE_ID") 
     Set<RoleState> roleState; 

     public Integer getRoleGroupId() { 
      return roleGroupId; 
     }  

    @Entity 
    @Table(name = "ROLE_STATE") 
    public class RoleState implements Serializable { 

     private static final long serialVersionUID = 1L; 

     /*@Embeddable 
     public static class Pk implements Serializable { 

      @Column(name = "ROLE_ID", nullable = false, updatable = false) 
      private Integer roleId; 

      @Column(name = "STATE_ID", nullable = false, updatable = false) 
      private Integer stateId; 

      @Column(name = "TARGET_STATE_ID", nullable = false, updatable = false) 
      private int targetStateID; 
     } 

     @EmbeddedId 
     private Pk pk;*/ 

     @Id 
     @Column(name = "ROLE_ID") 
     private Integer roleId; 

     @Id 
     @Basic(optional = true) 
     @Column(name = "STATE_ID") 
     private Integer stateId; 

     @Id 
     @Basic(optional = true) 
     @Column(name = "TARGET_STATE_ID") 
     private Integer targetStateID; 

     @OneToOne(fetch = FetchType.LAZY) 
     @JoinColumn(name = "STATE_ID",insertable=false,updatable=false) 
     State state; 

    AppUser appUser=(AppUser)currentSession().createQuery("from AppUser where  userName=:userNamel").setParameter("userNamel", userNamel.toUpperCase()).list().get(0); 

      System.out.println(appUser.getRoleGroup().getRoleID()); 
      Set<RoleState> roleState=appUser.getRoleGroup().getRoleState(); 
      for(RoleState role:roleState){ 
       System.out.println(role.getState().getName()); 
      } 

      System.out.println(appUser.getUserName()); 

roleState是空的,顯然我錯過的東西,任何幫助,將不勝感激。

回答

0

我想,你是缺少 「的mappedBy」 爲反方...嘗試以下操作:

@OneToMany(mappedBy="roleId", fetch=FetchType.EAGER) 
@JoinColumn(name="ROLE_ID") 
Set<RoleState> roleState;