Spring数据JPA + Hibernate propery前缀 [英] Spring Data JPA + Hibernate propery prefix

查看:235
本文介绍了Spring数据JPA + Hibernate propery前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Spring Boot的新手,并且向我发送了一个个人目标,尝试和JPA结合使用。



我在 http://spring.io / guides>,并设法使用MySQL数据库来获得JPA和spring引导程序:)



所以我创建了一个Entity类,如下所示:

  package demo.data; 

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
$ b $ @Entity
@Table(name =bugs)
public class Bug {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =bug_id)
private Long mId;

@Column(name =bug_severity,nullable = false)
private String mSeverity;

@Column(name =bug_status,nullable = false)
private String mStatus;

@Column(name =priority,nullable = false)
private String mPriority;

@Column(name =short_desc,nullable = false)
private String mShortDesc = null;

@Column(name =reporter)
private int mReporter = 0;

@Column(name =resolution)
private String mResolution = null;

@Column(name =product_id)
private int mProductId = 0;

@Column(name =version)
private String mVersion = null;

// ...额外的成员,通常包含@OneToMany映射
/ **
* JPA规范要求的无参数构造函数。这个是受保护的,因为它不应该被直接使用
* /
protected Bug(){
}
$ b $ / **
*创建一个一个bug的新实例。
*
* @param严重性错误的严重性
* @param status错误的状态
* @param priority错误的优先级
* /
public Bug(字符串严重性,字符串状态,字符串优先级){
this.mSeverity =严重性;
this.mStatus =状态;
this.mPriority = priority;
}

/ **
* @return错误ID。
* /
public Long getId(){
return mId;
}

/ **
* @param id设置错误ID。
* /
public void setId(Long id){
this.mId = id;
}

/ **
* @return错误的严重性
* /
public String getSeverity(){
return mSeverity ;
}

/ **
* @param severity设置错误的严重程度。
* /
public void setSeverity(String severity){
this.mSeverity = severity;
}

/ **
* @return返回优先级
* /
public String getPriority(){
return mPriority;
}

/ **
* @param priority设置错误的优先级。
* /
public void setPriority(String priority){
this.mPriority = priority;

$ b $ **
* @return错误状态
* /
public String getStatus(){
return mStatus ;

$ b $ **
* @param status错误状态
* /
public void setStatus(String status){
this.mStatus =状态;
}

public String getShortDesc(){
return mShortDesc;
}

public void setShortDesc(String shortDesc){
this.mShortDesc = shortDesc;
}

public int getReporter(){
return mReporter;
}

public void setReporter(int reporter){
this.mReporter = reporter;
}

public String getResolution(){
return mResolution;
}

public void setResolution(String resolution){
this.mResolution = resolution;
}

public int getProductId(){
return mProductId;
}

public void setProductId(int productId){
this.mProductId = productId;
}

public String getVersion(){
return mVersion;
}

public void setVersion(String version){
this.mVersion = version;
}

public String toString(){
return String.format(ID =%d,Severity =%s,Priority =%s,desc =%s,记者=%d,
getId(),getSeverity(),getPriority(),getShortDesc(),getReporter());


我创建了一个存储库,如下所示:

  package demo.data; 

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

公共接口BugRepository扩展CrudRepository< Bug,Long> {

列表< Bug> findByReporter(int记者);
Bug findOne(Long id);
long countByReporter(int reporter);
$ b $ // JPQL
@Query(SELECT b.status,count(*)FROM Bug b WHERE b.version =:version AND b.productId =:productId GROUP BY b.status )
public List< Object []> countByStatus(@Param(version)字符串版本,@Param(productId)int productId);
}

当我启动应用程序时,我看到以下错误:



'pre> 产生的原因:java.lang.IllegalArgumentException异常:对于方法public抽象的java.util.List demo.data.BugRepository.findByReporter无法创建查询元模型( INT)!
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateIfNotFoundQueryLookupStrategy。 resolveQuery(JpaQueryLookupStrategy.java:168)
位于org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
位于org.springframework.data.repository。 core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。< init>(RepositoryFactorySupport.java:320)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSuppor t.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory。 $ AbstractButtonBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
... 134 more
java.lang.IllegalArgumentException异常:由造成无法在org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull这个ManagedType [demo.data.Bug]
定位与给定名称[记者]属性( AbstractManagedType.java:144)
处org.springframework.data.jpa.repository.query.QueryUtils org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:130)
。 toExpressionRecursively(QueryUtil s.java:472)维持在org.springframework.data.jpa.repository org.springframework.data.jpa.repository.query.JpaQueryCreator $ PredicateBuilder.build(JpaQueryCreator.java:199)

。 query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:146)
在org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:86)
at org.springframework.data。 jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:44)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
at org。 springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
。在org.springframework.data.jpa.repository.query.PartTreeJpaQuery $ QueryPreparer<初始化>(PartTreeJpaQuery.java:108)
。在org.springframework.data .jpa.repository.query.PartTreeJpaQuery $ CountQueryPreparer。< init>(PartTreeJpaQuery.java:196)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery。< init>(PartTreeJpaQuery.java: 63)
。在org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
... 143更

