![](/img/trans.png)
[英]Spring Boot + Hibernate Web Application: How to globally intercept native and managed entity queries?
[英]Hibernate native queries in a file with no Entity
我的第一个选择是将一些@NamedNativeQueries
放在@NamedNativeQueries
数据传输对象(POJO,如果我使用它们可以是记录)的文件中。 但是 DTO 不是实体,因此 Hibernate 不会查看文件并且找不到查询。 [如果您想知道,我在@ConstructorResult
中使用@QueryResultSetMapping
来创建对象。] 我可以使用@Entity
将它们放在不同的文件中,但语义是错误的。
有什么神奇的方法可以让 Hibernate 查看一个文件,否则它会忽略吗?
您可以向会话工厂添加额外的映射资源。
例如使用 Spring Boot:
spring.jpa.mapping-resources=hibernate/MyMapping.hbm.xml,hibernate/MyMapping2.hbm.xml
或不使用 Spring Boot(来自https://mkyong.com/hibernate/how-to-add-hibernate-xml-mapping-file-hbm-xml-programmatically/ ):
SessionFactory sessionFactory = new Configuration()
.addResource("hibernate/MyMapping.hbm.xml")
.buildSessionFactory();
然后在添加的文件(例如hibernate/MyMapping.hbm.xml
)中,您可以插入命名查询:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<sql-query name="test">
select count(*) from SYSIBM.SYSDUMMY1
</sql-query>
</hibernate-mapping>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.