我已经尝试了多种方法来调用hibernate中的存储函数---
1)通过Session.doWork回调方法
session.doWork(new Work() {
@Override
public void execute(Connection conn)
throws SQLException {
CallableStatement stmt = conn.prepareCall("{? = call test(?)}");
stmt.registerOutParameter(1, Types.INTEGER);
stmt.setString(2, "callIndex");
stmt.execute();
eventVal = stmt.getInt(1);
}
});
这样可以正常但不能从这里返回任何东西,所以不能解决我的目的。
2)通过原生查询
StringBuilder query = new StringBuilder();
query.append("Select test() from dual");
SQLQuery sqlQuery = session.createSQLQuery(query.toString());
List resultList = sqlQuery.list();
events = new ArrayList<Events>();
Object result = null;
if (events != null && !events.isEmpty()) {
for (int i = 0; i < events.size(); i++) {
result = resultList.get(i);
}
}
这不起作用,给了我一些JDBC类型-10的无方言映射,不确定原因。
3)通过命名查询
我的实体类是---
@NamedNativeQueries({
@NamedNativeQuery(name = "testCall",
query = "? = call test()",
hints = {@QueryHint(name = "org.hibernate.callable", value = "true" )},
resultClass = Events.class
)
})
@Entity
@Table(name = "EVENTS")
public class Events implements Serializable {
private static final long serialVersionUID = -24850323296832289L;
/** The id. */
@Id
@Column(name = "EVENT_SID")
private Integer eventId;
/** The processed status. */
@Column(name = "EVENT_STATUS")
private String eventStatus;
/** The event type. */
@Column(name = "EVENT_TYPE_NAME")
private String eventType;
/** The live event. */
@Column(name = "IS_LIVE_EVENT")
private Integer liveEvent;
/** The event message. */
@Lob
@Column(name = "EVENT_MESSAGE")
private String eventMessage;
public Integer getEventId() {
return eventId;
}
public void setEventId(Integer eventId) {
this.eventId = eventId;
}
public String getEventStatus() {
return eventStatus;
}
public void setEventStatus(String eventStatus) {
this.eventStatus = eventStatus;
}
/**
* Gets the event type.
*
* @return the event type
*/
public String getEventType() {
return eventType;
}
/**
* Sets the event type.
*
* @param eventType
* the new event type
*/
public void setEventType(String eventType) {
this.eventType = eventType;
}
/**
* Gets the live event.
*
* @return the live event
*/
public Integer getLiveEvent() {
return liveEvent;
}
/**
* Sets the live event.
*
* @param liveEvent
* the new live event
*/
public void setLiveEvent(Integer liveEvent) {
this.liveEvent = liveEvent;
}
/**
* Gets the event message.
*
* @return the event message
*/
public String getEventMessage() {
return eventMessage;
}
/**
* Sets the event message.
*
* @param eventMessage
* the new event message
*/
public void setEventMessage(String eventMessage) {
this.eventMessage = eventMessage;
}
}
我打电话的DAO有方法---
public Integer callSqlBlock(){
int event = 0;
Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
List<Events> events = null;
events = session.getNamedQuery("testCall").list();
return events.get(0).getEventId();
}
在DB端存储函数---
create or replace
function test return SYS_REFCURSOR
as
p_order_recordset SYS_REFCURSOR;
begin
open p_order_recordset FOR SELECT EVENT_SID,EVENT_STATUS,EVENT_TYPE_NAME,IS_LIVE_EVENT FROM events;
return p_order_recordset;
end ;
通过命名查询执行存储函数给了我无效的列索引
请告诉我这里出错的地方,如果可能请提供一些例子