ManyToOne annotation fails with Hibernate 4.1: MappingException

Posted by barelas on Stack Overflow See other posts from Stack Overflow or by barelas
Published on 2012-05-31T10:01:17Z Indexed on 2012/05/31 10:40 UTC
Read the original article Hit count: 230

Filed under:
|

Using Hibernate 4.1.1.Final.

When I try to add @ManyToOne, schema creation fails with: org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister

User.java:

@Entity
public class User { 
 @Id
 private int id;
 public int getId() {return id;}
 public void setId(int id) {this.id = id;}
 @ManyToOne
 Department department;
 public Department getDepartment() {return department;}
 public void setDepartment(Department department) {this.department = department;}
}

Department.java

@Entity
public class Department {
 @Id
 private int departmentNumber;
 public int getDepartmentNumber() {return departmentNumber;}
 public void setDepartmentNumber(int departmentNumber) {this.departmentNumber = departmentNumber;}
}

hibernate.properties:

hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/dbname
hibernate.connection.username=user
hibernate.connection.password=pass
hibernate.connection.pool_size=5
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto=create

init (throwing exception):

ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().buildServiceRegistry();
sessionFactory = new MetadataSources(
serviceRegistrY.addAnnotatedClass(Department.class).addAnnotatedClass(User.class).buildMetadata().buildSessionFactory();

exception throwed at init:

org.hibernate.MappingException: Could not instantiate persister org.hibernate.persister.entity.SingleTableEntityPersister
    at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:174)
    at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:148)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:820)
    at org.hibernate.metamodel.source.internal.SessionFactoryBuilderImpl.buildSessionFactory(SessionFactoryBuilderImpl.java:65)
    at org.hibernate.metamodel.source.internal.MetadataImpl.buildSessionFactory(MetadataImpl.java:340)

I have tried adding some other annotations, but shouldn't the defaults work and create the tables and foreign key? If I remove the department from User, tables get generated fine.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about java

Related posts about hibernate