1

I am new to Hibernate and I cannot create joined sub-class table while using Hibernate annotation.

Here is my code.

This is the main class.

@Entity
@Table(name="CRM_User")
@Inheritance(strategy=InheritanceType.JOINED)
public class UserImp extends BaseModel implements IUser, Serializable
{
  ... ...

And Staff class extends User class.

@Entity
@Table(name="CRM_Staff") 
@PrimaryKeyJoinColumn(name="Id")
public class StaffImp extends UserImp implements IStaff, Serializable
{
  ... ...   

And when I run the unit test, I get the error.

/* Test get all User */
@Test
public void testGetAllUser()
{
    List<IUser> users = (List<IUser>) this.userDAO.getAll("UserImp");
    assertEquals(2, users.size());
}

This is the error.

......
19:31:04,880  INFO SchemaExport:281 - schema export complete
19:31:04,918  INFO DefaultTraversableResolver:81 - Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
19:31:05,158  WARN JDBCExceptionReporter:233 - SQL Error: 1146, SQLState: 42S02
19:31:05,158 ERROR JDBCExceptionReporter:234 - Table 'test.crm_staff' doesn't exist  
... ...

Thanks so much!

Charles
  • 675
  • 3
  • 12
  • 21

1 Answers1

1

Your annotations seem to be correct so: does table 'crm_staff' exist? Did you create a database with this table or (if hibernate should create this when running the test) did you set hibernate.hbm2ddl.auto to create-drop?

Stijn Geukens
  • 15,454
  • 8
  • 66
  • 101
  • Yes, hibernate.hbm2ddl.auto is set to create. And I use spring to map the entity class. Here is what I did. `AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean(); annotationSessionFactoryBean.setDataSource(basicDataSource()); annotationSessionFactoryBean.setAnnotatedClasses(new Class[]{ domainObj.user.UserImp.class, //domainObj.user.staff.StaffImp.class /* If I uncomment the above line, I get another error: 12:27:25,168 ERROR JDBCExceptionReporter:234 - Unknown column 'children15_.CRM_User.Id' in 'order clause' */ });` – Charles Apr 17 '11 at 02:29
  • Is there any reason you are not using spring to manage your bean creation? http://onjava.com/pub/a/onjava/2007/02/08/an-introduction-to-hibernate-3-annotations.html – Stijn Geukens Apr 17 '11 at 10:30