如何在 Spring 数据 JPA 中对命名查询使用 XML 配置? [英] How to use XML configuration for named queries in Spring data JPA?

查看:36
本文介绍了如何在 Spring 数据 JPA 中对命名查询使用 XML 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 文档 我们可以在 XML 中指定命名查询.但它没有说明如何在代码中使用它.有人可以帮忙吗?

As per the Documentation we can specify named queries in an XML. But it does not tell how to use it in the code. Can someone please help?

推荐答案

say you have defined the `Named Query` as `application.myquery` in XML file.

在service/dao层

In service/dao layer

List results = em.createNamedQuery("application.myquery")
    .setParameter("username", "blah")
    .setParameter("password","blahblahblah")
    .getResultList();

orm 文件需要包含在 persistence.xml 中,并且该文件应位于 META-INF/persistence.xml.

The orm file need to be included inside persistence.xml and the same should reside at META-INF/persistence.xml.

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">

  <persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
    <mapping-file>META-INF/orm.xml</mapping-file>
    <exclude-unlisted-classes/>
  </persistence-unit>

</persistence>

参考这个了解更多信息.

这篇关于如何在 Spring 数据 JPA 中对命名查询使用 XML 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