2010-09-01 6 views
0

J'ai créé des tables MySQL: tabel rôle, object_label et role_object_label (table liens)annotations Hibernate @ManyToMany

je définissais @ManyToMany et j'obtient exception. quel est le problème dans mon code?

@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 défini:

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

I obtient exception:

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

Merci! Rivki

+0

Définissez la consignation sur déboguer et recherchez une erreur au moment de la création de 'SessionFactory'. Il y a probablement quelque chose qui ne va pas avec 'ObjectLabel'. –

+0

Comment avez-vous défini vos variables de classe? – anirus

Répondre

1

L'erreur explique que ObjectLabel n'est pas une classe d'entité.

Vous avez annoté votre classe avec @Entity, mais vous avez oublié de mettre une annotation @Id. @Entity et @Id sont obligatoires pour déclarer une classe d'entité appropriée.

+0

J'ai modifié la question, j'ai défini Id dans la classe ObjectLabel mais je ne les ai pas écrits dans la question le nom de la propriété dans ObjectLabel sans annotation, car c'est le même nom dans DB. quel est le problème maintenant? – Rivki

+0

Personne n'a de réponse? – Rivki