aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Mapping with Multiple Entities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Mapping with Multiple Entities" Watch "Mapping with Multiple Entities" New topic
Author

Mapping with Multiple Entities

Amar Deep Singh Chaudhary
Ranch Hand

Joined: Dec 26, 2006
Posts: 49
hi ranchers

i have written three entities in which my one entity is related to other two entities with one to many bidirectional relationship. When i did the same i get error AS-org.Hibernate.hibernateException- can't simultaneously fetch multiple bags.
if i try to run with only a single relation then it works correctly.i am using the MappedBy clause.

Code for my parent table--

package com.ibilt.cgda.dadproject.ejb.businessobjects.common;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.util.Collection;
import java.util.ArrayList;
import java.io.Serializable;
import java.sql.*;
import javax.persistence.CascadeType;
import javax.persistence.FetchType;
/**
* @author amardeep
*
*/
@SequenceGenerator(name="mySequence",sequenceName="PRT_PRJTYPEID_N_P",initialValue=1,allocationSize=1)
@Entity
@Table(name="PRJ_M_PRJTYPE",schema="cgda_dad")
public class ProjectTypeEntity implements Serializable{
private long projectTypeId;
private String projectName,description,remark,lastUser;
private Boolean flag;
private Date lastDate,timeStamp;
private Collection<GradeTypeEntity> gradetypes = new ArrayList<GradeTypeEntity>();
//private Collection<AcoPurposeEntity> accPurpose = new ArrayList<AcoPurposeEntity>();
private Collection<AuthorityEntity> authority = new ArrayList<AuthorityEntity>();
//private Collection<LetterPurposeEntity> letterPurpose=new ArrayList<LetterPurposeEntity>();


// for description of project type or activity
@Column(name="DESCRIP_V")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
// it will contains the flag for activate/deactivate the fields
@Column(name="FLAG_B",columnDefinition="boolean")
public Boolean isFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
// it will contain the last update date
@Column(name="LUDATE_D")
public Date getLastDate() {
return lastDate;
}
public void setLastDate(Date lastDate) {
this.lastDate = lastDate;
}

// it will contain the information about last updated user
@Column(name="LUSER_V" )
public String getLastUser() {
return lastUser;
}
public void setLastUser(String lastUser) {
this.lastUser = lastUser;
}
//it will contain the name of the project activity
@Column(name="PRT_PRJNAME_V_U")
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
// it will contains the primary key of the ProjectTypeTable
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="mySequence")
@Column(name="PRT_PRJTYPEID_N_PK",columnDefinition="bigserial")
public long getProjectTypeId() {
return projectTypeId;
}
public void setProjectTypeId(long projectTypeId) {
this.projectTypeId = projectTypeId;
}
@Column(name="REMARK_V")
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Column(name="TIMESTAMP_T")
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}

//relationship with grade type-- a project can contain many grades/types


@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,mappedBy="projecttype")
public Collection<GradeTypeEntity> getGradetypes() {
return gradetypes;
}

public void setGradetypes(Collection<GradeTypeEntity> gradetypes) {
this.gradetypes = gradetypes;
}


@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,mappedBy="projectname")
public Collection<AuthorityEntity> getAuthority() {
return authority;
}
public void setAuthority(
Collection<AuthorityEntity> authority) {
this.authority = authority;
}
}

*
code for one child entitty---

package com.ibilt.cgda.dadproject.ejb.businessobjects.common;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import java.io.Serializable;
import java.sql.*;
/**
* @author amardeep
*
*/
@SequenceGenerator(name="mySequence",sequenceName="GRD_GRATYPEID_N_P",initialValue=1,allocationSize=1)
@Entity
@Table(name="PRJ_M_GRADETYPE", schema="cgda_dad")
public class GradeTypeEntity implements Serializable{
private long gradeTypeId;
private String gradName,description,remark,lastUser;
private Boolean flag;
private Date lastDate,timeStamp;
private ProjectTypeEntity projecttype;
//it will be used for entering the description about the grade type.
@Column(name="DESC_V")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}

