Hi,
I am trying to use cascade insert but even I checked the
FAQ site and followed the alternatives but out of luck
The problem that I have is the foreign key is altered after the row is created.
So I was wondering if you could look the following and point out what I miss.
Thanks in advance,
Hibernate version:2.1.8 (package as EJB using Weblogic 8.1)
Name and version of the database: MySQL 4.1.10
Mapping documents:
<hibernate-mapping package="com.prohorenko.example.persistence" default-access="property"
auto-import="true" default-cascade="none" >
<class name="Entity" table="entity"
mutable="true" polymorphism="implicit"
dynamic-update="false" dynamic-insert="false" select-before-update="false"
optimistic-lock="version">
<id name="id" column="entity_id" type="long" unsaved-value="0">
<generator class="native" />
</id>
<property name="hostName" column="hostName" type="string" length="50"/>
<!-- Cardinality Relationship -->
<set
name="snmpAgents"
inverse="true"
cascade="all"
lazy="true"
sort="unsorted"
outer-join="auto">
<key column="entity_id" />
<one-to-many class="SnmpAgent" />
</set>
</class>
<class name="SnmpAgent" table="snmpAgent" mutable="true" polymorphism="implicit"
dynamic-update="false" dynamic-insert="false" select-before-update="false"
optimistic-lock="version" >
<id name="id" column="ID" type="long" unsaved-value="0">
<generator class="native" />
</id>
<property name="snmpIpAddr" column="snmpIpAddr" type="string" length="32"/>
<many-to-one name="entity"
class="Entity"
column="entity_id"
cascade="none"
not-null="false"
outer-join="auto"
update="true" insert="true"
/>
<set
name="snmpAuths"
inverse="true"
cascade="all"
lazy="true"
sort="unsorted"
outer-join="auto">
<key column="snmpAgent_id" />
<one-to-many class="SnmpAuth" />
</set>
</class>
<class name="SnmpAuth" table="snmpAuth"
mutable="true" polymorphism="implicit"
dynamic-update="false" dynamic-insert="false" select-before-update="false"
optimistic-lock="version">
<id name="id" column="ID" type="long" unsaved-value="0">
<generator class="native" />
</id>
<property name="readComm" column="readComm" type="string" length="32"/>
<property name="writeComm" column="writeComm" type="string" length="32"/>
<many-to-one name="snmpAgent"
class="SnmpAgent"
column="snmpAgent_id"
cascade="none"
not-null="false"
outer-join="auto"
update="true" insert="true"
/>
</class>
</hibernate-mapping>
Codebetween sessionFactory.openSession() and session.close():
Transaction tx = session.beginTransaction();
Entity entity = new Entity(hostName);
SnmpAgent agent = new SnmpAgent(snmpIpAddr);
SnmpAuth auth = new SnmpAuth(readComm, writeComm);
agent.setEntity(entity);
entity.addSnmpAgent(agent);
agent.addSnmpAuth(auth);
auth.setSnmpAgent(agent);
session.saveOrUpdate(entity);
tx.commit();
Full stack trace of any exception that occurs:
WARNING: InitialContext did not implement EventContext
Mar 31, 2005 12:15:00 PM net.sf.hibernate.util.NamingHelper getInitialContext
INFO: JNDI InitialContext properties:{}
Hibernate: insert into entity (domain, hostName, dispName, nonSnmpIpAddr, modTim
e, modAccessStatus, modAccessTime) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into snmpAgent (snmpIpAddr, mibVer, readable, writable, notifi
able, agentType, entity_id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into snmpAuth (groupName, snmpVer, readComm, writeComm, userNa
me, authProtocol, authPwd, privPwd, snmpAgent_id) values (?, ?, ?, ?, ?, ?, ?, ?
, ?)
Error: net.sf.hibernate.HibernateException: identifier of an instance of com.prohorenko.example.persistence.SnmpAuth altered from 1 to 0
|