Retrieve server generated value [message #899776] |
Thu, 02 August 2012 10:19 |
Pavel Zelenka Messages: 61 Registered: July 2009 |
Member |
|
|
Hi,
I have this entity:
@Entity
@Table(name = "reklamace", catalog = "EDS", schema = "dbo")
public class Reklamace implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "id_reklamace")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer idReklamace;
@Column(name = "reklamace_cislo")
private Integer cislo;
@Column(name = "reklamace_datum")
@Temporal(TemporalType.TIMESTAMP)
private Date reklamaceDatum;
On the MSSQL server there is an trigger which generates value for cislo (reklamace_cislo) field on insert. I don't know how to retrieve back this value. I tried refresh() method but it doesn't work.
Can anybody help?
Thanks
Pavel
(EclipseLink 1.1.4, MSSQL 2008)
[Updated on: Thu, 02 August 2012 10:20] Report message to a moderator
|
|
|
Re: Retrieve server generated value [message #900775 is a reply to message #899776] |
Wed, 08 August 2012 12:58 |
|
The problem with refresh is that refresh needs to find the row by id, but your Id in JPA is still null. Refreshing would work for a non-id field, but for the Id there is no easy way to do this. You would be best off letting JPA generate your Id and remove the trigger, or relax it not to assign the Id if included in the insert.
EclipseLink does support a feature to allow triggers to generate Ids using Returning. Your can put @ReturnInsert on an Id, but this requires special insert syntax that is currently only supported by Oracle ("insert into ... returning ..."). If you use a stored procedure for the insert (using a DescriptorCustomizer to override the insertCall), then you could return the new id in a output parameter and using Returning.
James : Wiki : Book : Blog : Twitter
|
|
|
|
Re: Retrieve server generated value [message #905037 is a reply to message #901753] |
Wed, 29 August 2012 15:25 |
|
Returning is only supported on Oracle. To use it on MySQL you would need to define a stored procedure to do the insert and return the value as an output parameter.
It is probably easiest to either initialize the value in Java instead of the database, or refresh the object after inserting it.
James : Wiki : Book : Blog : Twitter
|
|
|
|
Powered by
FUDForum. Page generated in 0.03708 seconds