// it will be used for primark key purpose
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="mySequence")
@Column(name="GRD_GRATYPEID_N_PK",columnDefinition="bigserial")
public long getGradeTypeId() {
return gradeTypeId;
}
public void setGradeTypeId(long gradeTypeId) {
this.gradeTypeId = gradeTypeId;
}
// it will be used for entering the name of the grade
@Column(name="GRADENAME_V")
public String getGradName() {
return gradName;
}
public void setGradName(String gradName) {
this.gradName = gradName;
}
// it will be used for adding any remark
@Column(name="REMARK_V")
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
// it will contains the flag for activate/deactivate the fields
@Column(name="FLAG_B",columnDefinition="boolean")
public Boolean isFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
// it will contain the last update date
@Column(name="LUDATE_D")
public Date getLastDate() {
return lastDate;
}
public void setLastDate(Date lastDate) {
this.lastDate = lastDate;
}
//it will contain the last updated user id
@Column(name="LUSER_V")
public String getLastUser() {
return lastUser;
}
public void setLastUser(String lastUser) {
this.lastUser = lastUser;
}


// it will be used for time Stamp.
@Column(name="TIMESTAMP_T")
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
// to make relationship with Project type entity. The different grades can be contained within only one project.
@ManyToOne
public ProjectTypeEntity getProjecttype() {
return projecttype;
}
public void setProjecttype(ProjectTypeEntity projecttype) {
this.projecttype = projecttype;
}


}

code for other child entity--

package com.ibilt.cgda.dadproject.ejb.businessobjects.common;

import java.io.Serializable;
import java.sql.*;
import java.util.Collection;
import java.util.ArrayList;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.CascadeType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import javax.persistence.ManyToMany;

/**
* @author amardeep
*
*/
@SequenceGenerator(name="mySequence",sequenceName="PAH_AUTHORTYID_N_P",initialValue=1,allocationSize=1)
@Entity
@Table(name="PRJ_M_AUTHORTY",schema="cgda_dad")
public class AuthorityEntity implements Serializable {
private long authorityId;
private String description,remark,authorityName,lastUser;
private Date timeStamp,lastDate;
private Boolean flag;
private ProjectTypeEntity projectname;

//primary key of the Pending Authority Table
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="mySequence")
@Column(name="PAH_AUTHORTYID_N_PK",columnDefinition="bigserial")
public long getAuthorityId() {
return authorityId;
}
public void setAuthorityId(long authorityId) {
this.authorityId = authorityId;
}
//for puting name of the pending Authority
@Column(name="AUTHNAME_V")
public String getAuthorityName() {
return authorityName;
}
public void setAuthorityName(String authorityName) {
this.authorityName = authorityName;
}
//for giving description about the authority
@Column(name="DESC_V")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
// for enabling or disabling the fields
@Column(name="FLAG_B",columnDefinition="boolean")
public Boolean isFlag() {
return flag;
}
public void setFlag(Boolean flag) {
this.flag = flag;
}
// for getting the date of last updation
@Column(name="LUDATE_D")
public Date getLastDate() {
return lastDate;
}
public void setLastDate(Date lastDate) {
this.lastDate = lastDate;
}
// for getting the last updated user
@Column(name="LUSER_V")
public String getLastUser() {
return lastUser;
}
public void setLastUser(String lastUser) {
this.lastUser = lastUser;
}
// for remark purpose
@Column(name="REMARK_V")
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
// for time stamp purpose Only
@Column(name="TIMESTAMP_T")
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}


@ManyToOne
public ProjectTypeEntity getProjectname() {
return projectname;
}
public void setProjectname(ProjectTypeEntity projectname) {
this.projectname = projectname;
}

}

Any body please help me.

With Regards
Amar Deep Singh
E-Mail- amardeep@amardeep.100webspace.net amardeep.100webspace.net


"A thinking mind never dies"
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Are you using JBoss (my bet, or another with the same problem).

What's the exact message being spewn to the console?

Use code tags. (hit the paper and pencil icon, select the code, click on the CODE button below the text area)

My bet is this:

The Collection contract allows the same object to be in the collection more than once. This is a database problem, so it fails (with cryptic messages for you). Try changing them to Set, which states that an object cannot be placed inside twice, and see if that resolves the problem.

(was the word "Bag" in the error message?)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Amar Deep Singh Chaudhary
Ranch Hand

Joined: Dec 26, 2006
Posts: 49
dear
All
i have solved the problem. THe problem was in the code line---@OneToMany(cascade={CascadeType.ALL},fetch=FetchType.EAGER,mappedBy="projectname")///

to short out the problem i have revomed the fetch=FetchType.EAGER from the above code line now it works fine.

now i am facing another problem, when i try to set some value in database table it throw the exception SQL Syntax.It is due to the fact that i am generating my PK through Sequence.
The Database i am using is postgres.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mapping with Multiple Entities