-->
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: When is identity column value retrieved?
PostPosted: Sat Jun 25, 2016 6:12 pm 
Newbie

Joined: Sat Jun 25, 2016 5:58 pm
Posts: 3
Just embarking on this Hibernate journey here.

If I have a column that is defined like so

Code:
   @Id
   @GeneratedValue(strategy=GenerationType.IDENTITY)
   @Column(name="USER_ID")
        private Long userId;

and the application logic looks like so
Code:
      Session session = HibernateUtil.getSessionFactory().openSession();
      session.getTransaction().begin();
      User user = new User();
      //set various properties of user
      session.save(user);
I can see that if I inspect the value of the user instance right after the save it has the value of the id that was just inserted. So is hibernate doing a "SELECT" from the database right after the INSERT to retrieve that value?

But reading the manual I get the impression that if there are other columns in the table that are populated by means of triggers or some such, then I need to do a session.refresh() so the user instance has those properties.

And one related question
Is there anything to choose between these two statements
Code:
      User justInsertedUser = (User)session.get(User.class, user.getUserId());
and
Code:
      session.refresh(user);

Thanks!


Top
 Profile  
 
 Post subject: Re: When is identity column value retrieved?
PostPosted: Mon Jun 27, 2016 1:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
When it comes to database-generated columns (e.g IDENTITY), it really depends on the underlying driver capabilities to fetch the newly generated PK value.

The [url=https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#generatedKeyAlwaysReturned()]DatabaseMetaData.html#generatedKeyAlwaysReturned()
[/url] tells if the Driver supports retrieving generated key using the very same database rountrip used for executing the statement:

Code:
PreparedStatement ps= connection.prepareStatement(
    "INSERT INTO post (title) VALUES (?)",
     Statement.RETURN_GENERATED_KEYS
);

while (resultSet.next()) {
    LOGGER.info("Generated identifier: {}", resultSet.getLong(1));
}


Nevertheless, this might work with JDBC batching or not, which is also a Driver-specific detail. If the JDBC Driver does not support such a construct, Hibernate must use a secondary query to fetch the newly generated identifier.

As for refresh, you only need it if the database generates other columns than the identifier, like when you @Generated.


Top
 Profile  
 
 Post subject: Re: When is identity column value retrieved?
PostPosted: Mon Jun 27, 2016 6:52 am 
Newbie

Joined: Sat Jun 25, 2016 5:58 pm
Posts: 3
mihalcea_vlad wrote:
When it comes to database-generated columns (e.g IDENTITY), it really depends on the underlying driver capabilities to fetch the newly generated PK value.

The [url=https://docs.oracle.com/javase/7/docs/api/java/sql/DatabaseMetaData.html#generatedKeyAlwaysReturned()]DatabaseMetaData.html#generatedKeyAlwaysReturned()
[/url] tells if the Driver supports retrieving generated key using the very same database rountrip used for executing the statement:

Code:
PreparedStatement ps= connection.prepareStatement(
    "INSERT INTO post (title) VALUES (?)",
     Statement.RETURN_GENERATED_KEYS
);

while (resultSet.next()) {
    LOGGER.info("Generated identifier: {}", resultSet.getLong(1));
}


Nevertheless, this might work with JDBC batching or not, which is also a Driver-specific detail. If the JDBC Driver does not support such a construct, Hibernate must use a secondary query to fetch the newly generated identifier.

As for refresh, you only need it if the database generates other columns than the identifier, like when you @Generated.



great, that link helped- in fact there is a response that dovetails with my situation since I'm working with MySQL.
Quote:
MySQL receives the generated key(s) automatically in the OK packet of the protocol in response to executing a statement. There is no communication overhead when requesting generated keys.

which is presumably why in a further experiment I found that the same "user" instance that I used in the session.save actually had the value of the id populated when I inspected it immediately after the save statement.
Thanks!


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.