For some good reasons i do a spring data jpa native insert.
The query gets executed in the right way.
But what i need is the newly generated table id getting generated by the nextval('hibernate_sequence')
Is there a way to get this id?
Here is my query:
/**
* Inserts a new file attachment.
* This is useful and maybe better suitable, because one may not want to load the whole
* job offer to fullfill the JobOffer -> FileAttachment OneToMany Relation
*
* @param file a given file
* @param fileName a given file name
* @param jobOfferId a given job offer id
* @return int the new id
*/
@Modifying
@Query(value = "insert into fileattachment(fileattachmentid, file, filename, joboffer_jobofferid) values (nextval('hibernate_sequence'), ?1, ?2, ?3);", nativeQuery = true)
int insertFileAttachment(String file, String fileName, long jobOfferId);
The int return value just gives the number of inserted records (1).
But i need the newly Id.
I don´t want to query it after the insert by another database query. Because if i have to, the whole native insert gets obsolete.
Does someone know an answer/Has someone alternative tips?
Thank you!
Kind regards
Thomas
Edit:
I use the native insert to avoid loading the whole joboffer record, which is a lot of useless data, just to persist the data the way with the entitymanager.
Instead i insert the data native.
Anyway your tip with the returning data from insert statements was very cool.
I was giving that a try and it is working. Thank you very very much!
I ended up with this solution:
/**
* Inserts a new file attachment.
* This is useful and maybe better suitable, because one may not want to load the whole
* job offer to fullfill the JobOffer -> FileAttachment OneToMany Relation
*
* @param file a given file
* @param fileName a given file name
* @param jobOfferId a given job offer id
* @return int the new id
*/
@Query(value = "insert into fileattachment(fileattachmentid, file, filename, joboffer_jobofferid) values (nextval('hibernate_sequence'), ?1, ?2, ?3) returning fileattachmentid;", nativeQuery = true)
long insertFileAttachment(String file, String fileName, long jobOfferId);
If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.
Then at runtime, the ORM framework translates JPQL queries into SQL queries that are specific to the underlying database. So, a native query is a SQL statement that is specific to a particular database (e.g. MySQL), which differs slightly from JPQL which is used by Spring Data JPA by default.
Create ad-hoc native queries Creating an ad-hoc native query is quite simple. The EntityManager interface provides the createNativeQuery method for it. It returns an implementation of the Query interface, which is the same that you get when you call the createQuery method to create a JPQL query.
When you use the database method for an insert or update DML operation, an array of SaveResult is returned. Every element in the SaveResult array correlates with the sObject Array passed into the database method.
If you are using postgres you can simply do this. The key component is simply add a RETURNING *
which will return all columns in inserted in table. You could then simply add a return type which will be mapped for you. This is in Kotlin which can be easily adapted to java as well. The Query syntax is native.
@Query(
""" INSERT INTO table1 (a,b,c)
select a,b,c from table2 where a = :someNumber
RETURNING *;
""", nativeQuery = true
)
fun addToProcessed(finOrVin: String): PojoEntityTest?
References:
https://www.postgresql.org/docs/9.5/sql-insert.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With