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
|