2

Here is my entity models.

 @Entity
 @Table(name = "Folder")
 public class Folder implements Serializable{


     private User user;

     //unidirectional association
     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "AssignedToUserID" ,nullable=true)
     public User getUser() {
        return user;
     }
 }

 @Entity
 @Table(name = "User")
 public class User implements Serializable{

    @Id
    @Column(name = "UserID")
    public Integer getUserId() {
      return this.userId;
    }
 }

Basically i want retrieve all folders together whether the folder has assigned user or not.

And here is my HQL query:

*SELECT folder from Folder folder inner join fetch folder.user user*

SQL generated By hibnermate:
    select
        folder0_.FolderID ,
        folder0_.FolderName ,
        folder0_.AssignedToUserID ,
        user_0.UserID ,
        user_0.UserName
    from
        Folder folder0_ 
    left outer join
        User user_0 
            on folder0_.AssignedToUserID=user_0.UserID

I wanted to eagerly load all associated entities, I really wanted to avoid the other select statements because it hurts performance and i am retrieving around 500k of records.

I am expecting that hibernate will return null instance when it sees that AssignedToUserID is NULL. But unfortunately it throws EntityNotFoundException though.

Am i missing something here? Any suggestion is appreciated.

4
  • Is there any more text with the 'EntityNotFoundException'? Often it is of the kind: EntityNotFoundException: Unable to find XXXXX with id XXXXXX. Also, can you check in your database whether table Folder has rows with AssignedToUserID's that do not exist in User? Commented Sep 22, 2014 at 9:03
  • Yes you are correct. Unable to find User with id 0. Ill check if there are any records that dont exist. Commented Sep 23, 2014 at 1:17
  • I think it means there is a Folder with AssignedToUserID=0 Commented Sep 23, 2014 at 7:17
  • Its now working. It seems that i have missed out a record with id equal zero, but the last time i checked it there is none. But thank you so much its working now. :) Commented Sep 24, 2014 at 4:42

1 Answer 1

2

Try to use

left outer join fetch folder.user user

Sign up to request clarification or add additional context in comments.

1 Comment

Can you write what SQL query is performed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.