我追踪到我的成员变量是pascalcase-m而不是camelcase。当我将成员变量更改为camelcase时,它按预期工作。



然而,我想继续使用pascalcase-m作为成员变量,并且我似乎无法找到例子来让JPA做到这一点。所以想知道是否有其他人遇到了这个问题,并找到了解决方案?



谢谢

解决方案

好的,所以我在阅读更多的回答后回答我自己的问题。 $ b

  @Entity 
@Table(name =bugs)
public class Bug {

private Long mId;
private String mSeverity;
private String mStatus;
private String mPriority;
private String mShortDesc = null;
private int mReporter = 0;
private String mResolution = null;
private int mProductId = 0;
private String mVersion = null;

/ **
* JPA规范要求的无参数构造函数。这个是受保护的,因为它不应该被直接使用
* /
protected Bug(){
}
$ b $ / **
*创建一个一个bug的新实例。
*
* @param严重性错误的严重性
* @param status错误的状态
* @param priority错误的优先级
* /
public Bug(字符串严重性,字符串状态,字符串优先级){
this.mSeverity =严重性;
this.mStatus =状态;
this.mPriority = priority;
}

/ **
* @return错误ID。
* /
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name =bug_id)
public Long getId(){
return mId;
}

/ **
* @param id设置错误ID。
* /
public void setId(Long id){
this.mId = id;
}

/ **
* @return错误的严重程度
* /
@Column(name =bug_severity,nullable = false )
public String getSeverity(){
return mSeverity;
}

/ **
* @param severity设置错误的严重程度。
* /
public void setSeverity(String severity){
this.mSeverity = severity;
}

/ **
* @return返回优先级
* /
@Column(name =priority,nullable = false)
public String getPriority(){
return mPriority;
}

/ **
* @param priority设置错误的优先级。
* /
public void setPriority(String priority){
this.mPriority = priority;
}

