Hello,
I want to know hot to map in JPA/Hibernate with a legacy database a table containing two-columns composite keys, being one of them a discriminator column for a table-per-class hierarchy.
I have a case similar to the one that appears in the book "Java persistance with Hibernate", chapter 8.1.1, section "Composite keys with annotations".
I have to create a Java program for a database that contains a table for users with a composite key with two columns: user_id & department_code.
The column department_code is used as well for distinguish the kind of user, attending to the department criteria. In the application context the next rows could exist:
Code:
USER_ID DEPARTMENT_CODE NAME EMAIL
ghouse Medical Gregory House
[email protected]ghouse Management Gregory House
[email protected]lcuddy Management Lisa Cuddy
[email protected]There're specific applications for each kind of Users, so we want to modelize a hierarchy to use the classes separately.
I tried this;
Code:
@Embeddable
public class UserId implements Serializable {
@Column("USER_ID")
private String username;
@Column("DEPARTMENT_CODE")
private String departmentCode;
...
}
@Entity
@Table(name = "USERS")
@DiscriminatorColumn("DEPARTMENT_CODE")
public abstract class User {
@Id
private UserId userId;
...
}
@Entity
@DiscriminatorValue("Medical")
public abstract class UserMedical {
...
}
@Entity
@DiscriminatorValue("Medical")
public abstract class UserManagement {
...
}
However, I have an error when Hibernate tries to validate the database: column repeated (DEPARTMENT_CODE) and a suggestion to make the ID not insertable nor updatable.
But the ID must be provided for the user, so it can't be generated by the database.
Has anyone any suggestion to solve this?