使用Hibernate映射ArrayList [英] Map ArrayList with Hibernate
问题描述
我只是编写了我的第一个Hibernate的例子。
I just coded my first Hibernate examples.
数据库连接工作,我明白我如何映射一个字符串从POJO到数据库字段:
The database connection works and I understand how I can map a String from a POJO to a database field:
private String firstName;
并在映射文件中:
<property name="firstName" type="java.lang.String">
<column name="FIRSTNAME" />
</property>
但是如何将ArrayList映射到数据库?
But how can I map an ArrayList to the database? A simpl example from the mapping xml file would be appreciated.
干杯
UPDATE
我切换到List而不是ArrayList发现了一个例子。现在我映射如下:
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])
干杯
推荐答案
我注意到您正在使用XML你的POJO。您将在此处找到一些信息。
I notice that you are using XML to map your POJOs. You will find some information about that here.
例如:
<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.
这篇关于使用Hibernate映射ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!