gpt4 book ai didi

java - 在 Hibernate 中持久化 LinkedList

转载 作者:行者123 更新时间:2023-12-02 07:32:18 25 4
gpt4 key购买 nike

我试图保留一个具有 LinkedList 属性的类,但似乎无法正确执行。这是我的代码和映射:

 import java.util.LinkedList;
public class Stuff implements java.io.Serializable {
private long id;
private LinkedList<Image> images;


public Stuff() {
}


public Stuff(long Id) {
id = Id;
}

public long getId() {
return id;
}

public void setId(long mealId) {
id = mealId;
}


public LinkedList<Image> getNumberImages(int number) {
assert (number >= 0);
return (LinkedList<Image>) images.subList(0, number) ;
}

public LinkedList<Image> getImages() {
return images;
}
public LinkedList<Image> setImages(LinkedList<Image> images) {
this.images = images;
}


public void addImage(Image image) {
if (!images.contains(image)) {
images.add(image);
}
}

hibernate 映射:

<hibernate-mapping>
<class name="data.Stuff" table="Stuff">
<id name="id" type="long" access="field">
<column name="ID" />
<generator class="assigned" />
</id>
<list name="images" inverse="false" table="IMAGE" lazy="true" access="field">
<key>
<column name="ID" />
</key>
<list-index></list-index>
<one-to-many class="data.Image" />
</list>
</class>
</hibernate-mapping>

似乎我可以像这样持久保存类内容的对象,但是当我尝试恢复它们时,会发生以下错误:

Hibernate: select stuff0_.ID as ID0_, stuff0_.NAME as NAME0_, meal0_.GROUPING as GROUPING0_ from MEAL meal0_
org.hibernate.PropertyAccessException: could not set a field value by reflection setter of data.Meal.images

最佳答案

通常,Hibernate 将为集合提供自己的实现,因此您应该更喜欢接口(interface)而不是特定的实现。它可能试图为图像分配不同类型的列表,但失败了。您必须将字段更改为 List<Image> .

关于java - 在 Hibernate 中持久化 LinkedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8971010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com