-->
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: IdClass Annotation Help
PostPosted: Sat Sep 09, 2006 2:21 pm 
Newbie

Joined: Sat Sep 09, 2006 2:02 pm
Posts: 1
Hi,
I have a problem with the column names generated for the Entity class with a composite primary key. I have defined an EmployeePK class and used the IdClass annotation to map it to the Employee Entity. But the columns generated in the database for the @Id annotated fields are not as per the names specfied in the @Column annotation, but are being created with the bean property names of the Primary Key class.

...TABLE `employee` (
`lastName` varchar(255) NOT NULL,
`SSN` varchar(255) NOT NULL,
`FIRST_NAME` varchar(255) default NULL,
`LOCATION` varchar(255) default NULL,
PRIMARY KEY (`lastName`,`SSN`)
) ...

I am using JPA with hibernate 3.1.3 as my persitence provider and for MySQL with MySQLDialect. I want have to the column names for lastName as LAST_NAME and SSN as SOCIAL_SECURITY_NO in the database.

Can anyone here please help me overcome this problem. I have tried using the AttributeOverride, many examples seem to suggest that it can be used only with Embeddable Annotation, but I do need to have the basic bean properties within the entity class. So can anyone please help to overcome the problem without the embedding the Primary Key class in my Entity.

Thank You.

The following is my code.

Code:
public class EmployeePK implements Serializable {
   
   private String lastName;
   private String SSN;
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   public String getSSN() {
      return SSN;
   }
   public void setSSN(String ssn) {
      SSN = ssn;
   }

..............
}

@Entity
@Table(name = "EMPLOYEE", catalog = "cchs", uniqueConstraints = {})
@IdClass(EmployeePK.class)

public class Employee {
   
   private String firstName;
   private String lastName;
   private String SSN;
   private String location;
   
   
   @Column(name = "FIRST_NAME", unique = false, nullable = true, insertable = true, updatable = true)
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   
   @Id
   @Column(name = "LAST_NAME", unique = false, nullable = true, insertable = true, updatable = true)
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
               
@Id
   @Column(name = "SOCIAL_SECURITY_NO", unique = false, nullable = true, insertable = true, updatable = true)
   public String getSSN() {
      return SSN;
   }
   
   public void setSSN(String ssn) {
      SSN = ssn;
   }

              .....
}   


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 8:29 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hi

first off, you should put @Embeddable before your primary class

Code:

@AccessType("field")
@Embeddable
public class EmployeePK implements Serializable {

   @Column(name="last_name")
   private String lastName;

   @Column(name="ssn")
   private String SSN;
....   
}

@Entity
@Table(name = "EMPLOYEE", catalog = "cchs", uniqueConstraints = {})
@IdClass(EmployeePK.class)

public class Employee {
   
   @Id
   private String lastName;

   @Id
   private String SSN;



hope this help you


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.