I have a very similar problem with many-to-many and JBoss.
Mappings:
Code:
<class name="com.db.device.NetPortalSubnetwork" table="subnet" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" proxy="com.db.device.NetPortalSubnetwork">
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>
<set name="mgmtInfo" table="ne_subnet" inverse="true" lazy="false" cascade="save-update">
<key column="subnet_id"/>
<many-to-many class="com.db.device.NEMgmtInfo" column="ne_subnet"/>
</set>
</class>
Code:
<class name="com.diatem.db.device.NEMgmtInfo" table="nemgmtinfo" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false">
<!--cache usage="read-write" /-->
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>
<set name="subnets" table="ne_subnet" lazy="true">
<key>
<column name="ne_subnet" not-null="true"/>
</key>
<many-to-many class="com.diatem.db.device.NetPortalSubnetwork">
<column name="subnet_id" not-null="true"/>
</many-to-many>
</set>
</class>
Code:
<class name="com.db.device.NetPortalNetwork" table="network" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" proxy="com.db.device.NetPortalNetwork">
<id name="id" type="long" unsaved-value="0">
<generator class="native">
</generator>
</id>
<set name="subnet" cascade="all-delete-orphan" inverse="true" lazy="true">
<key column="net_subnet_id"/>
<one-to-many class="com.db.device.NetPortalSubnetwork"/>
</set>
</class>
Then I try to update the network:
Code:
NetPortalNetwork network = helper.getNetworkByPK(2);
NEMgmtInfo device = helper.getDeviceByPK(46);
Set subnets = helper.getSubnetsForNe(device);
device.setSubnets(subnets);
System.out.println(device);
Set nes = new HashSet();
nes.add(device);
NetPortalSubnetwork subnet =
new NetPortalSubnetwork("test_"+new Date(), "", network, nes, null);
network.getSubnet().add(subnet);
Iterator iter = nes.iterator();
while(iter.hasNext()){
NEMgmtInfo ne = (NEMgmtInfo)iter.next();
ne.getSubnets().add(subnet);
ne.setNetwork(network);
}
network = helper.updateNetwork(network);
Now, the funny thing is that
sometimes a new record is inserted into the join table (ne_subnet)
sometimes not.
Note:
The
Code:
helper
class calls a JBoss session bean which in turn opens and closes a Hibernate session per request.
Please help.