Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在hibernate中处理大型集合_Java_Hibernate - Fatal编程技术网

Java 如何在hibernate中处理大型集合

Java 如何在hibernate中处理大型集合,java,hibernate,Java,Hibernate,我有一个条目实体和一个注释实体。条目实体与注释实体具有OneToMany关联: @Entity @Table(name="entries") public class Entry { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="id") private int id; @OneToMany(mappedBy = "entry", cascade = Casca

我有一个条目实体和一个注释实体。条目实体与注释实体具有OneToMany关联:

@Entity
@Table(name="entries")
public class Entry {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="id")
    private int id;

    @OneToMany(mappedBy = "entry", cascade = CascadeType.ALL, orphanRemoval = true)
    @LazyCollection(LazyCollectionOption.EXTRA)
    private List<Comment> comments = new ArrayList<>();

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Comment> getComments() {
        return comments;
    }

    public boolean containsComment(int commentId) {
        return comments.contains(commentId);
    }

    public Comment getComment(int commentId) {
        return comments.get(commentId);
    }

    public void removeComment(Comment comment) {
       comments.remove(comment);
    }
}


@Entity
@Table(name="comments")
public class Comment {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    public int id;

    @ManyToOne
    private Entry entry;

    public int getId() {
        return this.id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Entry getEntry() {
        return entry;
    }

    public void setEntry(Entry entry) {
        this.entry = entry;
    }
}
我的方法是在控制器中通过entryId参数获取条目。 然后我想调用条目实体containsComment()-方法,在其中传递commentId请求参数。因此,如果集合包含具有给定id的注释,则为true,否则为false。如果为true,我将调用getComment()-方法来获取注释实体。 最后,我只需以注释实体作为参数调用removeComment()-方法

在PHP理论中,我认为这种方法是有效的。这在hibernate中也是正确的方法吗

在示例代码中,如果调用containsComment()方法,则会出现以下异常:

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public int com.mypackage.Comment.id] by reflection for persistent property [com.mypackage.Comment#id] : 7

请注意,我使用的是EXTRA-LAZY,因为注释可以很快得到一个大集合。

您可以通过查询来实现这一点,例如,如果
Comment
引用了
Entry
使用类似
的方法从注释c中选择c,其中c.id=:commentId和c.Entry.id=:entryId
-如果你得到了该注释,那么它就存在了,你可以立即使用它

删除评论很容易,就这么做吧。由于
Comment
是关系的所有者,因此删除它也将有效地将其从条目中删除

不过有一点建议:HQL/JPQL查询使用关键字
id
作为id属性。在您的情况下,这没关系,因为
id
是用
@id
注释的。但是,如果您有另一个
@Id
字段,则查询的行为将与您预期的不同,因为在查询中使用
Id
将假定您指的是
@Id
字段

最后是关于
comments.contains(commentId)
的旁注:类似列表的Java集合通常会查找作为参数传递的元素,但由于您的集合是
列表
,并且您传递了一个id(即
int
),因此不应该找到它。我不确定Hibernate集合是否使用了一些特殊的转换逻辑,但是对于Java程序员来说,调用看起来像是一个错误,因为您永远不会期望它返回true(就像“苹果列表中是否包含这个橙色?”)

org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public int com.mypackage.Comment.id] by reflection for persistent property [com.mypackage.Comment#id] : 7