樱花难忘 发表于 2009-2-20 11:18:08

如何在JPA的po类中调用存储过程和函数

在项目开发中用到了JPA规范,并在po类中使用了存储过程,这种资料在网上很容易找到,所以很快就跑通了,代码如下:


1
www.ad119.cn/bbs/attachments/basic/20090220/200922011174871877801.gif
@Entity
2
www.ad119.cn/bbs/attachments/basic/20090220/200922011174878177802.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174882877803.gif
@Table(name = "USER", schema = "MOBILE", uniqueConstraints =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{
3
www.ad119.cn/bbs/attachments/basic/20090220/200922011174892177805.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174896877806.gif
      @UniqueConstraint(columnNames =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{ "LOGINID" }),
4
www.ad119.cn/bbs/attachments/basic/20090220/200922011174892177805.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174896877806.gif
      @UniqueConstraint(columnNames =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{ "USERACCOUNT" }) })
5
www.ad119.cn/bbs/attachments/basic/20090220/200922011174878177802.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174882877803.gif
@NamedNativeQuery(name = "addUser", query = "{call addUser(:pPortalID,:ploginid,:ploginpasswd,:pSelfQuiz,:pSelfAnswer,:pUserEmail,:pUserAccount,:pUserPin)}", hints =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{ @QueryHint(name = "org.hibernate.callable", value = "true") }, resultClass = User.class)
6
www.ad119.cn/bbs/attachments/basic/20090220/200922011174878177802.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174882877803.gif
public class User implements java.io.Serializable
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{
7
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif

8
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private static final long serialVersionUID = 5325039036880950119L;
9
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private String userid;
10
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private String loginid;
11
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private String passwd;
12
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
      //省略若干代码
13
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577808.gif
}
    但是后来却要求将addUser改成了函数,所以也要将po类中的调用修改。起初我以为很简单,数据库中的存储过程和函数差不多,不用修改直接调用即可。但是事实却不像我想得那样,错误百出,我上网查了很多资料,但是没有能给出答案的,后来才知道只要修改一个地方就可以获得想要的功能,郁闷!
    下面是修改后的代码:


1
www.ad119.cn/bbs/attachments/basic/20090220/200922011174871877801.gif
@Entity
2
www.ad119.cn/bbs/attachments/basic/20090220/200922011174878177802.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174882877803.gif
@Table(name = "USER", schema = "MOBILE", uniqueConstraints =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{
3
www.ad119.cn/bbs/attachments/basic/20090220/200922011174892177805.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174896877806.gif
      @UniqueConstraint(columnNames =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{ "LOGINID" }),
4
www.ad119.cn/bbs/attachments/basic/20090220/200922011174892177805.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174896877806.gif
      @UniqueConstraint(columnNames =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{ "USERACCOUNT" }) })
5
www.ad119.cn/bbs/attachments/basic/20090220/200922011174878177802.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174882877803.gif
@NamedNativeQuery(name = "addUser", query = "{?=call addUser(:pPortalID,:ploginid,:ploginpasswd,:pSelfQuiz,:pSelfAnswer,:pUserEmail,:pUserAccount,:pUserPin)}", hints =
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{ @QueryHint(name = "org.hibernate.callable", value = "true") }, resultClass = User.class)
6
www.ad119.cn/bbs/attachments/basic/20090220/200922011174878177802.gif

www.ad119.cn/bbs/attachments/basic/20090220/200922011174882877803.gif
public class User implements java.io.Serializable
www.ad119.cn/bbs/attachments/basic/20090220/200922011174887577804.gif
{
7
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif

8
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private static final long serialVersionUID = 5325039036880950119L;
9
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private String userid;
10
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private String loginid;
11
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
    private String passwd;
12
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577807.gif
      //省略若干代码
13
www.ad119.cn/bbs/attachments/basic/20090220/20092201117491577808.gif
}
    请注意call addUser语句,存储过程是直接调用,而调用函数则是在前面加?=.这可是我几天几夜不休得出的结果,拿出来与大家分享,希望有需要的朋友不要走我的弯路!
页: [1]
查看完整版本: 如何在JPA的po类中调用存储过程和函数