-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: JPA/Hiber, legacy DB: composite keys and discriminator colum
PostPosted: Sun May 01, 2011 6:47 pm 
Newbie

Joined: Fri Apr 29, 2011 5:30 pm
Posts: 5
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   ghouse@hospital.com
ghouse      Management            Gregory House   ghouse@hospital.com
lcuddy       Management            Lisa Cuddy         lcuddy@hospital.com


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?


Top
 Profile  
 
 Post subject: Re: JPA/Hiber, legacy DB: composite keys and discriminator colum
PostPosted: Fri May 27, 2011 4:32 am 
Newbie

Joined: Fri Apr 29, 2011 5:30 pm
Posts: 5
Solved.

I used the annotation org.hibernate.annotations.DiscriminatorOptions.

I don't remember if I use the "force" or the "insert" attribute (or both) because I had to change the design.

But I know that it worked with this annotation correctly configured.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.