【问题标题】:Spring Data JPA + Hibernate propery prefixSpring Data JPA + Hibernate 属性前缀
【发布时间】:2015-01-24 00:07:29
【问题描述】:

我是使用 Spring Boot 的新手,并给我自己发送了一个个人目标,尝试将其与 JPA 结合使用。

我在http://spring.io/guides 上看到了示例,并设法使用 MySQL 数据库让 JPA 和 Spring Boot 工作:)

所以我创建了一个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 执行此操作的示例。所以想知道我是否有其他人遇到过这个问题并找到了解决方案?

谢谢

【问题讨论】:

  • 您遇到什么错误,考虑使用@Column 映射属性
  • 好的,经过更多阅读后发现我错过了一些东西。通过将@Column从属性声明移动到属性的getter解决了问题。

标签: spring hibernate jpa


【解决方案1】:

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

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

希望这对其他人有帮助:)

【讨论】:

  • 您能解释一下这段代码是如何解决这个问题的吗?
猜你喜欢
  • 2017-07-17
  • 1970-01-01
  • 2019-07-20
  • 2016-06-26
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 1970-01-01
相关资源
最近更新 更多