views:

97

answers:

1

If I have a mapping like this:

<class name="Users" table="users">
    <id column="id" name="id">
        <generator class="native"/>
    </id>
    ...
    <set name="types" table="types" cascade="all">
        <key column="user_id" />
        <element column="type_name" type="string" />
    </set>
</class>

How should the user object be created? I did this:

User u = new User();
u.getType().add(new Type(type_name));
getHibernateTemplate().save(u);

But there will be the error java.lang.ClassCastException: Type. The Type class only has an integer user_id and string type_name in it with get/set.

Why doesn't it work? Where can I find documentation on saving objects with collection of elements? Thanks you so much.

+1  A: 

Have a look at http://docs.jboss.org/hibernate/stable/core/reference/en/html/collections.html.

Change the element to:

<element column="type_name" type="Type" />

Then you can add types to the set. Right now you have it defined as String.

stevedbrown
Cool. Thanks! Saw that doc, but was still unsure about how objects are created...
April
Did I answer your question?
stevedbrown