-->
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: initialize natural id from surrogate key
PostPosted: Fri Oct 15, 2010 4:33 pm 
Newbie

Joined: Fri Oct 15, 2010 4:10 pm
Posts: 4
I've been looking through discussions on natural v surrogate key. I'm not asking about that. I'm using both.

However, I need to generate the natural id, not assign it. The surrogate key is already auto-generated and only one field can be auto-generated. I would like to initialize the natural id with the value of the surrogate key.

So, for annotation:
Code:
public class customer {
  ...

  @Id
  @GeneratedValue
  private Integer id;

  @Column(name="customer_id")
  private Integer customerId;

  ..
}


I am currently doing this for every time I create a new entity:

Code:
  Customer c = new Customer();
  em.persist(c);
  c.setCustomerId(c.getId());
  em.persist(c);


I'm not so concerned about the performance. I'm concerned I may introduce a bug by forgetting to init the natural id of some of my entities. Here's hoping there is a underside API to hibernate that lets me omit the two last lines above.

Thanks,
Regards,
John


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.