小编bab*_*ith的帖子

(Hibernate)如何使用List <String>的列表语义?

(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);
}
Run Code Online (Sandbox Code Playgroud)

问题可以看作第一个assertEquals工作,而第二个
(assertEquals(actual,expected);)失败.它发生,因为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 …
Run Code Online (Sandbox Code Playgroud)

hibernate list persistent bag semantics

1
推荐指数
1
解决办法
3097
查看次数

标签 统计

bag ×1

hibernate ×1

list ×1

persistent ×1

semantics ×1