-
Sep 16th, 2012, 12:16 PM
#1
failed to lazily initialize a collection of role: , no session or session was closed
Hello I am getting this problem with Hibernate. I am not sure what causing the issue. The query I am trying to run is this:
sessionFactory.getCurrentSession().createQuery("FR OM Goal").list()
Even when I tried changing the Domains' mapping of Weakness and WeaknessGoal to both EAGER (which I don't want)....my app crash because of infinite loops mapping from both Weakness and WeaknessGoal. I only want the option where WeaknessGoal eagerly fetches Weakness but, Weakness Lazily fetches WeaknessGoal (which is the case in the code below).
Hibernate: select goal0_.GOAL_ID as GOAL2_2_, goal0_.ACHIEVE_DATE as ACHIEVE3_2_, goal0_.DESCRIPTION as DESCRIPT4_2_, goal0_.goalType as goalType2_, goal0_.TITLE as TITLE2_, goal0_.id as id2_, goal0_.WEAKNESS_ID as WEAKNESS7_2_ from public.GOALS goal0_
Hibernate: select weakness0_.WEAKNESS_ID as WEAKNESS1_4_1_, weakness0_.CATEGORY_ID as CATEGORY4_4_1_, weakness0_.DATE_LOGGED as DATE2_4_1_, weakness0_.DESCRIPTION as DESCRIPT3_4_1_, weakness0_.id as id4_1_, category1_.CATEGORY_ID as CATEGORY1_1_0_, category1_.NAME as NAME1_0_ from public.WEAKNESS weakness0_ left outer join public.CATEGORY category1_ on weakness0_.CATEGORY_ID=category1_.CATEGORY_ID where weakness0_.WEAKNESS_ID=?
Hibernate: select goal0_.GOAL_ID as GOAL2_2_, goal0_.ACHIEVE_DATE as ACHIEVE3_2_, goal0_.DESCRIPTION as DESCRIPT4_2_, goal0_.goalType as goalType2_, goal0_.TITLE as TITLE2_, goal0_.id as id2_, goal0_.WEAKNESS_ID as WEAKNESS7_2_ from public.GOALS goal0_
Hibernate: select weakness0_.WEAKNESS_ID as WEAKNESS1_4_1_, weakness0_.CATEGORY_ID as CATEGORY4_4_1_, weakness0_.DATE_LOGGED as DATE2_4_1_, weakness0_.DESCRIPTION as DESCRIPT3_4_1_, weakness0_.id as id4_1_, category1_.CATEGORY_ID as CATEGORY1_1_0_, category1_.NAME as NAME1_0_ from public.WEAKNESS weakness0_ left outer join public.CATEGORY category1_ on weakness0_.CATEGORY_ID=category1_.CATEGORY_ID where weakness0_.WEAKNESS_ID=?
ERROR: org.hibernate.LazyInitializationException - failed to lazily initialize a collection of role: com.medullan.employeegrowthtracker.domain.Weakness .weaknessGoals, no session or session was closed
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.medullan.employeegrowthtracker.domain.Weakness .weaknessGoals, no session or session was closed
//Parent Class
@Entity
@Table(name = "GOALS")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE )
@DiscriminatorColumn(name="goalType", discriminatorType = DiscriminatorType.STRING)
public class Goal implements GoalInterface {
protected String goalType;
protected int id;
protected String title;
protected String description;
protected Date achieveDate;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name="GOAL_ID")
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
@Column(name = "DESCRIPTION", nullable = false)
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name = "ACHIEVE_DATE")
public Date getAchieveDate() {
return achieveDate;
}
public void setAchieveDate(Date achieveDate) {
this.achieveDate = achieveDate;
}
@Column(name="TITLE", nullable=false, length=250)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Column(name="goalType", insertable=false, updatable=false)
public String getGoalType() {
return this.goalType;
}
public void setGoalType(String goalType) {
this.goalType= goalType;
}
}
//Sub class for Parent Class - Goal
@Entity
@DiscriminatorValue("WeaknessGoal")
public class WeaknessGoal extends Goal {
private Weakness weakness;
public WeaknessGoal(){
}
public WeaknessGoal(String goalTitle, String goalDescription){
this.title = goalTitle;
this.description = goalDescription;
}
public WeaknessGoal(String goalTitle, String goalDescription, Date goalAchieveDate){
this.title = goalTitle;
this.description = goalDescription;
this.achieveDate = goalAchieveDate;
}
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="WEAKNESS_ID")
public Weakness getWeakness()
{
return this.weakness;
}
public void setWeakness(Weakness weakness)
{
this.weakness = weakness;
}
}
//Sub class for Parent Class - Goal
@Entity
@DiscriminatorValue("PersonalGoal")
public class PersonalGoal extends Goal {
private User user;
public PersonalGoal(){
}
public PersonalGoal(String goalTitle, String goalDescription){
this.title = goalTitle;
this.description = goalDescription;
}
public PersonalGoal(String goalTitle, String goalDescription, Date goalAchieveDate){
this.title = goalTitle;
this.description = goalDescription;
this.achieveDate = goalAchieveDate;
}
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="id")
public User getUser()
{
return this.user;
}
public void setUser(User user)
{
this.user = user;
}
}
//NOT A SUB CLASS BUT, MAPPED (ManyToOne) TO WeaknessGoal Class
@Entity //defines the class as an entity - a persistent POJO
@Table(name = "WEAKNESS")//allows you to define the table, catalog, and schema names for your entity mapping
public class Weakness {
public Weakness(){}
//JPA Identifer generation strategy - there are four others (AUTO,TABLE,IDENTITY,IDENTITY COPY)
private int id;
private String description;
private Date dateLogged;
private User user;
private Set<WeaknessGoal> weaknessGoals = new HashSet<WeaknessGoal>(0);
private Category category;
/*Getters and Setters*/
/**
* @return the user
*/
@ManyToOne(fetch=FetchType.LAZY)//Hibernate copies the identifier from another associated entity. Foreign generator.
@JoinColumn(name="id")
public User getUser() {
return user;
}
/**
* @param user the user to set
*/
public void setUser(User user) {
this.user = user;
}
/**
* @return the desc
*/
@Column(name = "DESCRIPTION", nullable = false)
public String getDescription() {
return description;
}
/**
* @param desc the desc to set
*/
public void setDescription(String desc) {
this.description = desc;
}
/**
* @return the id
*/
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
@Column(name = "WEAKNESS_ID")
public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the dateLogged
*/
@Column(name = "DATE_LOGGED")
public Date getDateLogged() {
return dateLogged;
}
/**
* @param dateLogged the dateLogged to set
*/
public void setDateLogged(Date dateLogged) {
this.dateLogged = dateLogged;
}
/**
* @return the category
*/
@ManyToOne(fetch = FetchType.EAGER) //One category has many weaknesses i.e. the Weakness table can have repeated category id's for different users.
@JoinColumn(name="CATEGORY_ID", nullable=true)
public Category getCategory() {
return category;
} /**
* @param category the category to set
*/
public void setCategory(Category category) {
this.category = category;
}
/**
* @return the goal
*/
@OneToMany(mappedBy="weakness", fetch=FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval=true)
public Set<WeaknessGoal> getWeaknessGoals() {
return this.weaknessGoals;
}
/**
* @param goal the goal to set
*/
public void setWeaknessGoals(Set<WeaknessGoal> weaknessgoals) {
this.weaknessGoals = weaknessgoals;
}
}
Last edited by deanclarke; Sep 16th, 2012 at 12:27 PM.
-
Sep 19th, 2012, 01:51 PM
#2
Not sure about the error. But in regards to fetching strategies. It should always be a use case decision, which means at query time and which means your query HQL says what to eager fetch.
So to eager fetch a collection of WeaknessGoal objects in your query would be "FROM Goal g JOIN FETCH g.weaknessgoals w"
Not sure about your object mappings, couldn't quite decipher what is connected to what. Because I would think weaknessgoals would not be a collection in Goal. The object graph from your posting (especially because you didn't use CODE tags) is unreadable I can't tell what class has what. So the HQL I wrote probably won't work. But it shows how in HQL you eagerly fetch a collection in an object.
Hope that helps.
Mark
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules