• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How Can I return the generated key with Hibernate?

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm inserting records in the database but I need to know how I can return the generated key using Hibernate. I'm currently using prepared statement to return the key as a string:

After the query is executed, I use this code to return back the generated key for the record I just inserted


I need to be able to do the same thing with Hibernate but I don't know how. Can anyone please help???

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hibernate does this for you, you don;t need to do anything.
 
Ranch Hand
Posts: 182
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roberto Hernandez,

You can use merge method of Session interface,



In the above code ,
nsb is the object to persist.
nsbC is the the object to return a copy of persisted object.
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
show me your hibernate code...then may be i can help you, furthermore check this
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

anbarasu aladiyan wrote:Hi Roberto Hernandez,

You can use merge method of Session interface,



In the above code ,
nsb is the object to persist.
nsbC is the the object to return a copy of persisted object.



Why do you need to do this? The save method will return the value of the generated identifier. Calling merge is an unecessary step.
 
Roberto Hernandez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
This is the code that I'm trying to convert to Hibernate. I'm fairly new to Hibernate so please bear with me.

the dao.insertApplication() returns the generated key as a String (appKey) because it will be a foreign key in the next insert of this transaction (dao.insertAppModuleInfo), and the generated key (moduleKey) will also serve as a foreign key for dao.insertModuleFunctionality() and dao.ModuleBusArea(). Am I making myself clear? There's probably a better way to do this in hibernate, I just don't know how. I'm hoping you all can help.
Thanks,
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

if you are gerenating your AppId using key generators then it automatically hibernate does for you.

for e.g.
i have a POJO



I am not setting user_id becoz it is auto generated by database sequence or hibernate incerement

just after calling the session.save(user)

if i want user_id i just now user user.getUserId() it will return user id generated by hibernate while inserting.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
go to the link :
https://www.devglan.com/spring-jdbc/fetch-auto-generated-primary-key-value-after-insert-spring-jdbc

to show this code


package com.domain.ecommerce.customer.user;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

public class JdbcTemplateImpl {

private final String INSERT_SQL = "INSERT INTO USERS(name,address,email) values(?,?,?)";

@Autowired
private JdbcTemplate jdbcTemplate;

public User create(final User user) {

KeyHolder holder = new GeneratedKeyHolder();
PreparedStatementCreator psc = new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(INSERT_SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getName());
ps.setString(2, user.getFullName());
ps.setString(3, user.getEmail());
return ps;
}
};
jdbcTemplate.update(psc, holder);

int newUserId = holder.getKey().intValue();
user.setId(newUserId);
return user;
}

}
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic