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;
}



}









share|improve this question




























    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;
    }



    }









    share|improve this question


























      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;
      }



      }









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 13 hours ago

























      asked 13 hours ago









      Claire Pfister

      14




      14
























          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





          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            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
































            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





            share|improve this answer

























              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





              share|improve this answer























                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





                share|improve this answer












                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






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 17 mins ago









                Alien

                3,58821022




                3,58821022






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    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




















































































                    Popular posts from this blog

                    mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

                    How to change which sound is reproduced for terminal bell?

                    Can I use Tabulator js library in my java Spring + Thymeleaf project?