Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Some queries regarding Hibernate Session Behaviour?

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 218
Hibernate Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
And tomorrow is the circus! We can go to the circus! I love the circus! We can take this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic