I am trying to get hibernate to write new objects each time I do a save as opposed to updating the existing records.
public class Risk {
private long riskID;
private Vehicle vehicle;
}
public class Vehicle {
private long vehicleID;
private long riskID;
private String regNumber;
private String abiCode;
private String make;
private String model;
}
So if a write the risk to the DB. Then I change vehicle on the web and try to save the risk to the DB a second time. I would like to to have two risks in the risk table and two vehicles in the vehicle table.
Currently I am using the hibernate session Save(Object o). This is always creating a new risk in the DB but never creating a new vehicle. It is just updating the original one.
Here is my mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class lazy="false" name="uk.co.test.Risk" schema="quote_engine" table="GV_RISK" >
<id column="riskID" name="riskID" type="long">
<generator class="identity"/>
</id>
<many-to-one name="vehicle" class="uk.co.test.Vehicle" column="vehicleID" not-null="true" cascade="all" unique="true" />
</class>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="uk.co.test.Vehicle" schema="quote_engine" table="GV_VEHICLE">
<id name="vehicleID" type="long" column="vehicleID">
<generator class="identity" />
</id>
<property name="regNumber" type="string" column="regNumber" />
<property name="abiCode" type="string" column="abiCode" />
<property name="make" type="string" column="make" />
<property name="model" type="string" column="model" />
</class>
I think that the simpler way of doing that should be:
It’s very important that you close the Hibernate session (use separated session for each save) so that the objects become detached, and when the objects become re attached Hibernates thinks that they are brand new.
Note: you should change long for Long (the primitive to the wrapper)