How to write entity into database using stored procedure? [message #381069] |
Tue, 02 September 2008 04:37  |
Eclipse User |
|
|
|
Originally I posted this message on eclipselink-users mailing list, but I
have no answer so far thus I am posting it here hoping to get some help.
I can read entities using stored procedures perfectly:
<code>
@Entity
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(
name="Book.getBook",
procedureName="TEST.GET_BOOK",
resultClass=Book.class,
parameters={
@StoredProcedureParameter(queryParameter="ID",
direction=Direction.IN_OUT),
@StoredProcedureParameter(queryParameter="TITLE",
direction=Direction.OUT)}),
@NamedStoredProcedureQuery(
name="Book.listBooks",
procedureName="TEST.LIST_BOOKS",
resultClass=Book.class,
parameters={@StoredProcedureParameter(queryParameter="OUTPUT ",
direction=Direction.OUT_CURSOR)})
})
public class Book implements Serializable {
@Id
private Long id;
private String title;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
</code>
After having such entity it is just enough to call:
Query query = em.createNamedQuery("Book.getBook");
query.setParameter("ID", 10);
Book book = query.getSingleResult();
or
Query query = em.createNamedQuery("Book.listBooks");
List<Question> books = query.getResultList();
Now I want to do the opposite - somehow pass the entity as stored
procedure input.
I imagine it could be done either by using a bunch of IN parameters
corresponding to entity fields (but then how to set them automatically
without using explicit query.setParameter calls), or rather by using
some automatic mapping of entity into java.sql.Struct which is then
passed as single stored procedure argument. I tried to configure it
without any success. Any ideas?
|
|
|
Re: How to write entity into database using stored procedure? [message #381073 is a reply to message #381069] |
Tue, 02 September 2008 10:04  |
Eclipse User |
|
|
|
You can override the EclipseLink insert/update/delete operations to use
stores procedures. There are not currently annotations for this (please
log or vote for this issue), but you can do it through a
DescriptorCustomizer. You use the ClassDescriptor getQueryManager() and
use the StoredProcedureCall class and setInsertCall() API. Then you just
need to persist the object, and your procedure will be called.
You could also define a named query with a bunch of IN parameters to
perform the insert, and pass all of the object's attributes manually, but
this would be treated as a data only operation.
-- James
|
|
|
Powered by
FUDForum. Page generated in 2.57567 seconds