我是JPA的新手。 背景:
我想要实现的是混合JPA注释和ORM(用于那些复杂的本机查询)。拥有下一个资源:
的persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="com.nexti_dragonstone-console_war_0.0.1PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.nexti.dragonstone.entity.DgMobile</class>
<class>com.nexti.dragonstone.entity.DgMobileControl</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<mapping-file>META-INF/orm.xml</mapping-file>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dragonglass?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="root"/>
</properties>
</persistence-unit>
</persistence>
META-INF / orm.xml
<?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">
<description>Extracted queries for DgMobile repository</description>
<!-- using JPQL query -->
<named-query name="DgMobile.findWhereVersionLessThan">
<query><![CDATA[
SELECT m FROM DgMobile m WHERE m.version < :version
]]></query>
</named-query>
<!-- using Native SQL query -->
<named-native-query name="DgMobile.findWhereVersionGreaterThan"
result-class="com.nexti.dragonstone.entity.DgMobile">
<query><![CDATA[
SELECT * FROM dg_mobile WHERE version > :version
]]></query>
</named-native-query>
</entity-mappings>
DgMobile.java(实体)
@Entity
@Table(name = "dg_mobile")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "DgMobile.findAll", query = "SELECT d FROM DgMobile d")
, @NamedQuery(name = "DgMobile.findByIdMobile", query = "SELECT d FROM DgMobile d WHERE d.idMobile = :idMobile")
, @NamedQuery(name = "DgMobile.findByImei", query = "SELECT d FROM DgMobile d WHERE d.imei = :imei")
, @NamedQuery(name = "DgMobile.findWhereVersionLessThan", query="SELECT m FROM DgMobile m WHERE m.version < :version")
})
@NamedNativeQueries({
})
public class DgMobile implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id_mobile", nullable = false)
private Integer idMobile;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 25)
@Column(nullable = false, length = 25)
private String imei;
...
DgMobileRepository
@RepositoryRestResource
public interface DgMobileRepository extends JpaRepository<DgMobile, Integer> {
//single JPA-JPQL query implementation
DgMobile findByIdMobile(@Param("id") int idMobile);
//single JPA-JPQL query implementation with custom param names
DgMobile findByImei(@Param("imei") String imei );
//JPA-JPQL implementation for complex or native queries (using an XML file)
@Query(nativeQuery = false, name = "DgMobile.findWhereVersionLessThan")
List<DgMobile> findWhereVersionLessThan(@Param("version") int version);
//JPA-SQL implementation for complex or native queries (using an XML file)
// with custom param and method names
@Query(nativeQuery = true, name = "DgMobile.findWhereVersionGreaterThan")
List<DgMobile> findWhereVersionGreaterThan(@Param("version") int version);
}
正如您所看到的,在声明到Repository中的最后一个方法中,我正在尝试使用在我的orm.xml文件中声明的本机sql查询。
构建项目时,将显示下一个错误:
创建名为'dgMobileRepository'的bean时出错:调用init 方法失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:没有 找到类型DgMobile的属性findWhereVersion!
我已经尝试为orm xml使用不同的名称并在persistence.xml上声明它,也尝试不要声明它,因为Springboot + JPA默认情况下在META-INF上搜索它。
我知道许多人根据惯例不赞成使用XML来支持使用注释。但对我而言,一个重要的原因是,当您使用大量复杂的表和数据时,您的查询有时非常大,当您将它们复制到SQL编辑器时,您不希望花时间格式化它们。因此,在这种情况下,我明确地不要使用JPA注释和ORM(XML声明)的混合进行查询。
完整代码可在github上找到。
作为附加信息,我按照以下说明操作:https://hahamo.wordpress.com/2016/06/21/jpa-queries-in-xml-files/
Lads,我缺少什么?