我是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")
我想它正在尝试连接到数据库并获取我实际配置的客户表。这是我的spring-config:
<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
请帮我解决这个问题!!
答案 0 :(得分:38)
您为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!
...
}
在您的特定示例中,您似乎向Repository接口添加了findByCustomer(Customer customer)
方法(可能称为CustomerRepository
)。您不需要该方法,因为Spring Data for JPA会自动为您生成findOne(<PrimaryKeyClass> id)
方法,您可以使用您的Domain类@Id
的{{1}}作为参数调用它
答案 1 :(得分:1)
Spring ORM的LocalContainerEntityManagerFactoryBean
(从3.1开始,我认为)有一个名为'packagesToScan'的属性。除非您有通过persistence.xml设置的其他供应商属性,否则请删除p:persistenceXmlLocation
并替换为
p:packagesToScan="[package name where your entities are]"
这告诉LocalContainerEntityManagerFactoryBean
在哪里找到所有@Entity
pojos并将它们包含在您的EntityManagerFactory中,以便Spring Data JPA可以找到放置'customer'的位置
答案 2 :(得分:1)
在我的情况下,由于Spring区分大小写,我一直在努力解决这个错误。我首先在 n ame中搜索,在域类中声明为第一个 N ame:
@Entity
public class User {
private String firstName;
...
只是因为find方法被声明为findByFirstname
我才遇到这个错误。将字母 n 更改为 N 后,它可以正常工作。
public Collection<User> findByFirstName(@Param("firstName") String firstname);