-->
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.  [ 1 post ] 
Author Message
 Post subject: Composite-key and @GeneratedValue once again.
PostPosted: Sat Oct 06, 2012 3:05 pm 
Newbie

Joined: Sat Oct 06, 2012 2:16 pm
Posts: 1
I need to have a composite ID in my entity, consisting of two integers that one must be generated. I have been exploring google for several days and I couldn't find anything constructive about composite keys and generating value. I cannot fix my model in order not to use composite key. My database sometimes imports "Client" records from another database so that is the only way to ensure uniqueness.


This is how my entity class looks like:

Code:
@Entity
@Table(name = "Klienci")
public class Client implements Serializable{

   private static final long serialVersionUID = 1L;
   private Integer id;
   private Salon salon;
   private Long pesel;

   public Client() {
   }

   public Client(Long pesel, Salon salon) {
      this.pesel = pesel;
      this.salon = salon;
   }

   @Id
   @Column(name = "Klient_ID")   
   public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }
   @Id
   @ManyToOne
        @JoinColumn(name = "Salon_fk")
   public Salon getSalon() {
      return salon;
   }


   public void setSalon(Salon salon) {
      this.salon = salon;
   }

   @Column(name = "PESEL")
   public Long getPesel() {
      return pesel;
   }

   public void setPesel(Long pesel) {
      this.pesel = pesel;
   }


When I call save(client) I get following exception:
Code:
Exception in thread "AWT-EventQueue-0" org.springframework.orm.hibernate3.HibernateSystemException: IllegalArgumentException occurred while calling setter of mbrnwsk.ClientsRegister.model.client.Client.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of mbrnwsk.ClientsRegister.model.client.Client.id


When I use the @EmbeddedId approach instead of multi @Id annotations I don't get this exception and object is persisted correctly, but generated id is not being set so I cannot use this object to persist another object that is parent of client.
Maybe should I write custom generator that returns composite-id instead of single value? But I have no idea how to do it.

I would be very grateful for solution as I'm stuck with this problem for several days.


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

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.