简体   繁体   中英

Invoke Sybase stored procedure from JPA/Hibernate

I need to invoke a Sybase stored procedure from JPA and return the values into a Transient object that will be used to populate another persistent object.

This is what I have done:

@Entity
public class CBSCustomer {
String cpr;
<--snipped-->

@Id
@Transient
public String getCpr() {
    return cpr;
}

<---snipped-->

}

Call to SP in bean:

List<CBSCustomer> fetchedCustomerList = getEmPhoenix().createNativeQuery("{call sp_name(?)}", CBSCustomer.class).setParameter(1, cprInput).getResultList();

if (fetchedCustomerList.size() > 0) {              
          CBSCustomer cbsCustomer = ((CBSCustomer)fetchedCustomerList.get(0));
          setDisabled(true);
      }

Unfortunately I keep getting errors complaining about column names, ie "Invalid column name for x" where x is the placeholder for my fields in CBSCustomer.

After alot of testing, this is the way I did it. I hope that it can help others.

1) You have to create an @Entity POJO for resultsetmapping and annotate all fields with @Transient.

2) You need to have a @ResultSetMapping annotation in that class, eg

@SqlResultSetMapping(
name="CBSCustomer",
entities={
    @EntityResult(
        entityClass=CBSCustomer.class,
        fields={
            @FieldResult(name="name", column="Name"),
            @FieldResult(name="cpr", column="CPR"),
<--snipped-->
        }
    )
}

3) Now you can invoke the stored procedure and map it to this field using the "CBSCustomer" alias in the createNativeQuery, eg

createNativeQuery("{call procedure(?)}", "CBSCustomer")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM