New @JoinTable导致nativeQuery中的org.hibernate.exception.SQLGrammarException,我们没有头绪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了New @JoinTable导致nativeQuery中的org.hibernate.exception.SQLGrammarException,我们没有头绪相关的知识,希望对你有一定的参考价值。
[这让我们的团队抓紧了头脑,我们不知道如何进行。
概述我们有两个具有独立实体类的订单类型,分别为CashIn和CashOut,并且我们有一个CustomerDetails实体。为避免不必要的重复,我设置了一个[[JoinTable以在需要时查找正确的CustomerDetails。
每个类中有更多条目,但这是与此相关的以下内容。class CashIn
@NamedEntityGraph(name = "Order.CustomerDetails", attributeNodes = @NamedAttributeNode("customerDetails"))
public class Order extends BaseEntity {
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(
name = "order_customer_details",
joinColumns = { @JoinColumn(name = "cashin_order_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "customer_details_id", referencedColumnName = "id") }
)
private CustomerDetails customerDetails;
class CashOut
public class CashOut extends BaseEntity {
/**
* The old approach was no good.
*/
@OneToOne(cascade = CascadeType.ALL)
@JoinTable(
name = "order_customer_details",
joinColumns = { @JoinColumn(name = "cashout_order_id", referencedColumnName = "id") },
inverseJoinColumns = { @JoinColumn(name = "customer_details_id", referencedColumnName = "id") }
)
private CustomerDetails customerDetails;
class CustomerDetails
@Entity
@Table(name = "order_details")
public class CustomerDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id",unique=true, nullable = false)
private Long id;
@Column(name = "name", length = 50)
private String name;
JoinTable
@Entity
@Table(
name = "order_customer_details"
)
public class OrderCustomerDetails extends BaseEntity {
/**
* OneToOne OrderCustomerDetails Bridge
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", columnDefinition = "int")
private Long id;
@Column(name = "cashin_order_id", columnDefinition = "bigint")
private Long cashInOrderId;
@Column(name = "cashout_order_id", columnDefinition = "bigint")
private Long cashOutOrderId;
@Column(name = "customer_details_id", columnDefinition = "bigint")
private Long customerDetailsId;
@CreationTimestamp
@Column(name = "created_date", nullable = false)
private Date createdDate;
所有这些都按预期工作,当检查或CashOut订单时,它会在JoinTable中查找正确的条目,并获取正确的CustomerDetails条目。CashIn
BUT
在我们计划的nativeQuery调用中@Query(nativeQuery = true,
value = "SELECT o.* FROM orders o JOIN vendors v on o.vendor_id = v.id" +
" where date_add(o.last_update_date, interval v.order_notification_delay second) < now() AND o.id > 10000")
List<Order> findPaidOrdersRequiringNotification();
使用示例返回CashIn
2019-11-08 12:43:15,405 ERROR [task-scheduler-4] org.hibernate.engine.jdbc.spi.SqlExceptionHelper: Column 'customer_details_id' not found.
2019-11-08 12:43:21,310 ERROR [task-scheduler-4] org.springframework.integration.handler.LoggingHandler: org.springframework.dao.InvalidDataAccessResourceUsageException: could not execute query; SQL [SELECT o.* FROM orders o JOIN vendors v on o.vendor_id = v.id where date_add(o.last_update_date, interval v.order_notification_delay second) < now() AND o.id > 10000]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute query
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:244)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:488)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:57)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy143.findPaidOrdersRequiringNotification(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:97)
at m6.controller.merchant.SortManipulatingAspect.enableIgnoreCaseSorting(SortManipulatingAspect.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy143.findPaidOrdersRequiringNotification(Unknown Source)
at m6.task.orderpaid.OrderPaidService.processPaidOrders(OrderPaidService.java:52)
at m6.task.orderpaid.OrderPaidService$$FastClassBySpringCGLIB$$307fb413.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673)
at m6.task.orderpaid.OrderPaidService$$EnhancerBySpringCGLIB$$3843faf7.processPaidOrders(<generated>)
at m6.task.orderpaid.OrderPaidTask.perform(OrderPaidTask.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.loader.Loader.doList(Loader.java:2620)
at org.hibernate.loader.Loader.doList(Loader.java:2600)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
at org.hibernate.loader.Loader.list(Loader.java:2424)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:336)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1967)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:322)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:125)
at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:606)
at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:483)
at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:121)
at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:85)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:116)
at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:106)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:483)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:461)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 57 more
Caused by: java.sql.SQLException: Column 'customer_details_id' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:898)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:887)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:861)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1078)
at com.mysql.jdbc.ResultSetImpl.getLong(ResultSetImpl.java:2736)
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63)
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234)
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224)
at org.hibernate.type.ManyToOneType.hydrate(ManyToOneType.java:169)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2790)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1729)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1655)
at org.hibernate.loader.Loader.getRow(Loader.java:1544)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
at org.hibernate.loader.Loader.doQuery(Loader.java:930)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
at org.hibernate.loader.Loader.doList(Loader.java:2617)
... 80 more
经过几天的测试,我可以确定的是,如果查询找到订单,它将呕吐。如果没有任何点击,它将很乐意继续而不会出错。中有null。这唯一可能与我们不同的是,我在
JoinTable
CREATE TABLE `order_customer_details` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`cashin_order_id` bigint(20) unsigned DEFAULT NULL,
`cashout_order_id` bigint(20) unsigned DEFAULT NULL,
`customer_details_id` bigint(20) unsigned DEFAULT NULL,
`created_date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16583 DEFAULT CHARSET=utf8 COMMENT='One to One table to link orders to customer details';
nativeQuery在MySQL Workbench中可以很好地工作。
我对如何继续感到困惑。是否有人建议我检查?
[这让我们的团队抓紧了头脑,我们不知道如何进行。概述我们有两种订单类型,分别具有Cashent和CashOut,具有separte实体类,并且我们有一个实体...
order
表的列的本机查询"SELECT o.* FROM orders o ...
但将这些结果映射到一个更复杂的类上,您的Order
实体上包含复杂的联接,因为查询是本机的,所以休眠不会执行通常在幕后执行的所有联接和魔术。为何查询在MySQL Workbench上有效但在您的应用程序上无效?这意味着,如果您希望本机查询按预期工作,则必须对查询执行所有这些连接,然后指示要使用SqlResultSetMapping或类似方法休眠它如何将这些结果映射到您的类中那。
以上是关于New @JoinTable导致nativeQuery中的org.hibernate.exception.SQLGrammarException,我们没有头绪的主要内容,如果未能解决你的问题,请参考以下文章
从 Hibernate 中的 JoinTable 中检索所有记录
NestJS + TypeORM 中的 JoinTable 问题