2012-09-19 80 views
1

我需要从JPA调用Sybase存储过程,并将值返回到将用于填充另一个持久对象的Transient对象。从JPA/Hibernate调用Sybase存储过程

这是我做了什么:

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

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

<---snipped--> 

} 

呼叫到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

经过很多测试,这是我做到的方式。我希望它能帮助别人。

1)您必须为resultsetmapping创建@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)现在,可以调用存储过程和使用“CBSCustomer”别名在createNativeQuery其映射到该字段中,例如

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

感谢您分享您的答案! –