• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Some queries regarding Hibernate Session Behaviour?

 
scott miles
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have below main programme





When i run above prograame in eclipse i see below below queries generated on eclipse console at different lines mentioned in above programme.These are

Hibernate: select person0_.id as id0_0_, person0_.cname as cname0_0_, person0_.addressId as addressId0_0_ from MyProject1.person person0_ where person0_.id=? //Generated at line A

Hibernate: select max(id) from MyProject1.person //Generated at line B

Hibernate: select max(id) from MyProject1.Address //Generated at line B

Hibernate: select person0_.id as id0_0_, person0_.cname as cname0_0_, person0_.addressId as addressId0_0_ from MyProject1.person person0_ where person0_.id=? //Generated at line G

Hibernate: insert into MyProject1.Address (addressLine1, id) values (?, ?)//Generated at line H

Hibernate: insert into MyProject1.person (cname, addressId, id) values (?, ?, ?)//Generated at line H

Hibernate: insert into MyProject1.person (cname, addressId, id) values (?, ?, ?) //Generated at line H

Hibernate: insert into MyProject1.person (cname, addressId, id) values (?, ?, ?)//Generated at line H

As per my understanding when console is showing generated query its hitting the database right?

Another question is why it did not generate the queries select max(id) from MyProject1.person, select max(id) from MyProject1.Address at line C and D as it should get latest max value of ID for person and address from database in case some other session inserted a new person in between?

Similarily why it did not generate the query select person0_.id as id0_0_, person0_.cname as cname0_0_, person0_.addressId as addressId0_0_ from MyProject1.person person0_ where person0_.id=? at line E and F .How does session come to know that person for id 2 and 3 is not updated by any other session.

One more interesting fact i noted is that if i run the one more same program with creation of one person with id 4 in parallel and stop first programme at debug point at line above line A then proceed it once second programme is completed then line E and F also generate the query(basically hits the database) but query not generated at Line G. Not sure how session is behaving and how does it decide when to hit database/Generate query?
 
Rishi Shehrawat
Ranch Hand
Posts: 218
Hibernate Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per my understanding when console is showing generated query its hitting the database right?

Yes

Another question is why it did not generate the queries select max(id) from MyProject1.person, select max(id) from MyProject1.Address at line C and D as it should get latest max value of ID for person and address from database in case some other session inserted a new person in between?

In case there is insert by another transaction your insert operation will fail due to unique constraint violation.

Similarily why it did not generate the query select person0_.id as id0_0_, person0_.cname as cname0_0_, person0_.addressId as addressId0_0_ from MyProject1.person person0_ where person0_.id=? at line E and F .How does session come to know that person for id 2 and 3 is not updated by any other session.

Hibernate knows that the data is in session & since it has not been flushed yet to the database it cannot be updated by another transaction.

Not sure how session is behaving and how does it decide when to hit database/Generate query?

In most of the cases the inserted/updated data is written to the database on transaction commit, even if the data is written (for some scenario's) it will not be visible to other transactions till the current transaction is committed.



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic