(Hibernate) 如何使用列表的列表语义?
(java1.6,hibernate,mySql)
我试图保留一个包含字符串列表的java类。问题是,当我获取它时,我得到一个 PersistentBag 而不是 List 或 PersistentList。我寻找答案或例子,但我只是变得更加困惑。
我有一个我使用的小测试用例:
@Test
public void testFind() {
FooEntity expected = createFoo();
FooEntity actual = dao.find(expected.getId());
assertEquals(expected, actual);
assertEquals(actual, expected);
}
问题可以看作是第一个assertEquals 工作而第二个,
(assertEquals(实际,预期);),失败。发生这种情况是因为 List 是作为 PersistentBag 检索的。
那么,你知道这里出了什么问题吗?你能帮我吗?
这是我的代码:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EXAMPLE4_FOO")
public class FooEntity {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "LIST")
@ElementCollection(fetch = FetchType.EAGER)
private List<String> strings = new ArrayList<String>();
public FooEntity() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<String> getStrings() {
return strings;
}
public void setStrings(ArrayList<String> strings) {
this.strings = strings;
}
/*
equals() and hashCode() ....
*/
}
(java1.6, hibernate, mySql)
i'm trying to persist a java class that contains a list of strings. the problem is that when i fetch it i get a PersistentBag instead of a List or a PersistentList. i looked for an answer or example but i only got more confused.
i have a small test case that i use:
@Test
public void testFind() {
FooEntity expected = createFoo();
FooEntity actual = dao.find(expected.getId());
assertEquals(expected, actual);
assertEquals(actual, expected);
}
the problem can be seen as the first assertEquals works while the second one,
(assertEquals(actual, expected);), fails. it happens since the List is retrieved as a PersistentBag.
so, do you know whats wrong here? can you help me on that?
here is my code:
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "EXAMPLE4_FOO")
public class FooEntity {
@Id
@GeneratedValue
@Column(name = "ID")
private int id;
@Column(name = "LIST")
@ElementCollection(fetch = FetchType.EAGER)
private List<String> strings = new ArrayList<String>();
public FooEntity() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public List<String> getStrings() {
return strings;
}
public void setStrings(ArrayList<String> strings) {
this.strings = strings;
}
/*
equals() and hashCode() ....
*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了拥有一个列表,Hibernate 需要知道如何对列表的元素进行排序或索引。使用@OrderColumn 注释或@OrderBy 注释。请参阅 http://docs.jboss.org /hibernate/core/3.6/reference/en-US/html_single/#collections-indexed 了解这些注释之间的详细信息和差异。
如果您无法对元素进行排序或索引,那么您拥有的是一个包,而不是一个列表。应该修复您的 equals 方法,以避免考虑元素的顺序。
To have a List, Hibernate needs to know how to order or index elements of the list. Use the @OrderColumn annotation or the @OrderBy annotation. See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#collections-indexed for details and differences between those annotations.
If you can't order or index your elements, then you have a bag, not a list. And it's your equals method that should be fixed to avoid taking the order of the elements into consideration.