0

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. For that reason, I had to add fetch=EAGER to my Collections. 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;
    }

}

2 Answers2

0

JPA annotations are parsed not to allow more than 2 eagerly loaded collection.

See Hibernate cannot simultaneously fetch multiple bags

So, I suspect that you cannot have two @OneToMany annotations in the same class with fetch = EAGER.

Community
  • 1
  • 1
chackley
  • 61
  • 1
  • 1
  • So I just have to remove the fetch on the 'User' in my 'Sign'? –  Jan 28 '14 at 23:16
  • I believe you have to remove either the `FetchType.EAGER` on `signs`, or the one on `videos`, yes. – chackley Feb 04 '14 at 21:00
  • According to [this answer](http://stackoverflow.com/a/4335514/2631700), it's also possible that newer (JPA 2.0-compliant) versions of Hibernate may not have this limitation. – chackley Feb 04 '14 at 21:01
0

Change private List<DayVideo> videos in private Set<DayVideo> videos

ovi
  • 11
  • 3