Hibernate error "object not found" using @Inheritance with Joined strategy

694 Views Asked by At

I have 3 Java classes (BaseEntity, User, Role) that are working correctly with Hibernate. Both User and Role are a subclass of BaseEntity. I have a table for my users and a table for my roles. There is a join table for the mapping of users to roles. All annotations used are javax.persistence annotations.

@MappedSuperclass
public abstract class BaseEntity {
   private String id;
   private Timestamp entityCreation;
   private Timestamp entityUpdate;
   ...
}

@Entity
@Table(name="roles")
public class Role extends BaseEntity {
   private String description;
   private String name;
   ...
}

@Entity
@Table(name="users")
public class User extends BaseEntity {
   private String username;
   private String password;

   @ManyToMany( cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY )
   @JoinTable( name = "users_roles", joinColumns = { @JoinColumn( name = "user_id" ) }, inverseJoinColumns = { @JoinColumn( name = "role_id" ) } )
   private Set<Role> roles;
   ...
}

For various reasons I need to extend my model and have a superclass for Role. This superclass needs to have a self-reference. My idea was to use the @Inheritance annotation with the joined strategy here, but I keep getting an error from Hibernate. From what I read Hibernate should understand that it needs to do a join to get attributes from the superclass.

@Entity
@Table("authorities")
@Inheritance(strategy=InheritanceType.JOINED)
public class Authority extends BaseEntity {
   private String name;

   @ManyToMany( cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH }, fetch = FetchType.LAZY )
   @JoinTable( name = "authorities_includes", joinColumns = { @JoinColumn( name = "authority_id" ) }, inverseJoinColumns = { @JoinColumn( name = "include_id" ) } )
   private Set<Authority> includes;
   ...
}

@Entity
@Table(name="roles")
public class Role extends Authority {
   private String description;
   ...
}

The error I'm getting:

[WARN] [18:30:04.004] [] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -5501, SQLState: 42501 [ERROR] [18:30:04.004] [] org.hibernate.engine.jdbc.spi.SqlExceptionHelper - user lacks privilege or object not found: ROLE1_.NAME

The Hibernate SQL output:

[DEBUG] [18:30:04.004] [] org.hibernate.SQL - 
    select
        roles0_.user_id as user_id1_22_1_,
        roles0_.role_id as role_id2_23_1_,
        role1_.id as id1_18_0_,
        role1_.entity_creation as entity_c2_18_0_,
        role1_.entity_update as entity_u3_18_0_,
        role1_.name as name4_18_0_ 
    from
        odm_users_roles roles0_ 
    inner join
        odm_roles role1_ 
            on roles0_.role_id=role1_.id 
    where
        roles0_.user_id=?

I am using Hibernate 4.2.19

As you can see, Hibernate is trying to select the value of the name attribute from the authority table, which is correct, but is then complaining that that column does not exist on that table.

How do I fix this issue?

0

There are 0 best solutions below