简体   繁体   English

启动应用程序时出现MultipleBagFetch

[英]MultipleBagFetch when starting application

I am getting the following error on my application: 我的应用程序出现以下错误:

SEVERE: Exception sending context initialized event to listener instance of class com.puntobile.videre.util.VidereContextListener
javax.persistence.PersistenceException: [PersistenceUnit: Videre] Unable to build EntityManagerFactory

This happened when I changed my DAO and closed each EntityManager session when it was used, which I was not doing before, and is a bad practise. 这是在我更改DAO并在使用它时关闭每个EntityManager会话时发生的,这是我以前没有做过的,这是一种不好的做法。 For that reason, I had to add fetch=EAGER to my Collections. 因此,我必须将fetch = EAGER添加到我的收藏夹中。 Here are my three entities: 这是我的三个实体:

package com.puntobile.videre.data;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.*;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
@SequenceGenerator(name="user_seq", sequenceName="user_seq", allocationSize=1, initialValue=1)
@Table(name="users")
public class User {

    @Id
    @GeneratedValue(generator="user_seq", strategy=GenerationType.SEQUENCE)
    private Long id;
    @Column(name="userName")
    private String userName;
    @Column(name="password")
    private String password;
    @Column(name="accessLevel")
    private int accessLevel;

    @OneToMany(mappedBy="user", fetch=FetchType.EAGER)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<Sign> signs;

    public User() {}

    public User(String userName, String password, int accessLevel) {
        this.userName = userName;
        this.password = password;
        this.accessLevel = accessLevel;
        this.signs = new ArrayList<Sign>();
    }

    public Long getId() {
        return id;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAccessLevel() {
        return accessLevel;
    }

    public void setAccessLevel(int accessLevel) {
        this.accessLevel = accessLevel;
    }

    public void setSigns(List<Sign> signs) {
        this.signs = signs;
    }

    public List<Sign> getSigns() {
        return signs;
    }   
}

Another one: 另一个:

package com.puntobile.videre.data;

import java.util.List;

import javax.persistence.*;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;

@Entity
@SequenceGenerator(name="sign_seq", sequenceName="sign_seq", allocationSize=1, initialValue=1)
public class Sign {
    @Id
    @GeneratedValue(generator="sign_seq", strategy=GenerationType.SEQUENCE)
    private Long id;
    @Column(name="signName")
    private String name;
    @Column(name="url")
    private String url;
    @Column(name="description")
    private String description;
    @Column(name="lastOnline", nullable=false)
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    private DateTime lastOnline;

    @ManyToOne(fetch=FetchType.EAGER)
    private User user;

    @OneToMany(mappedBy="sign", fetch=FetchType.EAGER)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<DayVideo> videos;

    public Sign() {}

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<DayVideo> getVideos() {
        return videos;
    }

    public void setVideos(List<DayVideo> videos) {
        this.videos = videos;
    }

    public DateTime getLastOnline() {
        return lastOnline;
    }

    public void setLastOnline(DateTime lastOnline) {
        this.lastOnline = lastOnline;
    }


}

And the last one: 最后一个:

package com.puntobile.videre.data;

import javax.persistence.*;

import org.hibernate.annotations.Type;
import org.joda.time.LocalDate;

@Entity
@SequenceGenerator(name="dayvideo_seq", sequenceName="dayvideo_seq", allocationSize=1, initialValue=1)
public class DayVideo { 

    @Id
    @GeneratedValue(generator="dayvideo_seq", strategy=GenerationType.SEQUENCE)
    private Long id;
    @Column(name="startDate", nullable=false)
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    LocalDate startDate;
    @Column(name="endDate", nullable=false)
    @Type(type="org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    LocalDate endDate;
    @Column(name="videoName")
    String videoName;
    @ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name="sign_id")
    Sign sign;

    public DayVideo() {}

    public Long getId() {
        return id;
    }

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

    public LocalDate getStartDate() {
        return startDate;
    }

    public void setStartDate(LocalDate startDate) {
        this.startDate = startDate;
    }

    public LocalDate getEndDate() {
        return endDate;
    }

    public void setEndDate(LocalDate endDate) {
        this.endDate = endDate;
    }

    public String getVideoName() {
        return videoName;
    }

    public void setVideoName(String videoName) {
        this.videoName = videoName;
    }

    public Sign getSign() {
        return sign;
    }

    public void setSign(Sign sign) {
        this.sign = sign;
    }

}

JPA annotations are parsed not to allow more than 2 eagerly loaded collection. 解析JPA批注时,不允许超过2个急切加载的集合。

See Hibernate cannot simultaneously fetch multiple bags 请参见Hibernate无法同时获取多个包

So, I suspect that you cannot have two @OneToMany annotations in the same class with fetch = EAGER. 因此,我怀疑您不能在同一类中通过fetch = EAGER使用两个@OneToMany批注。

private Set<DayVideo> videos更改private List<DayVideo> videos private Set<DayVideo> videos

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM