[英]Hibernate native queries in a file with no Entity
My first choice where to put some @NamedNativeQueries
is in the file where the data transfer object (a POJO, could be a record if I were using them) is defined.我的第一个选择是将一些
@NamedNativeQueries
放在@NamedNativeQueries
数据传输对象(POJO,如果我使用它们可以是记录)的文件中。 But the DTO isn't an entity so Hibernate doesn't look at the file and the queries are not found.但是 DTO 不是实体,因此 Hibernate 不会查看文件并且找不到查询。 [If you are wondering, I use a
@ConstructorResult
in a @QueryResultSetMapping
to create the objects.] I can put them in a different file with an @Entity
but the semantics are wrong. [如果您想知道,我在
@ConstructorResult
中使用@QueryResultSetMapping
来创建对象。] 我可以使用@Entity
将它们放在不同的文件中,但语义是错误的。
Is there any magic way to make Hibernate look at a file it otherwise would ignore?有什么神奇的方法可以让 Hibernate 查看一个文件,否则它会忽略吗?
You can add extra mapping resources to the session factory.您可以向会话工厂添加额外的映射资源。
For example with Spring Boot:例如使用 Spring Boot:
spring.jpa.mapping-resources=hibernate/MyMapping.hbm.xml,hibernate/MyMapping2.hbm.xml
or without Spring Boot (from https://mkyong.com/hibernate/how-to-add-hibernate-xml-mapping-file-hbm-xml-programmatically/ ):或不使用 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();
Then in the added files (eg hibernate/MyMapping.hbm.xml
) you can insert your named queries:然后在添加的文件(例如
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.