如何在JPA的po类中调用存储过程和函数
转载
在项目开发中用到了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语句,存储过程是直接调用,而调用函数则是在前面加?=.这可是我几天几夜不休得出的结果,拿出来与大家分享,希望有需要的朋友不要走我的弯路!
上一篇: 10 个检测用户名有效性的JS脚本推荐
下一篇: JPA注解学习总结 (转)