java 使用 Hibernate 映射 ArrayList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4324857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Map ArrayList with Hibernate
提问by Martin Preusse
I just coded my first Hibernate examples.
我刚刚编写了我的第一个 Hibernate 示例。
The database connection works and I understand how I can map a String from a POJO to a database field:
数据库连接有效,我了解如何将字符串从 POJO 映射到数据库字段:
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.
但是如何将 ArrayList 映射到数据库?来自映射 xml 文件的简单示例将不胜感激。
Cheers
干杯
UPDATE
更新
I switched to List instead of ArrayList found an example. Now I map as follows:
我切换到 List 而不是 ArrayList 找到了一个例子。现在我映射如下:
<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
干杯
回答by Vincent Ramdhanie
I notice that you are using XML to map your POJOs. You will find some information about that here.
我注意到您正在使用 XML 来映射您的 POJO。您将在此处找到一些相关信息。
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 linkwill explain how to map any collection using annotations.
但是,使用注释有一些优点。此链接将解释如何使用注释映射任何集合。
回答by Alberto Vazquez
You have a little error in the XML configuration:
您在 XML 配置中有一个小错误:
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.
Hibernate 会自动为您创建新表。
回答by Bozho
See the collection mappingsection 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.
请参阅文档的集合映射部分。有多种方法可以映射列表(一对多、多对多、元素集合)。您可以将其映射为列表或包,因此请阅读整个部分。