- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下查询,该查询在 mysql 上运行良好。我想通过 spring-data-jpa 来实现相同的 querydsl API 。
select c.* from orders o, customers c where o.customerNumber=103 and c.customerNumber=103;
CustomerRepository.java
public interface CustomerRepository extends JpaRepository<Customer, Integer>,
QueryDslPredicateExecutor<Customer>{
@Query("select c from Customer c where c.country='USA'")
List<Customer> findByCountry();
@Query("select c from Order o, Customer c where o.customerNumber=:customerNumber and c.customerNumber=:customerNumber")
List<Customer> findByCustomerNumber(@Param("customerNumber") Integer customerNumber);
}
但是当我运行代码时,我看到以下错误:
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: customerNumber of: net.javabeat.springdata.jpa.data.Order [select c from net.javabeat.springdata.jpa.data.Order o, net.javabeat.springdata.jpa.data.Customer c where o.customerNumber=:customerNumber and c.customerNumber=:customerNumber]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy.$Proxy31.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
... 53 more
Caused by: org.hibernate.QueryException: could not resolve property: customerNumber of: net.javabeat.springdata.jpa.data.Order [select c from net.javabeat.springdata.jpa.data.Order o, net.javabeat.springdata.jpa.data.Customer c where o.customerNumber=:customerNumber and c.customerNumber=:customerNumber]
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1967)
at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:313)
at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:490)
at org.hibernate.hql.internal.ast.tree.DotNode.getDataType(DotNode.java:616)
at org.hibernate.hql.internal.ast.tree.DotNode.prepareLhs(DotNode.java:267)
at org.hibernate.hql.internal.ast.tree.DotNode.resolve(DotNode.java:214)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:119)
at org.hibernate.hql.internal.ast.tree.FromReferenceNode.resolve(FromReferenceNode.java:115)
at org.hibernate.hql.internal.ast.HqlSqlWalker.resolve(HqlSqlWalker.java:882)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.expr(HqlSqlBaseWalker.java:1264)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.exprOrSubquery(HqlSqlBaseWalker.java:4426)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.comparisonExpr(HqlSqlBaseWalker.java:3898)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2076)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.logicalExpr(HqlSqlBaseWalker.java:2001)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.whereClause(HqlSqlBaseWalker.java:794)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:595)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:299)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:247)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:250)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:185)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:222)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:200)
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1703)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:291)
... 60 more
我开发的测试用例:
@Test
public void testCustomerOrder(){
List<Customer> customers = customerRepository.findByCustomerNumber(103);
System.out.println("CUSTOMETS : "+customers);
for (Customer customer : customers) {
System.out.println("================================");
System.out.println(customer.getCustomerNumber());
System.out.println(customer.getCustomerName());
System.out.println(customer.getContactFirstName());
System.out.println(customer.getContactLastName());
System.out.println(customer.getPhone());
System.out.println(customer.getAddressLine1());
System.out.println(customer.getAddressLine2());
System.out.println(customer.getCity());
System.out.println(customer.getState());
System.out.println(customer.getPostalCode());
System.out.println(customer.getCountry());
System.out.println(customer.getCreditLimit());
List<Order> orders = customer.getOrders();
for (Order order : orders) {
System.out.println("-------------------------");
System.out.println(order.getOrderNumber());
System.out.println(order.getOrderDate());
System.out.println(order.getRequiredDate());
System.out.println(order.getShippedDate());
System.out.println(order.getStatus());
System.out.println();
}
}
}
订单.java
@Entity
@Table(name="orders")
@NamedQuery(name="Order.findAll", query="SELECT o FROM Order o")
public class Order implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int orderNumber;
@Lob
private String comments;
@Temporal(TemporalType.DATE)
private Date orderDate;
@Temporal(TemporalType.DATE)
private Date requiredDate;
@Temporal(TemporalType.DATE)
private Date shippedDate;
private String status;
//bi-directional many-to-one association to Orderdetail
@OneToMany(mappedBy="order")
private List<Orderdetail> orderdetails;
//bi-directional many-to-one association to Customer
@ManyToOne
@JoinColumn(name="customerNumber")
private Customer customer;
// setters and getters
}
客户.java
@Entity
@Table(name="customers")
@NamedQuery(name="Customer.findAll", query="SELECT c FROM Customer c")
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int customerNumber;
private String addressLine1;
private String addressLine2;
private String city;
private String contactFirstName;
private String contactLastName;
private String country;
private double creditLimit;
private String customerName;
private String phone;
private String postalCode;
private String state;
//bi-directional many-to-one association to Employee
@ManyToOne
@JoinColumn(name="salesRepEmployeeNumber")
private Employee employee;
//bi-directional many-to-one association to Order
@OneToMany(mappedBy="customer")
private List<Order> orders;
//bi-directional many-to-one association to Payment
@OneToMany(mappedBy="customer")
private List<Payment> payments;
// setters and getters
}
有专家尽快指点吗?
EDIT-1:我尝试了下面,但仍然出现错误:
@Query("select c from Customer c where c.orders.customerNumber=:customerNumber and c.customerNumber=:customerNumber")
List<Customer> findByCustomerNumber(@Param("customerNumber") Integer customerNumber);
上述配置出现错误:
Caused by: java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List net.javabeat.springdata.repository.CustomerRepository.findByCustomerNumber(java.lang.Integer)!
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:92)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.<init>(SimpleJpaQuery.java:62)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromMethodWithQueryString(JpaQueryFactory.java:72)
at org.springframework.data.jpa.repository.query.JpaQueryFactory.fromQueryAnnotation(JpaQueryFactory.java:53)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$DeclaredQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:136)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$CreateIfNotFoundQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:204)
at org.springframework.data.jpa.repository.query.JpaQueryLookupStrategy$AbstractQueryLookupStrategy.resolveQuery(JpaQueryLookupStrategy.java:73)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:416)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:206)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:251)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:237)
at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:92)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1631)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568)
... 40 more
Caused by: java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: customerNumber of: net.javabeat.springdata.jpa.data.Order [select c from net.javabeat.springdata.jpa.data.Customer c where c.orders.customerNumber=:customerNumber and c.customerNumber=:customerNumber]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1374)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:294)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
at com.sun.proxy.$Proxy31.createQuery(Unknown Source)
at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
... 53 more
Caused by: org.hibernate.QueryException: could not resolve property: customerNumber of: net.javabeat.springdata.jpa.data.Order [select c from net.javabeat.springdata.jpa.data.Customer c where c.orders.customerNumber=:customerNumber and c.customerNumber=:customerNumber]
Edit-2:根据 @Andy 的建议,我可以访问数据,但也看到以下错误
org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: net.javabeat.springdata.jpa.data.Customer.orders, could not initialize proxy - no Session
at org.hibernate.collection.internal.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:566)
at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:186)
at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:545)
at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:124)
at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:266)
at net.javabeat.springdata.CustomerTest.testCustomerOrder(CustomerTest.java:57)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:70)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
最佳答案
@Query("从客户 c 中选择 c,其中 c.orders.customerNumber=:customerNumber 和 c.customerNumber=:customerNumber")
您尝试访问 Order
中的 customerNumer
属性,但我没有看到名为 customeNumber
的属性。您只有一个 orderNumber
。
@Query("从客户 c 中选择 c,订单 o 其中 o.customer.customerNumber=:customerNumber 和 c.customerNumber=:customerNumber")
但我不太确定你真正想要实现什么。您想要客户的所有订单吗?
关于java - 由 : java. lang.IllegalArgumentException : org. hibernate.QueryException 引起:无法解析属性:customerNumber of:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35707894/
我在执行此代码时遇到问题org.hibernate.QueryException:无法解析属性 def show = { def results = PM.createCriteria().l
我在尝试将条目插入包含地理位置数据的表时遇到问题。事实上,该表仅包含一个地理位置列,一种 Point 数据类型。我正在使用 Java JPA 和 MariaDB。最奇怪的部分是,代码曾经可以工作,但应
我正在使用 FluentNHibernate 和 Linq To Nhibernate,以及这些实体(仅相关部分): public class Player : BaseEntity { pr
我使用 NHiberNate 编写了联合查询,但收到 NHibernate.QueryException:not an Association: Id 这就是 NHibernate 库的样子 Tick
我有一个正在尝试执行的 HQL 查询,但我不断收到 QueryException。 我的查询是 SELECT a FROM (SELECT MAX(m.version) maxVersionOp, m
使用oracle DB,我们创建了一个没有PK的表(我们知道这不是一个好的做法,但我们仍然希望有人可以帮助我们),我们需要使用约束作为替代方案。我们能够将数据插入数据库,不幸的是我们无法检索,因为它会
我关闭了 EclipseLink 缓存,因为我正在外部修改数据,并且不希望遇到手动刷新所有内容的麻烦。显然,这是在 persistence.xml 中关闭缓存以避免对象身份问题的正确方法:
使用 JPA 2.0、Java EE 5、Weblogic 10.3 (11g)、JDK 6、EclipseLink。 当我尝试运行它时: CriteriaQuery _criteriaquery
我已经在本地主机中创建了数据库 Illuminate\Database\QueryException : SQLSTATE[HY000] [1045] Access denied for user
我想通过限制在 Hibernate 中的条件添加订单。 我有两个表 Event 和 MainEvent 与一对多的关系(一个 MainEvent 有很多事件)。 final Criteria
我正在尝试使用 Hibernate 连接到数据库。当前表格具有以下布局: (field1, field2, field3, optional1, optional2...) 其中 field1、fie
这是此错误的另一种情况: 21:22:15,881 ERROR [SessionFactoryImpl] Error in named query: ch.software.gvs.TroubleNo
这是我的查询 SQL_QUERY="SELECT review.comment FROM ReviewDO review WHERE title='"+titleName+"'"; 我试图通过使用标题
我有一个 org.hibernate.QueryException,我很难理解为什么。 hibernate 未解析的属性 langue_id 在我的实体 Conditionnement 中定义。 堆栈
我想在laravel中注册时遇到问题。完成注册表后,我看到此错误消息: QueryException in Connection.php line 770:SQLSTATE[HY000] [1045]
这是我尝试处理重复条目的模型代码: $userData = ['name' => $name, 'email' => $email, 'password' => $password]; publ
我在 Laravel 应用程序中工作,并通过 user_id 列将 users 表与 posts 表相关联。 posts 表中已有 user_id 列。但是当我运行 php artisan db:se
我正面临一种奇怪的问题,不知道该往哪个方向解决;-) 我目前正在开发一个 Web 应用程序,用于使用 Hibernate 存储员工。 插入、删除、搜索;一切正常。 但! 每个员工都有一个由字符串列表表
这是我第一次使用JPA。我用 Java 和 JPA 编写代码。我的代码如下。 String queryStr = "SELECT m.title, b.isbn, b.authors"; q
我正在尝试从当前用户获取特定租金的列表。 Controller 中的代码: def index() { if (isLoggedIn()) { String username = getPri
我是一名优秀的程序员,十分优秀!