1

Hello i have my entity class Enterprise and Role, with this code:

@Entity
@Table(name = "enterprises")
public class Enterprise implements Serializable{

@Id
@Column( name = "user_name" )
private String userName;
private String name;
@Column ( name = "tax_id" )
private String taxId;
private String email;
@Column ( name = "contact_name" )
private String contactName;
@Column ( name = "contact_surname" )
private String contactSurname;
private String phone;
@Column ( name = "enabled_account" )
private Boolean enabledAccount;
@ManyToMany(targetEntity = Role.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "enterprise_role", joinColumns = { 
        @JoinColumn(name = "id_enterprise", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "id_role", 
                nullable = false, updatable = false) })
private List<Role> roles;
@Column ( name = "enterprise_description" )
private String enterpriseDescription;
private String password;

public Enterprise() {
  roles  = new ArrayList<Role>();
}
//the getters and setters

And my Role class:

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

    @Id
    @Column ( name = "id_role" ) 
    private Integer id;

    @Column ( name = "role_type" )
    private String roleType;
    private String description;

    public Role() {
    }

When i save an object y have not problem but when i try to execute this query: from Enterprise order by user_name desc

Obtain this error:

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an     unmapped class: com.efix.model.Enterprise.roles[com.efix.model.Role]
at          org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1068)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:600)
at org.hibernate.cfg.annotations.CollectionBinder$1.secondPass(CollectionBinder.java:541)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:324)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1286)
at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:859)

And i have defined this Entities in the hibernate.xml config file, pointing to the class with the entire path, example com.example.Enterprise or com.example.Role.

Can any body why is this? Thanks in advance

My hibernate.cfg is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
    <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/prueba_efix</property>
    <property name="hibernate.connection.username">postgres</property>
    <property name="hibernate.connection.password">postgres</property>
    <mapping class="com.efix.model.Enterprise"/>
    <mapping class="com.efix.model.Role"/>
  </session-factory>
</hibernate-configuration>

And the Configuration File i generate with Netbeans at HibernateUtil.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user1977204
  • 234
  • 5
  • 17
  • Please show us your `hibernate.xml` file and your `Configuration` setup. – Sotirios Delimanolis Sep 30 '13 at 18:37
  • I edit the post and put the hibernate.cfg, the HibernateUtil is the default – user1977204 Sep 30 '13 at 18:47
  • I believe you can remove the 'targetEntity' property and try this. – Zeus Sep 30 '13 at 18:51
  • The exception persist, is rarely because i have defined the entity in my .cfg . Another Sugestion? – user1977204 Sep 30 '13 at 18:55
  • 1
    Which entity annotation are you using in some questions like this http://stackoverflow.com/questions/4956855/hibernate-problem-use-of-onetomany-or-manytomany-targeting-an-unmapped-clas problem occurs due to importing from hibarnate package instead of javax.persistance package – fgakk Sep 30 '13 at 21:28
  • I have the javax.persistance package importations. I dont know how the problem is, never happens to me this and is weird error. – user1977204 Sep 30 '13 at 21:44

1 Answers1

0

Remove the targetEntity = Role.class part from @ManyToMany annotation of Enterprise entity.

Masudul
  • 21,823
  • 5
  • 43
  • 58