1

I just coded my first Hibernate examples.

The database connection works and I understand how I can map a String from a POJO to a database field:

private String firstName;

And in the mapping file:

<property name="firstName" type="java.lang.String">
    <column name="FIRSTNAME" />
</property>

But how can I map an ArrayList to the database? A simpl example from the mapping xml file would be appreciated.

Cheers

UPDATE

I switched to List instead of ArrayList found an example. Now I map as follows:

    <list name="test" inverse="false" table="CONTACT" lazy="true">
        <key>
            <column name="ID" />
        </key>
        <list-index></list-index>
        <element type="java.lang.String">
            <column name="TEST" />
        </element>
    </list>

Unfortunately, I get an exception that I do not understand:

Exception in thread "main" org.hibernate.MappingException: Foreign key (FK6382B0003257FF7F:CONTACT [ID])) must have same number of columns as the referenced primary key (CONTACT [ID,idx])

Any ideas?

Cheers

3 Answers 3

6

I notice that you are using XML to map your POJOs. You will find some information about that here.

for example:

   <list name="myArrayListProperty" cascade="all">
        <key column="parent_id"/>
        <index column="idx"/>
        <one-to-many class="WhatIsInTheList"/>
    </list>

However, using annotations have some advantages. This link will explain how to map any collection using annotations.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I didn't know about annotations but it seems to be easier.
1

See the collection mapping section of the docs. There are multiple ways to map a list (one-to-many, many-to-many, a collection of elements). You can map it as a list or as a bag, so read the whole section.

1 Comment

Thanks for your help, but this kind of answer is not very helpful for somebody who is new to a certain technology. Everything is in the docs, but usually you don't understand it. I don't know how you star tlearning something new, but I put together small 'Hello World' examples. An actual code fragment would have been much more useful.
1

You have a little error in the XML configuration:

When you have a list the solution to map this list using a database is to link with a additional table, so instead of doing:

<list name="test" inverse="false" table="CONTACT" lazy="true">
        <key>
            <column name="ID" />
        </key>
        <list-index></list-index>
        <element type="java.lang.String">
            <column name="TEST" />
        </element>
</list>

You should have to do map to a new data table that holds the list values:

<list name="test" inverse="false" table="CONTACT_test" lazy="true">
        <key>
            <column name="ID" />
        </key>
        <list-index></list-index>
        <element type="java.lang.String">
            <column name="TEST" />
        </element>
</list>

Hibernate automatically creates the new table for you.

Comments

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.