弹簧数据jpa @Entity属性未找到异常 [英] spring data jpa @Entity property not found exception
问题描述
我对spring-data-jpa很陌生,目前正试图用hibernate实现。我已经按照教程!为此,我目前面临启动应用程序本身的问题。
在启动过程中,我收到以下异常:
原因:org.springframework.data.mapping.PropertyReferenceException:No在org.springframework.data.mapping.PropertyPath中找到类型为com.adaptris.dashboard.customer.Customer
的物业客户。< init>(PropertyPath.java:74)$ or
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)
在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 $ Predica te.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)维持在org.springframework.data org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy $ AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:68)
。 repository.core.support.RepositoryFactorySupport $ QueryExecutorMethodInterceptor。< init>(RepositoryFactorySupport.java:280)
at org.springframework.data.re在org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
。 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/>
<! - 激活Spring Data JPA存储库支持 - >
< jpa:repositories base-package =com.adaptris.dashboard.customer/>
<! - 声明具有池化功能的数据源 - >
< bean id =jpaDataSourceclass =com.mchange.v2.c3p0.ComboPooledDataSource
destroy-method =closep:driverClass =$ {app.jdbc.driverClassName}
p:jdbcUrl =$ {app.jdbc.url}p:user =$ {app.jdbc.username}p:password =$ {app.jdbc.password}
p: acquireIncrement =5p:idleConnectionTestPeriod =60p:maxPoolSize =100
p:maxStatements =50p:minPoolSize =10/>
<! - 声明一个JPA entityManagerFactory - >
< bean id =entityManagerFactory
class =org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
p:persistenceXmlLocation =classpath *:META-INF / persistence.xml
p:persistenceUnitName =hibernatePersistenceUnitp:dataSource-ref =jpaDataSource
p:jpaVendorAdapter-ref =hibernateVendor/>
<! - 指定我们的ORM供应商 - >
class =org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter
p:showSql =false/>
<! - 声明一个事务管理器 - >
< bean id =transactionManagerclass =org.springframework.orm.jpa.JpaTransactionManager
p:entityManagerFactory-ref =entityManagerFactory/>
而DB是运行的MYSQL。以下是属性:
#数据库属性
app.jdbc.driverClassName = com.mysql.jdbc.Driver
app.jdbc.url = jdbc\:mysql\:// Mallik-PC / adaptrisschema
app.jdbc.username = dbuser
app.jdbc.password = dbpassword
请帮我解决这个问题!!
听起来像您为Domain类指定的Repository接口包含 findXXX
方法,而 XXX
不是您的Domain类的属性。
示例: public interface UserRepository扩展CrudRepository< User,String> {
/ **
*按名称查找用户。
*
* @param name要查找的用户的名称。
* @返回具有给定名称的用户。
* /
public User findByName(String name);
}
而你的Domain类看起来像这样并且错过了name属性:
@Entity
public class User {
private String firstname;
私人字符串姓氏;
//这里没有私人字符串名称!
...
在您的特定示例中,它看起来像添加了一个 findByCustomer(Customer customer)
方法到您的Repository接口(可能称为 CustomerRepository
)。您不需要该方法,因为Spring Data for JPA会自动为您生成 findOne(< PrimaryKeyClass> id)
方法,您可以使用 @Id
您的Domain类 Customer
作为参数。
I am new to the spring-data-jpa and am currently trying to implement with hibernate. I've followed the tutorial! for this and am currently facing issues starting the application itself. I get the following exception during startup:
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)
Customer is my domain class annotated with
@Entity(name = "customer")
And I suppose it is trying to connect to the DB and fetch the customer table which I've actually configured. Here is my 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" />
And the DB is MYSQL which is running. Following are the properties:
# 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
Please help me out in getting out of this issue!!
Sounds like the Repository interface you have specified for your Domain class contains a findXXX
method whereas XXX
is not a property of your Domain class.
Example:
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);
}
And your Domain class looks like this and misses the "name" property:
@Entity
public class User {
private String firstname;
private String lastname;
// No "private String name" in here!
...
}
In your particular example it looks like you added a findByCustomer(Customer customer)
method to your Repository interface (probably called something like CustomerRepository
). You don't need that method as a findOne(<PrimaryKeyClass> id)
method is automatically generated for you by Spring Data for JPA and you can just call it with the @Id
of your Domain class Customer
as a parameter.
这篇关于弹簧数据jpa @Entity属性未找到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!