2013-05-30 163 views
0

我想在@一對多要使用映射,但它拋出一個異常休眠的mappedBy複合鍵

org.hibernate.MappingException: Unable to read the mapped by attribute for moduleRoles in org.caau.entity.UserModuleRole! 

這裏是我的實體mapping.UserModuleRole是複合class.if使用註釋代碼,程序是correct.Does有人幫我解決?

@Entity 
@Table(name = "user") 
public class User{ 
    private long id; 
    private Set<UserModuleRole> moduleRoles; 

    @Id 
    @Column(name = "id", nullable = false) 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    public long getId() { 
     return id; 
    } 

    @OneToMany(fetch = FetchType.EAGER,mappedBy="user") 
    //if use under code,program is correct,but i want to use mappedBy! 
    //@OneToMany(fetch = FetchType.EAGER) 
    //@JoinColumn(name = "user_id") 
    public Set<UserModuleRole> getModuleRoles() { 
     return moduleRoles; 
    } 

} 

這裏是類UserModuleRole

@Entity 
    @Table(name = "user_module_role") 
    @IdClass(UserModuleRolePK.class) 
     public class UserModuleRole{ 
     private User user; 
     private ModuleRole moduleRole; 
     private long addUserId; 
     private Date addDate; 
     private long updateUserId; 
     private Date updateDate; 

     @Id 
     public User getUser() { 
      return user; 
     } 

     @Id 
     public ModuleRole getModuleRole() { 
      return moduleRole; 
     } 
    } 
    class UserModuleRolePK implements Serializable { 

     private static final long serialVersionUID = -9132981262254922539L; 

     private User user; 
     private ModuleRole moduleRole; 

     @ManyToOne 
     @JoinColumn(name = "user_id", nullable = false) 
     public User getUser() { 
      return user; 
     } 

     @ManyToOne 
     @JoinColumns({ @JoinColumn(name = "module_id", nullable = false), @JoinColumn(name = "role_id", nullable = false) }) 
     public ModuleRole getModuleRole() { 
      return moduleRole; 
     } 
    } 
    } 

回答

1

首先,你所得到的例外是由於這樣的事實,沒有在你的UserModuleRole類名爲user任何關係屬性。

你所想要實現一個它擁有許多一對多的實體UserModuleRole之間關係,中介類的例子是不需要的,因爲只有場UserModuleRole /屬性的外鍵引用到另外兩個實體。

除非你忘了更多的信息添加到您的問題,您應該使用映射爲簡單得多,類似如下:

@Entity 
public class ModuleRole { 

    private Long id; 

    private Set<User> users; 

    @Id 
    @Column(name = "id") 
    public Long getId() { 
     return id; 
    } 

    @ManyToMany 
    @JoinTable(name = "user_module_role", 
     joinColumns = @JoinColumn(name = "module_id", referencedColumnName = "id"), 
     inverseJoinColumns = @JoinColumn(name = "user_id"), referencedColumnName = "id") 
    public Set<Users> getUsers() { 
     return users; 
    } 

} 

@Entity 
@Table(name = "user") 
public class User{ 
    private Long id; 
    private Set<ModuleRole> moduleRoles; 

    @Id 
    @Column(name = "id", nullable = false) 
    @GeneratedValue(strategy = GenerationType.AUTO) 
    public long getId() { 
     return id; 
    } 

    @ManyToMany(mappedBy = "users") 
    public Set<ModuleRole> getModuleRoles() { 
     return moduleRoles; 
    } 

} 

Hibernate會處理中介表上自己的,你不」除非你想添加額外的領域的關係,在這種情況下,this tutorial將有所幫助。

+0

但我user_module_role有其他領域 創建表user_module_role( \t的user_id INT無符號不爲空, \t是模塊INT無符號不爲空, \t ROLE_ID INT無符號不爲空, \t add_user_id INT無符號的默認值是1, add_date日期時間默認'1970/01/01', update_user_id int unsigned default 1, update_date datetime default'1970/01/01'); –

+0

您是否檢查了我在答案末尾放置的教程鏈接? –