2010-09-01 51 views
0

我創建的表在MySQL: 作用TABEL,object_label和role_object_label(鏈接表)Hibernate的註解@ManyToMany

我定義@ManyToMany,我得到異常。 我的代碼有什麼問題?

@Entity 
@Table(name = "object_label") 
public class ObjectLabel implements Serializable { 

    private static final long serialVersionUID = 3475812350796110403L; 
    private String name; 

    public Long getId() { return id; } 

    @Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
@Column(precision = 10, unique = true, nullable = false, updatable = false) 
public Long getId() { 
    return id; 
} 

@Override 
public void setId(Long id) { 
    this.id = id; 
} 

    /** 
    * @return the name 
    */ 
    public String getName() { 
     return name; 
    } 
    /** 
    * @param name the name to set 
    */ 
    public void setName(String name) { 
     this.name = name; 
    } 


} 


@Entity 
@Table(name = "role") 
public class Role implements Serializable { 

public Long getId(){return id; }

@Id 
    @GeneratedValue(strategy = GenerationType.AUTO) 
@Column(precision = 10, unique = true, nullable = false, updatable = false) 
public Long getId() { 
    return id; 
} 

@Override 
public void setId(Long id) { 
    this.id = id; 
} 

    @ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(
     name = "role_object_label", joinColumns = @JoinColumn(name = "role_id"), 
     inverseJoinColumns = @JoinColumn(name = "object_label_id")) 
    public Set<ObjectLabel> getObjectLabels(){ 
     return this.objectLabels; 
    } 

    /** 
    * @param objectLabels the objectLabels to set 
    */ 
    public void setObjectLabels(Set<ObjectLabel> objectLabels) { 
     this.objectLabels = objectLabels; 
    } 

    private Set<ObjectLabel> objectLabels = new HashSet<ObjectLabel>(); 
} 

在hibernate.cfg.xml定義:

<mapping class="com.myCompany.model.RoleObjectLabel" /> 
<mapping class="com.myCompany.model.ObjectLabel" /> 

我得到異常:

Caused by: org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.sintecmedia.model.Role.objectLabels[com.myCompany.model.ObjectLabel] 

謝謝! Rivki

+0

將記錄設置爲調試並在'SessionFactory'創建時查找錯誤。 ObjectLabel可能有問題。 – 2010-09-01 14:00:42

+0

你是如何定義類變量的? – anirus 2017-04-19 17:22:15

回答

1

該錯誤解釋了ObjectLabel不是實體類。

你用@Entity註釋了你的類,但是你忘記了@Id註解。 @Entity和@Id都是強制聲明一個合適的實體類。

+0

我編輯了這個問題,我在ObjectLabel類中定義了Id,但是我沒有將它們寫在ObjectLabel中的屬性名稱 中,因爲它在數據庫中是同名的。 現在有什麼問題? – Rivki 2010-09-01 10:52:55

+0

沒有人有答案? – Rivki 2010-09-02 06:59:13