1

Consider entity

public class User {
...
@OneToMany(cascade = CascadeType.ALL)
List<SocialCredential> credentialsList = new ArrayList<SocialCredential>();
}

with DAO Implementation method

@Transactional
@Override
public User getUser(long id){
    Session s = sessionFactory.getCurrentSession();
    User asu = (User) s.get(User.class, id);
    return asu;
}

and Controller

@Controller
public class DummyController {
  @Autowired
  UserDAO userDAO;

  public void anyMethodAccessedByGetORPost(){
     User u= userDAO.getUser(1L);
  }

}

My question is why a simple query for entity User automatically fires query to initialize entity list of SocialCredential ? Is there anything wrong with @Transaction.I am not interested to EAGERLY load list SocialCredential.

2
  • I highly doubt the error you get is from the transaction method. You probably get the error in your page or controller trying to iterate over the collection (at which point the transaction and thus session has already ended). Commented Oct 23, 2015 at 14:29
  • I have figured out the issue .As I am using Google App Engine, the issue lies somewhere at that end .Reposted the issue at stackoverflow.com/questions/33317003/… Commented Oct 24, 2015 at 10:16

1 Answer 1

0

The error you have is a very common Hibernate Error.

List<SocialCredential> credentialsList = new ArrayList<SocialCredential>();

You need to get that list, you can use another Query to get that specific list, and set to your user Object.

Session s = sessionFactory.getCurrentSession();
list credentialsList = this.getCredentialsList();
User asu = (User) s.get(User.class, id);
asu.setCredentialsList(credentialsList);    
return asu;

Other way that sometimes work is to do:

Session s = sessionFactory.getCurrentSession();
User asu = (User) s.get(User.class, id);
 **adding this line **
asu.getCredentialsList.get(0);
return asu;
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer.Changing List<SocialCredential> credentialsList = new ArrayList<SocialCredential>(); to List<SocialCredential> credentialsList; does not my problem.The two method you depicted is a hack to load the full object(FetchType.EAGER already does that) but that doesnot solve the issue to load the proxy/partial object.
I have figured out the issue .As I am using Google App Engine, the issue lies somewhere at that end .Reposted the issue at stackoverflow.com/questions/33317003/…

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.