Let's say I've got a class called User and a class ExtendedUser, which is a subclass of User. Both classes are mapped using hibernate (JPA) annotations:
User:
@Entity
@Table("user-table")
@Inheritance(strategy = InheritanceType.JOINED)
public class User {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
@Column(name = "name")
private String name;
}
ExtendedUser:
@Entity
@Table(name = "extended-user-table")
@PrimaryKeyJoinColumn(name="id")
public class ExtendedUser extends User{
@Column(name = "username")
private String username;
@Column(name = "password")
private String password;
}
Sometimes a user needs to be promoted to an ExtendedUser; similarly an ExtendedUser sometimes needs to be demoted to a User.
First question: is there any way in hibernate to perform this directly?
Not being able to find such a way I tried to do the following (example for demoting):
- Copy the fields over from the ExtendedUser instance to a new instance of User.
- Delete the ExtendedUser instance
- Save the User instance
However, this seems to automatically increment the id when trying to save the new User. All of this is being done in a transaction - could that be the cause? However, I do not want to take them outside of the transaction, since then automatic rollback will not work if, say, the save fails.
Second question (in case the answer to the first one is no): how do I fix the issue of IDs automatically incrementing.
Thanks in advance.