Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据JPA+Hibernate属性前缀_Spring_Hibernate_Jpa - Fatal编程技术网

Spring数据JPA+Hibernate属性前缀

Spring数据JPA+Hibernate属性前缀,spring,hibernate,jpa,Spring,Hibernate,Jpa,我刚开始使用SpringBoot,给了我一个个人目标,让我尝试和JPA一起玩 我在上看到了这些示例,并使用MySQL数据库成功地实现了JPA和spring引导: 因此,我创建了一个实体类,如下所示: package demo.data; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistenc

我刚开始使用SpringBoot,给了我一个个人目标,让我尝试和JPA一起玩

我在上看到了这些示例,并使用MySQL数据库成功地实现了JPA和spring引导:

因此,我创建了一个实体类,如下所示:

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);  
}
我创建了一个存储库,如下所示:

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做到这一点。所以我想知道是否有其他人遇到了这个问题并找到了解决方案


谢谢

好的,我在读了一些书之后回答我自己的问题

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

希望这有助于其他人:

你得到了什么错误,考虑使用@栏来绘制PrimeStOK,所以在阅读了更多的书之后,我错过了一些东西。通过将@Column从属性声明移动到属性的getter,解决了问题。您能解释一下这段代码是如何解决问题的吗?