如何在JPA的po类中调用存储过程和函数
2014-12-22 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517193
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
在项目开发中用到了JPA规范,并在po类中使用了存储过程,这种资料在网上很容易找到,所以很快就跑通了,代码如下:
@Entity
@Table(name = "USER", schema = "MOBILE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "LOGINID" }),
@UniqueConstraint(columnNames = { "USERACCOUNT" }) })
@NamedNativeQuery(name = "addUser", query = "{call addUser(:pPortalID,:ploginid,:ploginpasswd,:pSelfQuiz,:pSelfAnswer,:pUserEmail,:pUserAccount,:pUserPin)}", hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }, resultClass = User.class)
public class User implements java.io.Serializable {
private static final long serialVersionUID = 5325039036880950119L;
private String userid;
private String loginid;
private String passwd;
//省略若干代码
}
但是后来却要求将addUser改成了函数,所以也要将po类中的调用修改。起初我以为很简单,数据库中的存储过程和函数差不多,不用修改直接调用即可。但是事实却不像我想得那样,错误百出,我上网查了很多资料,但是没有能给出答案的,后来才知道只要修改一个地方就可以获得想要的功能,郁闷!
下面是修改后的代码:
@Entity
@Table(name = "USER", schema = "MOBILE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "LOGINID" }),
@UniqueConstraint(columnNames = { "USERACCOUNT" }) })
@NamedNativeQuery(name = "addUser", query = "{?=call addUser(:pPortalID,:ploginid,:ploginpasswd,:pSelfQuiz,:pSelfAnswer,:pUserEmail,:pUserAccount,:pUserPin)}", hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }, resultClass = User.class)
public class User implements java.io.Serializable {
private static final long serialVersionUID = 5325039036880950119L;
private String userid;
private String loginid;
private String passwd;
//省略若干代码
}
请注意call addUser语句,存储过程是直接调用,而调用函数则是在前面加?=.这可是我几天几夜不休得出的结果,拿出来与大家分享,希望有需要的朋友不要走我的弯路!
转载
@Entity
@Table(name = "USER", schema = "MOBILE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "LOGINID" }),
@UniqueConstraint(columnNames = { "USERACCOUNT" }) })
@NamedNativeQuery(name = "addUser", query = "{call addUser(:pPortalID,:ploginid,:ploginpasswd,:pSelfQuiz,:pSelfAnswer,:pUserEmail,:pUserAccount,:pUserPin)}", hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }, resultClass = User.class)
public class User implements java.io.Serializable {
private static final long serialVersionUID = 5325039036880950119L;
private String userid;
private String loginid;
private String passwd;
//省略若干代码
}
但是后来却要求将addUser改成了函数,所以也要将po类中的调用修改。起初我以为很简单,数据库中的存储过程和函数差不多,不用修改直接调用即可。但是事实却不像我想得那样,错误百出,我上网查了很多资料,但是没有能给出答案的,后来才知道只要修改一个地方就可以获得想要的功能,郁闷!
下面是修改后的代码:
@Entity
@Table(name = "USER", schema = "MOBILE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "LOGINID" }),
@UniqueConstraint(columnNames = { "USERACCOUNT" }) })
@NamedNativeQuery(name = "addUser", query = "{?=call addUser(:pPortalID,:ploginid,:ploginpasswd,:pSelfQuiz,:pSelfAnswer,:pUserEmail,:pUserAccount,:pUserPin)}", hints = { @QueryHint(name = "org.hibernate.callable", value = "true") }, resultClass = User.class)
public class User implements java.io.Serializable {
private static final long serialVersionUID = 5325039036880950119L;
private String userid;
private String loginid;
private String passwd;
//省略若干代码
}
请注意call addUser语句,存储过程是直接调用,而调用函数则是在前面加?=.这可是我几天几夜不休得出的结果,拿出来与大家分享,希望有需要的朋友不要走我的弯路!
转载
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询