-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Collection is not lazy?
PostPosted: Tue Feb 27, 2007 6:51 am 
Newbie

Joined: Wed Feb 14, 2007 5:38 am
Posts: 6
Hibernate version: 3.2

I have the following code:

@Entity
public class Client {
@Id
@GeneratedValue
private int id;
private String name;
@OneToMany (cascade={CascadeType.PERSIST})
@JoinColumn (name="clientid")
private Collection<CreditCard> creditCards;

public Client() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public Collection<CreditCard> getCreditCards() {
return creditCards;
}
public void setCreditCards(Collection<CreditCard> creditCards) {
this.creditCards = creditCards;
}

}



@Entity
public class CreditCard {
@Id
@GeneratedValue
private int id;
private String cardnumber;

public CreditCard() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setCardnumber(String cardnumber) {
this.cardnumber = cardnumber;
}
public String getCardnumber() {
return cardnumber;
}

}



public class Application3 {
private static SessionFactory sessionFactory;

static {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}

public static void main(String[] args) {
Session session = null;
Transaction tx = null;
Product product = null;
int clientid=0;


try {
session = sessionFactory.openSession();
tx = session.beginTransaction();

Client client = new Client();
client.setName("John Doe");
CreditCard cc1 = new CreditCard();
cc1.setCardnumber("1234 5678 2345 3456");
CreditCard cc2 = new CreditCard();
cc2.setCardnumber("9876 5432 7654 8765");
CreditCard cc3 = new CreditCard();
cc3.setCardnumber("4376 9765 4529 0001");
CreditCard cc4 = new CreditCard();
cc4.setCardnumber("8745 5564 3821 9930");
CreditCard cc5 = new CreditCard();
cc5.setCardnumber("3256 1865 3927 3345");
CreditCard cc6 = new CreditCard();
cc6.setCardnumber("3286 8346 9153 0011");
CreditCard cc7 = new CreditCard();
cc7.setCardnumber("2231 4453 7221 8333");
CreditCard cc8 = new CreditCard();
cc8.setCardnumber("5552 3338 4571 9992");
Collection<CreditCard> list = new ArrayList<CreditCard>();
list.add(cc1);
list.add(cc2);
list.add(cc3);
list.add(cc4);
list.add(cc5);
list.add(cc6);
list.add(cc7);
list.add(cc8);
client.setCreditCards(list);
session.persist(client);
clientid = client.getId();
tx.commit();

} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
if (session != null)
session.close();
}

try {
session = sessionFactory.openSession();
tx = session.beginTransaction();

Client client2 = (Client)session.load(Client.class, new Integer(clientid));
System.out.println("Client name= " + client2.getName());

Collection<CreditCard> list2 = client2.getCreditCards();
Iterator<CreditCard> iterator = list2.iterator();
while (iterator.hasNext()){
CreditCard theCard = iterator.next();
System.out.println("CreditCard number= " + theCard.getCardnumber());
}

tx.commit();

} catch (HibernateException e) {
tx.rollback();
e.printStackTrace();
} finally {
if (session != null)
session.close();
}
}

}


Running this code produces the following result:

Hibernate: /* insert Client */ insert into Client (id, name) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)

Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* insert CreditCard */ insert into CreditCard (id, cardnumber) values (null, ?)
Hibernate: call identity()
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* create one-to-many row Client.creditCards */ update CreditCard set clientid=? where id=?
Hibernate: /* load Client */ select client0_.id as id1_0_, client0_.name as name1_0_ from Client client0_ where client0_.id=?
Client name= John Doe
Hibernate: /* load one-to-many Client.creditCards */ select creditcard0_.clientid as clientid1_, creditcard0_.id as id1_, creditcard0_.id as id2_0_, creditcard0_.cardnumber as cardnumber2_0_ from CreditCard creditcard0_ where creditcard0_.clientid=?
CreditCard number= 1234 5678 2345 3456
CreditCard number= 9876 5432 7654 8765
CreditCard number= 4376 9765 4529 0001
CreditCard number= 8745 5564 3821 9930
CreditCard number= 3256 1865 3927 3345
CreditCard number= 3286 8346 9153 0011
CreditCard number= 2231 4453 7221 8333
CreditCard number= 5552 3338 4571 9992

What I don't understand is that the collection of CreditCard object are not loaded lazy. I would suspect that hibernate would issue a new select query to the database every time I print the creditcard number in the while loop.

When I do some testing, it seems that the line

Iterator<CreditCard> iterator = list2.iterator();

issues a select to the database, and all creditcards are loaded in one select.

Why are all the creditcards not lazy loaded?

Rene.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 7:42 am 
Newbie

Joined: Wed Feb 07, 2007 7:42 am
Posts: 13
Location: Spain
When it's executed the line

Code:
Client client2 = (Client)session.load(Client.class, new Integer(clientid));


client2 has its dates, and a reference of its creditCards collection. There aren't CreditCard objects, because it's a lazy association.

Then, it's executed the line

Code:
Collection<CreditCard> list2 = client2.getCreditCards();


This return a list of CreditCards object, in one select is obtened all the CreditCards from the client.

The load lazy get all the object from the collection when the collection is hinted.

Regards


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 27, 2007 7:42 am 
Newbie

Joined: Wed Feb 07, 2007 7:42 am
Posts: 13
Location: Spain
When it's executed the line

Code:
Client client2 = (Client)session.load(Client.class, new Integer(clientid));


client2 has its dates, and a reference of its creditCards collection. There aren't CreditCard objects, because it's a lazy association.

Then, it's executed the line

Code:
Collection<CreditCard> list2 = client2.getCreditCards();


This return a list of CreditCards object, in one select is obtened all the CreditCards from the client.

The load lazy get all the object from the collection when the collection is hinted.

Regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.