JPA @OneToMany取空

时间:2011-12-07 13:54:52

标签: jpa one-to-many

表格

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 oneToMany 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 是空的,显然我想念一些东西,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

我认为,你错过了反面的“mappedBy”...尝试以下内容:

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