简体   繁体   English

从JPA / Hibernate调用Sybase存储过程

[英]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. 我需要从JPA调用Sybase存储过程,并将值返回到Transient对象中,该对象将用于填充另一个持久对象。

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

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. 不幸的是,我一直在抱怨列名错误,例如“ x的列名无效”,其中x是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. 1)您必须创建一个@Entity POJO进行结果集映射,并使用@Transient注释所有字段。

2) You need to have a @ResultSetMapping annotation in that class, eg 2)您需要在该类中具有@ResultSetMapping批注,例如

@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 3)现在,您可以使用createNativeQuery中的“ CBSCustomer”别名调用存储过程并将其映射到此字段,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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