Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Retrieve server generated value
Retrieve server generated value [message #899776] Thu, 02 August 2012 06:19 Go to next message
Pavel Zelenka is currently offline 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 06:20]

Report message to a moderator

Re: Retrieve server generated value [message #900775 is a reply to message #899776] Wed, 08 August 2012 08:58 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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 #901753 is a reply to message #900775] Tue, 14 August 2012 09:09 Go to previous messageGo to next message
Pavel Zelenka is currently offline Pavel Zelenka
Messages: 61
Registered: July 2009
Member
James thank you for your reply. The problem is not in id filed idReklamace but in non-id field called cislo. This value is generated via trigger and I need to retrieve it. I think @ReturnUpdate or @ReturnInsert would be the solution but it doesn't work (EclipseLink 1.1.4, MSSQL 2008)
This is the error message:
Exception [EclipseLink-193] (Eclipse Persistence Services - 1.1.4.v20100812-r7860): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There is no custom [UpdateObjectQuery] set, but ReturningPolicy contains field(s) to be returned and [SQLServerPlatform] doesnt support generating call with returning.
Descriptor: RelationalDescriptor(cz.mediaservis.eds.entity.eds.Reklamace --> [DatabaseTable(EDS.dbo.reklamace)])


Any idea how to solve it?

Thanks
Re: Retrieve server generated value [message #905037 is a reply to message #901753] Wed, 29 August 2012 11:25 Go to previous messageGo to next message
James Sutherland is currently offline James Sutherland
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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
Re: Retrieve server generated value [message #905843 is a reply to message #905037] Fri, 31 August 2012 03:22 Go to previous message
Pavel Zelenka is currently offline Pavel Zelenka
Messages: 61
Registered: July 2009
Member
Thanks. Refreshing the object is probably the best solution for me.
Previous Topic:Attempting to retry query
Next Topic:The parameter name in the query's selection criteria does not match any parameter name defined in th
Goto Forum:
  


Current Time: Sun Apr 20 19:43:35 EDT 2014

Powered by FUDForum. Page generated in 0.01576 seconds