【问题标题】:spring data jpa @Entity property not found exceptionspring data jpa @Entity property not found 异常
【发布时间】:2013-01-13 13:51:32
【问题描述】:

我是 spring-data-jpa 的新手,目前正在尝试使用 hibernate 来实现。我关注了tutorial!为此,我目前面临启动应用程序本身的问题。 在启动过程中出现以下异常:

 Caused by: org.springframework.data.mapping.PropertyReferenceException: No property customer found for type com.adaptris.dashboard.customer.Customer
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:74)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:326)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:352)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:306)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:270)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:244)
at org.springframework.data.repository.query.parser.Part.<init>(Part.java:73)
at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:180)
at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:260)
at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:240)
at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:71)
at org.springframework.data.jpa.repository.query.PartTreeJpaQuery.<init>(PartTreeJpaQuery.java:57)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:90)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:162)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:280)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:148)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)

客户是我用

注释的域类
@Entity(name = "customer")

我想它正在尝试连接到数据库并获取我实际配置的客户表。这是我的弹簧配置:

<tx:annotation-driven transaction-manager="transactionManager" />
<!-- Activate Spring Data JPA repository support -->
<jpa:repositories base-package="com.adaptris.dashboard.customer" />

    <!-- Declare a datasource that has pooling capabilities -->
<bean id="jpaDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close" p:driverClass="${app.jdbc.driverClassName}"
    p:jdbcUrl="${app.jdbc.url}" p:user="${app.jdbc.username}" p:password="${app.jdbc.password}"
    p:acquireIncrement="5" p:idleConnectionTestPeriod="60" p:maxPoolSize="100"
    p:maxStatements="50" p:minPoolSize="10" />

<!-- Declare a JPA entityManagerFactory -->
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:persistenceXmlLocation="classpath*:META-INF/persistence.xml"
    p:persistenceUnitName="hibernatePersistenceUnit" p:dataSource-ref="jpaDataSource"
    p:jpaVendorAdapter-ref="hibernateVendor" />

<!-- Specify our ORM vendor -->
<bean id="hibernateVendor"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
    p:showSql="false" />

<!-- Declare a transaction manager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" />

数据库是正在运行的 MYSQL。以下是属性:

# database properties
app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.url=jdbc\:mysql\://Mallik-PC/adaptrisschema
app.jdbc.username=dbuser
app.jdbc.password=dbpassword

请帮我解决这个问题!!

【问题讨论】:

    标签: hibernate spring-data-jpa


    【解决方案1】:

    听起来您为 Domain 类指定的 Repository 接口包含一个 findXXX 方法,而 XXX 不是您的 Domain 类的属性。

    例子:

    public interface UserRepository extends CrudRepository<User, String> {
    
        /**
         * Finds a user by name.
         * 
         * @param name The name of the User to find.
         * @return The User with the given name.
         */
        public User findByName(String name);
    }
    

    而您的 Domain 类看起来像这样并且缺少“name”属性:

    @Entity
    public class User {
        private String firstname;
        private String lastname;
        // No "private String name" in here!
        ... 
    }
    

    在您的特定示例中,您似乎向您的存储库接口添加了一个findByCustomer(Customer customer) 方法(可能称为CustomerRepository 之类的东西)。您不需要该方法,因为 Spring Data for JPA 会自动为您生成 findOne(&lt;PrimaryKeyClass&gt; id) 方法,您只需使用 Domain 类 Customer 的 @Id 作为参数来调用它。

    【讨论】:

    • 感谢您的回复!!花了一天时间后,我发现了这一点,并从我的存储库中删除了该方法。但是 findOne( id) 想法听起来不错。我试试看!!。
    • 它也解决了我的问题,但是...?为什么要从方法名称映射查询结果?它应该通过从数据库中获取的参数来完成,您能详细说明一下吗??
    • 其工作方式是始终是与数据库列匹配的属性的getter/setter 方法的名称。您可以通过为属性指定不同的名称并使用 @Column("MyColumnName") 注释手动进行映射来修改它。如果您不执行其中任何一项(名称匹配或@Column 都不执行),则框架不知道如何将查询结果映射到您的对象。
    • 非常感谢,我已经为此苦苦挣扎了一天,只是不明白我的问题是什么。
    【解决方案2】:

    Spring ORM 的LocalContainerEntityManagerFactoryBean(我认为是 3.1)有一个名为“packagesToScan”的属性。除非您有其他要通过 persistence.xml 设置的供应商属性,否则请删除 p:persistenceXmlLocation 并替换为

    p:packagesToScan="[package name where your entities are]"
    

    这告诉LocalContainerEntityManagerFactoryBean 在哪里可以找到您所有的@Entity pojo,并将它们包含在您的 EntityManagerFactory 中,以便 Spring Data JPA 可以找到放置“客户”的位置

    【讨论】:

      【解决方案3】:

      在我的例子中,由于 Spring 区分大小写,我遇到了这个错误。我按 firstname 搜索,域类中的声明为 firstName:

      @Entity
      public class User {
          private String firstName;
      ...
      

      只是因为 find 方法被声明为 findByFirstname 我得到了这个错误。将字母 n 更改为 N 后即可使用。

      public Collection<User> findByFirstName(@Param("firstName") String firstname);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-31
        • 1970-01-01
        • 1970-01-01
        • 2013-05-01
        • 2021-02-17
        • 1970-01-01
        相关资源
        最近更新 更多