Hibernate is not inserted into the DTYPE – value NULL

I basically have the following categories:

@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 */
}

< p>I want Hibernate to use the column DTYPE varchar(31) that exists in the user table by default. When my code tries to persist a new Admin object, I see the following is logged in to the console:

Hibernate: insert into user (email _address) values ​​(?)
Hibernate: insert into admin (role) values ​​(?)

So, Hibernate will not try to set the user.DTYPE column, and it remains NULL in the database. I expect it will be set to Admin.

What am I doing wrong? I also tried to explicitly set @DiscriminatorColumn on the User class and @DiscriminatorValue on the Admin class (and tried to add to User), but to no avail.

When using InheritanceType.JOINED, the Discriminator column is really not needed, and based on this bug report, it is obviously not applicable to hibernate annotations.

In your In the comment, you said “Because no user object can be instantiated directly, there is no need for a type discriminator because the type will be known in advance.” That’s right. But the main point is that for connection inheritance, the content retrieved from the DB The type of is calculated according to which table it comes from, and does not have to be stored in the column.

There seems to be some confusion and debate about whether the discriminatory column should be supported. In fact, the error I mentioned above was rejected because The hibernate team thinks it should not be supported.

I basically have the following categories:

@ 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 */
}

< p>and

@Entity
@Tab le( 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 want Hibernate to use it in the user table by default The column DTYPE varchar(31). When my code tries to persist the new Admin object, I see the following is logged into the console:

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

Therefore, Hibernate will not try to set the user.DTYPE column, and it remains in the database as NULL. I expected it to be set to Admin.

What am I doing wrong? I also tried to explicitly set @DiscriminatorColumn on the User class and @DiscriminatorValue on the Admin class (and tried to add to User), but to no avail.

When using InheritanceType.JOINED, the Discriminator column is really not needed, and based on this bug report, it obviously does not apply to hibernate annotations.

In your comment, you said “Because there is no user object to directly Instantiation, so there is no need for a type discriminator, because the type will be known in advance. “That’s right. But the main point is that for connection inheritance, the type of content retrieved from the DB is calculated based on which table it comes from, not necessarily Stored in the column.

There seems to be some confusion and controversy as to whether the authentication column should be supported. In fact, the error I mentioned above was rejected because the hibernate team believes that it should not be supported.

Leave a Comment

Your email address will not be published.