-->
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.  [ 4 posts ] 
Author Message
 Post subject: Persist an entity with foreign key
PostPosted: Wed Nov 19, 2008 8:15 pm 
Newbie

Joined: Thu Jan 04, 2007 6:51 pm
Posts: 6
Hi,
I am a newbie
I am creating a POJO from a flat file which has a foreign-key relation with an existing table. I want to persist this entity into the database.

Example:

NewEntity

@Id
int id;
String name;
@JoinColumn(name="foreign_id", nullable=false)
AnotherObject object;

The flat file has a value for 'foreign_id' and if I want to populate the field, then I need to create an instance of AnotherObject to set its attribute from the flat file.
My question is when I call EM.persist(NewEntity) will the hibernate not try to persist AnotherObject? If so, How can I avoid it?


Top
 Profile  
 
 Post subject: Re: Persist an entity with foreign key
PostPosted: Thu Nov 20, 2008 1:23 am 
Newbie

Joined: Thu Nov 20, 2008 1:08 am
Posts: 9
Hi Pat,

patb23 wrote:
I am creating a POJO from a flat file which has a foreign-key relation with an existing table. I want to persist this entity into the database.

Example:

NewEntity

@Id
int id;
String name;
@JoinColumn(name="foreign_id", nullable=false)
AnotherObject object;

The flat file has a value for 'foreign_id' and if I want to populate the field, then I need to create an instance of AnotherObject to set its attribute from the flat file.
My question is when I call EM.persist(NewEntity) will the hibernate not try to persist AnotherObject? If so, How can I avoid it?


I do this:

Code:
  NewEntity ne = new NewEntity();
  ne.setName("New Entity Name");

  AnotherObject ao = new AnotherObject();
  ao.setId("12345"); // the FK I want to populate into my NewEntity

  ne.setObject(ao);


Then I save/persist/merge "ne" in the usual way. Note, don't try and set any other property of your foreign object (AnotherObject) except its primary key field and Hibernate won't try and persist it to the DB.

HTH,

Rob Hills[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 25, 2008 3:20 pm 
Newbie

Joined: Thu Jan 04, 2007 6:51 pm
Posts: 6
Thanks. It worked in all but one case.

I have entities called Policy, Producer, Branch and UserPolicyType
of these, Policy and Producer are inserted into the table whereas Branch and UserPolicyType are masters.

I notice that a Producer is being populated for each Policy and hence getting duplicate rows of Producer.
I am placing the snippet below.
Request help to resolve the problem.
Thanks
Code:
@Entity
@Table(name="CSENTRYPOLICY")
public class CSentryPolicy implements Serializable{

   @Id @GeneratedValue (strategy=GenerationType.SEQUENCE , generator="POLICY_SEQ" ) //omitted rest for clarity
   int iPolicyID;

   @ManyToOne(//omitted rest for clarity)
   @JoinColumn(name="SPRODUCERNBR", referencedColumnName="SPRODUCERNBR",nullable=false)
   CSentryProducer producer;
   
   @OneToOne(//omitted rest for clarity)
   @JoinColumn(name="IADDRESSID", nullable=false)
   CSentryAddress address;

   @ManyToOne(//omitted rest for clarity)
   @JoinColumns({
      @JoinColumn(name="CATNBR", referencedColumnName="IUSERPOLICYTYPEID"),
      @JoinColumn(name="POTYPE", referencedColumnName="SPOLICYPRINTTITLE" )
   })
   CSentryUserPolicyType policyType;

.
.
.
@Table(name="CSENTRYPRODUCER", uniqueConstraints={@UniqueConstraint(columnNames={"SPRODUCERNBR"})})
public class CSentryProducer implements Serializable{

   @Id
   @GeneratedValue(//omitted rest for clarity)
   int iProducerID;
   
   @OneToOne(cascade=CascadeType.ALL)
   @JoinColumn(name="IADDRESSID")
   CSentryAddress address;


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 26, 2008 9:49 am 
Newbie

Joined: Thu Jan 04, 2007 6:51 pm
Posts: 6
Hi,
I found the reason. I am getting this error whenever the tables are mapped by non-primary key field.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.