Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate not inserting DTYPE - value is null

I have essentially the following classes:

@Entity
@Table( name="user" )
@Inheritance( strategy = InheritanceType.JOINED )
public abstract class User implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue( strategy = GenerationType.IDENTITY )
  @Column( name = "id" )
  private Long id;

  @Column( name = "email_address" )
  private String emailAddress;

  /* getters and setters not shown */
}

and

@Entity
@Table( name="admin" )
public class Admin extends User implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Column( name = "role" )
  private String role;

  /* getters and setters not shown */
}

I expect Hibernate to use by default a column DTYPE varchar(31) which exists in the user table. When my code tries to persist a new Admin object I see the following logged to console:

Hibernate: insert into user (email_address) values (?)
Hibernate: insert into admin (role) values (?) 

Thus no attempt is made by Hibernate to set the user.DTYPE column, and it remains NULL in the database. I expected it would be set to Admin.

What am I doing wrong? I have also tried explicitly setting @DiscriminatorColumn on the User class and @DiscriminatorValue on the Admin class (and also tried adding to User as well) but to no avail.

like image 320
Alex Avatar asked May 10 '13 11:05

Alex


1 Answers

A Discriminator column is really not needed when using InheritanceType.JOINED, and based on this bug report, apparently doesn't work with hibernate annotations.

In your comment, you say "Because no User object can be instantiated directly, there is no need for a type discriminator, since the type will be known ahead of time." and that's sort of right. But the main point really is that for joined inheritance, the type of something retrieved from the DB is figured out based on which table it came from and doesn't have to be stored in a column.

There appears to be some confusion and debate as to whether discriminator columns should be supported, and in fact the bug I noted above was rejected, as the hibernate team decided it shouldn't be supported.

like image 134
Don Roby Avatar answered Oct 13 '22 11:10

Don Roby