/ **
* @return错误的状态
* /
@Column(name =bug_status,nullable = false )
public String getStatus(){
return mStatus;

$ b $ **
* @param status错误状态
* /
public void setStatus(String status){
this.mStatus =状态;


@Column(name =short_desc,nullable = false)
public String getShortDesc(){
return mShortDesc;
}

public void setShortDesc(String shortDesc){
this.mShortDesc = shortDesc;
}

@Column(name =reporter)
public int getReporter(){
return mReporter;
}

public void setReporter(int reporter){
this.mReporter = reporter;


@Column(name =resolution)
public String getResolution(){
return mResolution;
}

public void setResolution(String resolution){
this.mResolution = resolution;


@Column(name =product_id)
public int getProductId(){
return mProductId;
}

public void setProductId(int productId){
this.mProductId = productId;


@Column(name =version)
public String getVersion(){
return mVersion;
}

public void setVersion(String version){
this.mVersion = version;
}

public String toString(){
return String.format(ID =%d,Severity =%s,Priority =%s,desc =%s,记者=%d,
getId(),getSeverity(),getPriority(),getShortDesc(),getReporter());


希望这可以帮助别人:)


I'm new to using Spring Boot and have sent me self a personal goal to try and play with and JPA in combination.

I've seen the samples on http://spring.io/guides and have managed to get the JPA and spring boot working using a MySQL database :)

So I created an Entity class as follows:

package demo.data;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "bugs")
public class Bug {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column (name = "bug_id")
    private Long mId;

    @Column(name = "bug_severity", nullable = false)
    private String mSeverity;

    @Column(name = "bug_status", nullable = false)
    private String mStatus;

    @Column(name = "priority", nullable = false)
    private String mPriority;

    @Column(name="short_desc", nullable = false)
    private String mShortDesc = null;

    @Column(name="reporter")
    private int mReporter = 0;

    @Column(name="resolution")
    private String mResolution = null;

    @Column(name="product_id")
    private int mProductId=0;

    @Column(name="version")
    private String mVersion=null;

    // ... additional members, often include @OneToMany mappings
    /**
     * no-args constructor required by JPA spec. This one is protected since it shouldn't be used directly
     */
    protected Bug() {
    }

    /**
     * Creates a new instance of a bug.
     *
     * @param severity The Severity of the bug
     * @param status The status of the bug
     * @param priority The priority of the bug
     */
    public Bug(String severity, String status, String priority) {
        this.mSeverity = severity;
        this.mStatus = status;
        this.mPriority = priority;
    }

    /**
     * @return The bug id.
     */
    public Long getId() {
        return mId;
    }

    /**
     * @param id Sets the bug id.
     */
    public void setId(Long id) {
        this.mId = id;
    }

    /**
     * @return The severity of the bug
     */
    public String getSeverity() {
        return mSeverity;
    }

    /**
     * @param severity Set the severity of the bug.
     */
    public void setSeverity(String severity) {
        this.mSeverity = severity;
    }

    /**
     * @return Returns the priority
     */
    public String getPriority() {
        return mPriority;
    }

    /**
     * @param priority Sets the priority of the bug.
     */
    public void setPriority(String priority) {
        this.mPriority = priority;
    }

    /**
     * @return The status of the bug
     */
    public String getStatus() {
        return mStatus;
    }

    /**
     * @param status The status of the bug
     */
    public void setStatus(String status) {
        this.mStatus = status;
    }

    public String getShortDesc() {
        return mShortDesc;
    }

    public void setShortDesc(String shortDesc) {
        this.mShortDesc = shortDesc;
    }

    public int getReporter() {
        return mReporter;
    }

    public void setReporter(int reporter) {
        this.mReporter = reporter;
    }

    public String getResolution() {
        return mResolution;
    }

    public void setResolution(String resolution) {
        this.mResolution = resolution;
    }

    public int getProductId() {
        return mProductId;
    }

    public void setProductId(int productId) {
        this.mProductId = productId;
    }

    public String getVersion() {
        return mVersion;
    }

    public void setVersion(String version) {
        this.mVersion = version;
    }

    public String toString() {
        return String.format("ID = %d, Severity = %s, Priority = %s, desc=%s, reporter=%d", 
                getId(), getSeverity(), getPriority(), getShortDesc(), getReporter());
    }
}

I created a repository as follows:

package demo.data;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

public interface BugRepository extends CrudRepository<Bug, Long> {

    List<Bug> findByReporter(int reporter);
    Bug findOne(Long id);
    long countByReporter(int reporter);

    // JPQL
    @Query("SELECT b.status, count(*) FROM Bug b WHERE b.version = :version AND b.productId = :productId GROUP BY b.status")    
    public List<Object[]> countByStatus(@Param("version")String version, @Param("productId")int productId);  
}

When I start the application I see the following error:

Caused by: java.lang.IllegalArgumentException: Could not create query metamodel for method public abstract java.util.List demo.data.BugRepository.findByReporter(int)!
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:93)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:168)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:69)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:320)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:169)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224)
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210)
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
    ... 134 more
Caused by: java.lang.IllegalArgumentException: Unable to locate Attribute  with the the given name [reporter] on this ManagedType [demo.data.Bug]
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.checkNotNull(AbstractManagedType.java:144)
    at org.hibernate.jpa.internal.metamodel.AbstractManagedType.getAttribute(AbstractManagedType.java:130)
    at org.springframework.data.jpa.repository.query.QueryUtils.toExpressionRecursively(QueryUtils.java:472)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator$PredicateBuilder.build(JpaQueryCreator.java:199)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.toPredicate(JpaQueryCreator.java:146)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:86)
    at org.springframework.data.jpa.repository.query.JpaQueryCreator.create(JpaQueryCreator.java:44)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createCriteria(AbstractQueryCreator.java:109)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:88)
    at org.springframework.data.repository.query.parser.AbstractQueryCreator.createQuery(AbstractQueryCreator.java:73)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$QueryPreparer.<init>(PartTreeJpaQuery.java:108)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery$CountQueryPreparer.<init>(PartTreeJpaQuery.java:196)
    at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:63)
    at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:91)
    ... 143 more

Which I tracked down to my member variables being pascalcase-m and not camelcase. When I change the member variables to camelcase it works as expected.

However I would like to continue using pascalcase-m for member variables and I can't seem to find an examples to get JPA to do this. So was wondering if anybody else I had come across this problem and found a solution ?

Thanks

解决方案

Ok, so I'm answering my own question after some more reading

@Entity
@Table(name = "bugs")
public class Bug {

    private Long mId;
    private String mSeverity;
    private String mStatus;
    private String mPriority;
    private String mShortDesc = null;
    private int mReporter = 0;
    private String mResolution = null;
    private int mProductId=0;
    private String mVersion=null;

    /**
     * no-args constructor required by JPA spec. This one is protected since it     shouldn't be used directly
     */
    protected Bug() {
    }

    /**
     * Creates a new instance of a bug.
     *
     * @param severity The Severity of the bug
     * @param status The status of the bug
     * @param priority The priority of the bug
     */
    public Bug(String severity, String status, String priority) {
        this.mSeverity = severity;
        this.mStatus = status;
        this.mPriority = priority;
    }

    /**
     * @return The bug id.
     */
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column (name = "bug_id")
    public Long getId() {
        return mId;
    }

    /**
     * @param id Sets the bug id.
     */
    public void setId(Long id) {
        this.mId = id;
    }

    /**
     * @return The severity of the bug
     */
    @Column(name = "bug_severity", nullable = false)
    public String getSeverity() {
        return mSeverity;
    }

    /**
     * @param severity Set the severity of the bug.
     */
    public void setSeverity(String severity) {
        this.mSeverity = severity;
    }

    /**
     * @return Returns the priority
     */
    @Column(name = "priority", nullable = false)
    public String getPriority() {
        return mPriority;
    }

    /**
     * @param priority Sets the priority of the bug.
     */
    public void setPriority(String priority) {
        this.mPriority = priority;
    }

    /**
     * @return The status of the bug
     */
    @Column(name = "bug_status", nullable = false)
    public String getStatus() {
        return mStatus;
    }

    /**
     * @param status The status of the bug
     */
    public void setStatus(String status) {
        this.mStatus = status;
    }

    @Column(name="short_desc", nullable = false)
    public String getShortDesc() {
        return mShortDesc;
    }

    public void setShortDesc(String shortDesc) {
        this.mShortDesc = shortDesc;
    }

    @Column(name="reporter")
    public int getReporter() {
        return mReporter;
    }

    public void setReporter(int reporter) {
        this.mReporter = reporter;
    }

    @Column(name="resolution")
    public String getResolution() {
        return mResolution;
    }

    public void setResolution(String resolution) {
        this.mResolution = resolution;
    }

    @Column(name="product_id")
    public int getProductId() {
        return mProductId;
    }

    public void setProductId(int productId) {
        this.mProductId = productId;
    }

    @Column(name="version")
    public String getVersion() {
        return mVersion;
    }

    public void setVersion(String version) {
        this.mVersion = version;
    }

    public String toString() {
        return String.format("ID = %d, Severity = %s, Priority = %s, desc=%s,     reporter=%d", 
            getId(), getSeverity(), getPriority(), getShortDesc(), getReporter());
    }
}

Hope this helps somebody else :)

这篇关于Spring数据JPA + Hibernate propery前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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