3

I'm trying to persist an Object which have a set of Strings to database, I found that I must use @ElementCollection to do that, but since I'm using .hbm.xml configuration files through all my project I want to do it using xml . this question Hibernate, List<String> shows how to do it through annotations, and this link http://en.wikibooks.org/wiki/Java_Persistence/ElementCollection provides tips to do it using xml. But when I tried to use <element-collection> my eclipse IDE didn't accept it and gave me an error at the element <class/> which says the contents of element class must match ...

my class is simply like this

public class Role {
private Long id;
private Integer version;
private String name;
private Set<String> menuItems;
/** getters and setters **/

and my Role.hbm.xml is like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.element.collection.beans">
    <class name="Role" table="Role">
    <id name="id" type="java.lang.Long" column="id">
        <generator class="org.hibernate.id.TableHiLoGenerator">
            <param name="table">HibernateUniqueKey</param>
            <param name="column">NexHiValue</param>
        </generator>
    </id>
<version name="version" column="Version" />
<property name="name" column="name" type="java.lang.String"
        not-null="true" length="128" />

 <element-collection name="menuItems">
<collection-table>menuItems</collection-table>
</element-collection>

</class>

update:

here is the last mapping of the set, the rest is not changed

    <set name="menuItems" sort="unsorted" table="menuItems" lazy="false">
        <key column="itemId" />
        <element column="itemName" type="string" />
    </set>

1 Answer 1

3

Check the doc:

And the example from that source:

<set name="aliases"
            table="person_aliases" 
            sort="natural">
    <key column="person"/>
    <element column="name" type="string"/>
</set>

So the answer is <element>

This <element> could be used in any type of colleciton mapping, like <bag> <set> <list>...

Also could be interesting: Understading the restrictions for collection of dependent objects in hibernate ...

Based on more details, we could say, that the mapping here would be like:

<class name="Role" table="Role">
    ...

    <set name="menuItems" sort="unsorted" table="menuItems" lazy="false">
        <key column="menuId" /> // not itemId
        <element column="itemName" type="string" />
    </set>
</class>
Sign up to request clarification or add additional context in comments.

8 Comments

I get an Exception when trying to save a role cannot insert null into menuItems.menuId
Could you show me the latest mapping? We will find it I guess ;) Just show me what you have (extend the answer I'd say)
I would say I got it! Look Exception says: cannot insert null into menuItems.menuId but the mapping is <key column="itemId" /> and it must be <key column="menuId" />
sorry but I just wrote it wrong, the Exception says: org.hibernate.exception.ConstraintViolationException: ORA-01400: cannot insert NULL into ("HUB"."MENUITEMS"."ITEMID") and I'm using <column = "itemId"/>
That is really weird. Look, in case Hibernate is mapped properly, this simple scenario is working always. So, check my answer and doc, compare it with what you really do have (DB vs mapping). This is very easy scenario... (having many of these mappings)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.