Spring Data JPA + Hibernate propery前缀

时间:2014-11-25 20:24:16

标签: spring hibernate jpa

我刚开始使用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;

@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());
    }
}

我按如下方式创建了一个存储库:

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);  
}

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

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

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

但是我想继续使用pascalcase -m作为成员变量,我似乎找不到让JPA这样做的例子。所以我想知道是否有其他人遇到过这个问题并找到了解决方案?

由于

1 个答案:

答案 0 :(得分:0)

好的,所以我在更多阅读之后回答了我自己的问题

@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());
    }
}

希望这有助于其他人:)