簡體   English   中英

從JPA / Hibernate調用Sybase存儲過程

[英]Invoke Sybase stored procedure from JPA/Hibernate

我需要從JPA調用Sybase存儲過程,並將值返回到Transient對象中,該對象將用於填充另一個持久對象。

這是我所做的:

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

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

<---snipped-->

}

在bean中調用SP:

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);
      }

不幸的是,我一直在抱怨列名錯誤,例如“ x的列名無效”,其中x是CBSCustomer中我的字段的占位符。

經過大量測試,這就是我做到的方式。 我希望它可以幫助其他人。

1)您必須創建一個@Entity POJO進行結果集映射,並使用@Transient注釋所有字段。

2)您需要在該類中具有@ResultSetMapping批注,例如

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

3)現在,您可以使用createNativeQuery中的“ CBSCustomer”別名調用存儲過程並將其映射到此字段,例如

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM