I have a select statement that loads in the class Folders with a one-to-many relationship with File. While this sometimes happens without error, it sometimes gives me a Hibernate error saying that my use of session is unsafe, or that there were two representations of the same collection Folders.file. What am I doing wrong? Thanks for your help!
Folders.java
@Entity
@Table(name= "folders")
public class Folders implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "folder_code")
private Integer folderCode;
@Column(name = "assign_code")
private Integer assignCode;
public Set<File> getFile() {
return file;
}
public void setFile(Set<file> assignments) {
this.file = file;
}
@OneToMany(targetEntity=File.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name="assign_code",referencedColumnName="assign_code")
Set<Folder> folder;
public Integer getAssignCode() {
return assignCode;
}
public void setAssignCode(Integer assignCode) {
this.assignCode = assignCode;
}
public Integer getFolderCode() {
return folderCode;
}
public void setFolderCode(Integer folderCode) {
this.folderCode = folderCode;
}
public Date retrieveFileStartDate(){
List<File> file;
if(this.getFile()!=null){
file= new ArrayList<File>(this.getFile());
}else{
file = new ArrayList<File>();
}
return file.size()>0 ? new
Date(file.get(0).getStartDate()): null;
}
}
File.java
@Entity
@Table(name= "file")
public class File implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name = "assign_code")
private Integer assignCode;
@Column(name = "start_date")
private String startDate;
@Column(name = "end_date")
private String endDate;
public Integer getAssignCode() {
return assignCode;
}
public void setAssignCode(Integer assignCode) {
this.assignCode = assignCode;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
}
I am not sure about the error you are getting but looking at your entities i can say that relationship mapping is not correct.
You are mapping @OneToMany in Folder entity but what about @ManyToOne in File entity?
also define mappedBy attribute to make it work expected.
Folder.java
@OneToMany(targetEntity=File.class,cascade=CascadeType.ALL,fetch=FetchType.EAGER,mappedBy="file")
@JoinColumn(name="assign_code",referencedColumnName="assign_code")
private Set<Folder> folder;
File.java
@ManyToOne
private File file;
//getter and setter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.