Hi,
I'm having trouble putting new entries into a map collection. Getting the map and changing existing entries work fine.
When persisting the map, new entries are inserted without the primary key. Ie. this is what happens:
insert into child (parentid,name,value) values (1,"test","testvalue")
when what should happen is something like this:
insert into child (childid,parentid,name,value) values (child_seq.nextval,1,"test","testvalue")
I could solve this by letting the db add the id itself, but this seems like a pretty common scenario so I guess there must be something wrong with my mapping.
All <map> examples I've come across seem to rely on the child table using PRIMARY KEY(parentid,name), yet this seems to be discouraged as "legacy" db design in all the HIbernate docs I've come across as it would require the mapping for the child to use <composite-id>.
Here are the mappings I use:
Code:
<class name="Parent" table="parent" lazy="false">
<id name="id" column="parentid">
<generator class="sequence">
<param name="sequence">parent_seq</param>
</generator>
</id>
<map name="children" table="child" inverse="false">
<key column="parentid"/>
<index column="name" type="string"/>
<element column="value" type="string"/>
</map>
</class>
<class name="Child" table="child" lazy="false">
<id name="id" column="childid">
<generator class="sequence">
<param name="sequence">child_seq</param>
</generator>
</id>
<many-to-one name="parent" class="Parent" column="parentid" not-null="true"/>
<property name="ame" column="name"/>
<property name="value" column="value"/>
</class>
Java code:
Code:
map = parent.getChildren();
map.put("new value","test");
parent.setChildren(map);
session.save(parent);
...