Hi Vitor!
See my ideas inline :)
2007/11/2, Vitor Rodrigues <[EMAIL PROTECTED]>:
> is there a way to use output variables in stored procedures in OpenJPA? I
> have a stored procedure called calcutaleTax (subtotal IN, taxamount OUT)
> that I use to calculate the tax for an order. subtotal is an input argument,
> and taxamount is an output argument.
>
> I did a few experiments, but didn't manage to get the taxamount value. This
> is what I currently have, but the value is not being assigned to "tax":
> private double calculateTaxJPA(double subtotal){
> EntityManager em = DatabaseUtil.getEntityManager();
> double taxamount = 0;
> Query q = em.createQuery("Call JDBC_SC.CALCULATETAX(?,?)");
> q.setParameter(1, subtotal);
> q.setParameter(2, taxamount);
> q.executeUpdate();
> return taxamount;
> }
>
>
> So, this is all I was able to think of. Does anyone know how can I achieve
> my needs? From a JDBC perspective, you need a CallableStatement in order to
> call a stored procedure instead of a regular Statement or PreparedStatement.
> However, CallableStatement has the execute() method, not present in the
> javax.persistence.Query class.
>
> If anybody has any ideas, please let me know :-)
I actually do not know the solution, but the above can not work, as
taxamount is passed into the query as value, not as a reference. So,
there is no way to store anything back to taxamount. Have you tried a
native query? Or tried to call getSingleResult() on the query?
Reference:
http://java.sun.com/javaee/5/docs/api/javax/persistence/EntityManager.html
http://java.sun.com/javaee/5/docs/api/javax/persistence/Query.html
>
> Second question: @Ids that aren't persisted.
> From my understanding, even if I don't declare an @Id annotation in a class,
> OpenJPA assigns an Id variable to that class and tries to insert into the
> database. This caused some exceptions on my app, because I didn't have Id
> column in the database. Is it possible to have a transient ID so that
> OpenJPA has different IDs for all materialized java objects, but then it
> discards that information when storing it in the database. As an example, I
> have the PorderItem class, representing an item in a purchase order, and it
> goes like this:
>
> @Entity
> @Table(name="PORDER_ITEM", schema="JPA_SC")
> public class PorderItem {
>
> protected int poid; //purchaseorder ID
> protected String pid; //product ID
> protected int cid; //customer ID
> protected int poqty; // quantity
> protected String postatus;
> protected String podate;
>
> public PorderItem(){
> }
> .........
> }
>
>
> How can I get OpenJPA to store only these fields without inserting an extra
> ID column? Note that I cannot use a composite ID with several variables,
> because it is possible to have repeated entries.
Which is impossible. Every row in a table must be unique and every
RDBMS enforces this. So, your primary key seems to be composed of all
the above properties (maybe minus quantity). Looking at it more
closely, are you sure that poid isn't your primary key?
HTH
Oliver