Two Representations of the Same Object - Hibernate Issue
up vote
0
down vote
favorite
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;
}
}
java spring hibernate
add a comment |
up vote
0
down vote
favorite
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;
}
}
java spring hibernate
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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;
}
}
java spring hibernate
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;
}
}
java spring hibernate
java spring hibernate
edited 13 hours ago
asked 13 hours ago
Claire Pfister
14
14
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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")
Set<Folder> folder;
File.java
@ManyToOne
private File file;
//getter and setter
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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")
Set<Folder> folder;
File.java
@ManyToOne
private File file;
//getter and setter
add a comment |
up vote
0
down vote
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")
Set<Folder> folder;
File.java
@ManyToOne
private File file;
//getter and setter
add a comment |
up vote
0
down vote
up vote
0
down vote
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")
Set<Folder> folder;
File.java
@ManyToOne
private File file;
//getter and setter
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")
Set<Folder> folder;
File.java
@ManyToOne
private File file;
//getter and setter
answered 17 mins ago
Alien
3,58821022
3,58821022
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265993%2ftwo-representations-of-the-same-object-hibernate-issue%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password