Return Auto Generated Key Query Mysql Java

 
Ranch Hand
posted 10 years ago
  • Get a MySQL Connection from the Driver Manager: 20.34.2. JDBC Mysql Connection String: 20.34.3. MySQL Connection Properties: Passing Additional Properties Using a Database URL: 20.34.4. Keep the Connection Alive for MySQL: 20.34.5. MySQL Data type to Java Data type Conversion Table: 20.34.6. Create a MySQL Table to Store Java Types (Using.
  • How to get primary key value (auto-generated keys) from inserted queries using JDBC? JDBC Object Oriented Programming Programming If you insert records into a table which contains auto-incremented column, using a Statement or, PreparedStatement objects.
  • MySQL Connector/J 5.1 Developer Guide / JDBC Concepts / Retrieving AUTOINCREMENT Column Values through JDBC 6.4 Retrieving AUTOINCREMENT Column Values through JDBC Before version 3.0 of the JDBC API, there was no standard way of retrieving key values from databases that supported auto increment or identity columns.
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
Key

Oct 11, 2006  Just recently I ran into a problem with what seems to be a deficiency in the Oracle Database. When trying to return an auto-generated key as is done in Microsoft's SQL database, it seems Oracle for whatever reason didn't add this capability, the key couldn't be passed back to the.Net call. MySQL and Java « Previous Message. Hp g60 coprocessor driver windows 7. Wednesday, January 08, 2003 10:57 Subject: Re: retrieving auto-generated keys on bulk insert The code that we have has a low-level save. Meaning if a statement causes the creating of more than one key then the ResultSet object should return all. Using auto generated keys.; 2 minutes to read +2. A way to make IDENTITY values available to an application that is updating a database table without a requiring a query and a second round-trip to the server. Updates that have to use the auto-generated key feature must operate against a table that contains an IDENTITY column. This field is known as an auto increment field in MySQL, and either an identity or serial field in other databases like SQL Server or Postgresql. In either case, the problem is the same: How to get the value of the primary key field following your SQL INSERT statement.) Java Spring JDBC - Auto-generated database key source code.

posted 10 years ago
Hibernate does this for you, you don;t need to do anything.
Ranch Hand
posted 10 years ago
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
posted 10 years ago
show me your hibernate code.then may be i can help you, furthermore check this
Bartender
posted 10 years ago

anbarasu aladiyan wrote:Hi Roberto Hernandez,
You can use merge method of Session interface,

In the above code ,
nsb is the object to persist. /gta-iv-product-key-windows-live-generator.html.
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.
Ranch Hand
posted 10 years ago
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
posted 10 years ago
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

Return Auto Generated Key Query Mysql Java Software

posted 1 year ago

Mysql Insert Query

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;
}
}