-->
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: Inserting value in FK field
PostPosted: Thu Jun 23, 2005 10:46 am 
Newbie

Joined: Wed Mar 23, 2005 2:32 pm
Posts: 12
I have the following table "Product" that has a FK 'vendor_cd' referencing a lookup table of vendors. Assuming the existence of vendor code '1' => 'Acme Company' , I wish to accomplish the following in sql: insert into product(code,name,vendor_cd) values(101,'Phone',1). I don't wish to create/update the vendor table because the entry '1' already exists.

My current mapping for 'Product' is as follows.
<hibernate-mapping>
<class name="com.acme.Product" table="Product">

<id name="Code" type="java.lang.Long">
<column name="CODE" length="19" not-null="true" unique="true" sql-type="BIGINT" />
</id>

<property name="Name" type="java.lang.String">
<column name="NAME" length="30" not-null="false" sql-type="VARCHAR" />
</property>

<many-to-one name="Vendor" class="com.acme.Vendor">
<column name="VENDOR_CD" length="19" not-null="false" />
</many-to-one>
</class>
</hibernate-mapping>

The code:
Vendor vendor = new Vendor();
vendor.setCode(new Long(1));

Product product = new Product();
product.setCode(new Long(101));
product.setName("Phone");
product.setVendor(vendor); <== assign vendor to this product
dao.create(product);

However, I get the following error. org.springframework.dao.InvalidDataAccessApiUsageException:
object references an unsaved transient instance - save the transient instance before flushing:
As mentioned earlier I don't wish to create/update the exiting vendor entry.
Any ideas/suggestions would be appreciated. TIA


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 23, 2005 2:09 pm 
Regular
Regular

Joined: Thu Apr 21, 2005 9:05 am
Posts: 50
Location: Boston, U.S
You must load the vedor object from db and set it on the product
then it will work fine. This should be done within the context of
a session.

Vendor v = session.load(.........)
Product p = new Product();
p.setxxx
p.setVendor(v)

session.save(p);


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.