special0ne
special0ne

Reputation: 6263

Hibernate Lazy loads when it does not make sense

I am working on my first app in Hibernate. I have only one Entity User with not associations

User Entity definition:

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

@Id
@Column(name="USER_NAME")
private String userName;

@Column(name="PASSWORD")
private String password;

@Basic
@Column(name="EMAIL")
private String email;

and Users table was created with:

CREATE TABLE USERS( user_name varchar(45) PRIMARY KEY, password varchar(45) NOT NULL, email varchar(45) NOT NULL ); (As i said I kept things simple :)

fetching the user record is done like:

User c = (User) sessionFactory.getCurrentSession().load(User.class, userName);

The problem is that i am not getting back a User object but a User_$$_javaassit object with new handler member

handler = org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer userName=null password=null email=null

I understand what lazy load is and when it should be used but this is not one of these situations. I would rather not "turn it off" for the whole system since i will be using it later on.

Any suggestions what i am missing here?

Upvotes: 3

Views: 1190

Answers (2)

raddykrish
raddykrish

Reputation: 1866

i think hibernate by default does lazy loading for all the entities it seems. check this link http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html

Just a thought, instead of using hibernate directly (meaning using the session) you can try using EntityManager which is JPA specification and you can try to tie up the persistent unit which is Hibernate in your case. this helps in future porting to a different ORM easy.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691755

You're missing the Session.get() method.

Session.load() is used precisely to return a proxy. It's useful when you just need a reference to an entity to set as a parent of child entity, for example. In this case, you don't need the state of the parent entity, but only a reference.

BTW, the JPA equivalent method is called getReference(), whereas the JPA equivalent to get() is find().

Upvotes: 1

Related Questions