我正在阅读来自http://www.petrikainulainen.net/programming/spring-framework/spring-data-jpa-tutorial-creating-database-queries-with-named-queries/的petri教程,并决定做一个关于Spring Data JPA的概念证明(POC)。我使用XML创建了一个命名的本机查询,并使用@Query(nativeQuery = true)
注释了我的用户存储库。 (顺便说一句,Hibernate是我的JPA提供者)。
这是我的UserRepository.java中的方法
@Query(nativeQuery = true)
public User findByNameAndPasswordXml(@Param("name") String name,@Param("password") String password);
这是我的orm.xml文件,位于src / main / resources / META-INF文件夹下面
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
version="2.0">
<named-native-query name="User.findByNameAndPasswordXml"
result-class="org.jrue.poc.springhibernate.domain.User">
<description>Multiple search query</description>
<query>
SELECT
*
FROM
M_USER
WHERE
name = :name
AND password = :password
</query>
</named-native-query>
</entity-mappings>
为了使Spring Data工作,我使用下面的注释
在持久性配置中指定了位置@EnableJpaRepositories(basePackages={"org.jrue.poc.springhibernate.repository"}
,namedQueriesLocation="classpath:META-INF/*.xml")
然而,当我执行单元测试时,我收到以下异常,该异常指向我的orm.xml文件。
Caused by: java.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 23; Document root element "entity-mappings", must match DOCTYPE root "null".
at sun.util.xml.PlatformXmlPropertiesProvider.load(Unknown Source)
at java.util.Properties$XmlSupport.load(Unknown Source)
at java.util.Properties.loadFromXML(Unknown Source)
at org.springframework.util.DefaultPropertiesPersister.loadFromXml(DefaultPropertiesPersister.java:78)
at org.springframework.core.io.support.PropertiesLoaderUtils.fillProperties(PropertiesLoaderUtils.java:91)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:175)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:156)
at org.springframework.beans.factory.config.PropertiesFactoryBean.createProperties(PropertiesFactoryBean.java:102)
at org.springframework.beans.factory.config.PropertiesFactoryBean.afterPropertiesSet(PropertiesFactoryBean.java:71)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
... 68 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 6; columnNumber: 23; Document root element "entity-mappings", must match DOCTYPE root "null".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source
我使用错误的XML格式吗?我从网上搜索过,发现实体映射没有相关问题。