如何在使用 Quarkus Panache 的选择查询中使用 LocalTime

huangapple go评论106阅读模式
英文:

How to use LocalTime in select query with Quarkus Panache

问题

以下是翻译好的代码部分:

  1. I try to find my entity `OpeningTimes` for a specific `LocalDateTime`. My entity knows some days of week as Collection and two `LocalTime` as begin and end.
  2. My entity is this:
  3. ```java
  4. package org.something.model;
  5. import ...
  6. @Entity
  7. @JsonIgnoreProperties(
  8. value = {"persistent"},
  9. ignoreUnknown = true)
  10. @RegisterForReflection
  11. public class OpeningTimes extends PanacheEntityBase {
  12. private static final String FIND_OPENINGS_BY_DATETIME_QUERY =
  13. "SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN daysOfWeek AND timeFrom <= ?2 AND timeTo >= ?2";
  14. ...
  15. public static List<OpeningTimes> findByDateTime(LocalDateTime dateTime) {
  16. return find(FIND_OPENINGS_BY_DATETIME_QUERY, dateTime.getDayOfWeek(), dateTime.toLocalTime())
  17. .list();
  18. }
  19. ...
  20. }

When I run my Test which calls the findByDateTime method I get this:

  1. Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  2. WARN: SQL Error: 0, SQLState: 42601
  3. Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  4. ERROR: ERROR: syntax error at or near "{"
  5. Position: 332
  6. ...
  7. Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  8. Position: 332
  9. ...

I searched for some information of how to use panache correctly with LocalDateTime but unfortunately I couldn't find any.

  1. 请注意,我已经根据您的要求进行了翻译,去除了代码和内容中的其他部分。如果您有任何进一步的问题或需要进一步的帮助,请随时提问。
  2. <details>
  3. <summary>英文:</summary>
  4. I try to find my entity `OpeningTimes` for a specific `LocalDateTime`. My entity knows some days of week as Collection and two `LocalTime` as begin and end.
  5. My entity is this:
  6. ```java
  7. package org.something.model;
  8. import com.fasterxml.jackson.annotation.JsonCreator;
  9. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  10. import com.fasterxml.jackson.annotation.JsonProperty;
  11. import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
  12. import io.quarkus.runtime.annotations.RegisterForReflection;
  13. import org.hibernate.annotations.Cascade;
  14. import org.hibernate.annotations.GenericGenerator;
  15. import javax.persistence.ElementCollection;
  16. import javax.persistence.Entity;
  17. import javax.persistence.GeneratedValue;
  18. import javax.persistence.Id;
  19. import javax.validation.constraints.NotNull;
  20. import java.time.DayOfWeek;
  21. import java.time.LocalDateTime;
  22. import java.time.LocalTime;
  23. import java.util.*;
  24. import static org.hibernate.annotations.CascadeType.ALL;
  25. @Entity
  26. @JsonIgnoreProperties(
  27. value = {&quot;persistent&quot;},
  28. ignoreUnknown = true)
  29. @RegisterForReflection
  30. public class OpeningTimes extends PanacheEntityBase {
  31. private static final String FIND_OPENINGS_BY_DATETIME_QUERY =
  32. &quot;SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN daysOfWeek AND timeFrom &lt;= ?2 AND timeTo &gt;= ?2&quot;;
  33. @Id
  34. @GeneratedValue(generator = &quot;UUID&quot;)
  35. @GenericGenerator(name = &quot;UUID&quot;, strategy = &quot;org.hibernate.id.UUIDGenerator&quot;)
  36. private UUID id;
  37. @NotNull private String name;
  38. private String description;
  39. private LocalTime timeFrom;
  40. private LocalTime timeTo;
  41. @ElementCollection
  42. @Cascade(value = {ALL})
  43. private Collection&lt;DayOfWeek&gt; daysOfWeek;
  44. /** DO NOT USE! ONLY FOR JPA! */
  45. public OpeningTimes() {
  46. super();
  47. name = &quot;&quot;;
  48. }
  49. @JsonCreator
  50. public OpeningTimes(
  51. @JsonProperty(&quot;name&quot;) String name,
  52. @JsonProperty(&quot;timeFrom&quot;) LocalTime timeFrom,
  53. @JsonProperty(&quot;timeTo&quot;) LocalTime timeTo,
  54. @JsonProperty(&quot;daysOfWeek&quot;) Collection&lt;DayOfWeek&gt; daysOfWeek) {
  55. this.name = name;
  56. this.timeFrom = timeFrom;
  57. this.timeTo = timeTo;
  58. this.daysOfWeek = new HashSet&lt;&gt;(daysOfWeek);
  59. }
  60. public OpeningTimes(String name, LocalTime from, LocalTime to, DayOfWeek... daysOfWeek) {
  61. this(name, from, to, new ArrayList&lt;&gt;(Arrays.asList(daysOfWeek)));
  62. }
  63. public static List&lt;OpeningTimes&gt; findByDateTime(LocalDateTime dateTime) {
  64. return find(FIND_OPENINGS_BY_DATETIME_QUERY, dateTime.getDayOfWeek(), dateTime.toLocalTime())
  65. .list();
  66. }
  67. public LocalTime getTimeFrom() {
  68. return timeFrom;
  69. }
  70. public void setTimeFrom(LocalTime timeFrom) {
  71. this.timeFrom = timeFrom;
  72. }
  73. public LocalTime getTimeTo() {
  74. return timeTo;
  75. }
  76. public void setTimeTo(LocalTime timeTo) {
  77. this.timeTo = timeTo;
  78. }
  79. public Collection&lt;DayOfWeek&gt; getDaysOfWeek() {
  80. return daysOfWeek;
  81. }
  82. public void setDaysOfWeek(Set&lt;DayOfWeek&gt; daysOfWeek) {
  83. this.daysOfWeek = daysOfWeek;
  84. }
  85. public UUID getId() {
  86. return id;
  87. }
  88. public void setId(UUID id) {
  89. this.id = id;
  90. }
  91. public String getName() {
  92. return name;
  93. }
  94. public void setName(String name) {
  95. this.name = name;
  96. }
  97. public String getDescription() {
  98. return description;
  99. }
  100. public void setDescription(String description) {
  101. this.description = description;
  102. }
  103. @Override
  104. public boolean equals(Object o) {
  105. if (this == o) return true;
  106. if (!(o instanceof OpeningTimes)) return false;
  107. OpeningTimes that = (OpeningTimes) o;
  108. return Objects.equals(getId(), that.getId())
  109. &amp;&amp; Objects.equals(getName(), that.getName())
  110. &amp;&amp; Objects.equals(getDescription(), that.getDescription())
  111. &amp;&amp; Objects.equals(getTimeFrom(), that.getTimeFrom())
  112. &amp;&amp; Objects.equals(getTimeTo(), that.getTimeTo())
  113. &amp;&amp; Objects.equals(getDaysOfWeek(), that.getDaysOfWeek());
  114. }
  115. @Override
  116. public int hashCode() {
  117. return Objects.hash(
  118. getId(), getName(), getDescription(), getTimeFrom(), getTimeTo(), getDaysOfWeek());
  119. }
  120. @Override
  121. public String toString() {
  122. return &quot;OpeningTimes{&quot;
  123. + &quot;id=&quot;
  124. + id
  125. + &quot;, name=&#39;&quot;
  126. + name
  127. + &#39;\&#39;&#39;
  128. + &quot;, description=&#39;&quot;
  129. + description
  130. + &#39;\&#39;&#39;
  131. + &quot;, timeFrom=&quot;
  132. + timeFrom
  133. + &quot;, timeTo=&quot;
  134. + timeTo
  135. + &quot;, daysOfWeek=&quot;
  136. + daysOfWeek
  137. + &#39;}&#39;;
  138. }
  139. public void merge(OpeningTimes openingTimes) {
  140. this.name = openingTimes.name;
  141. this.description = openingTimes.description;
  142. this.timeFrom = openingTimes.timeFrom;
  143. this.timeTo = openingTimes.timeTo;
  144. this.daysOfWeek = openingTimes.daysOfWeek;
  145. }
  146. }

When I run my Test which calls the findByDateTime method I get this:

  1. Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  2. WARN: SQL Error: 0, SQLState: 42601
  3. Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
  4. ERROR: ERROR: syntax error at or near &quot;{&quot;
  5. Position: 332
  6. javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
  7. at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
  8. at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1539)
  9. at org.hibernate.query.Query.getResultList(Query.java:165)
  10. at io.quarkus.hibernate.orm.panache.runtime.PanacheQueryImpl.list(PanacheQueryImpl.java:137)
  11. at eu.wiegandt.openworkshoporganizer.model.OpeningTimes.findByDateTime(OpeningTimes.java:70)
  12. at eu.wiegandt.openworkshoporganizer.OpeningTimesIT.findByDateTime_InRange_TestEvent(OpeningTimesIT.java:38)
  13. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  14. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  15. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  16. at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  17. at io.quarkus.test.junit.QuarkusTestExtension.runExtensionMethod(QuarkusTestExtension.java:342)
  18. at io.quarkus.test.junit.QuarkusTestExtension.interceptTestMethod(QuarkusTestExtension.java:281)
  19. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
  20. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
  21. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
  22. at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
  23. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
  24. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
  25. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
  26. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
  27. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
  28. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
  29. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
  30. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
  31. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
  32. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
  33. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:205)
  34. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  35. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201)
  36. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
  37. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
  38. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
  39. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  40. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
  41. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
  42. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
  43. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  44. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
  45. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
  46. at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
  47. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
  48. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
  49. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  50. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
  51. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
  52. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
  53. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  54. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
  55. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
  56. at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
  57. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
  58. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
  59. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  60. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
  61. at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
  62. at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
  63. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  64. at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
  65. at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
  66. at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
  67. at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
  68. at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
  69. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
  70. at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
  71. at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
  72. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
  73. at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
  74. at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
  75. at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
  76. at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
  77. at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
  78. Suppressed: java.lang.IllegalArgumentException: Removing a detached instance eu.wiegandt.openworkshoporganizer.model.OpeningTimes#f30bcdfe-c67c-441b-af5d-255a6f5ce871
  79. at org.hibernate.event.internal.DefaultDeleteEventListener.disallowDeletionOfDetached(DefaultDeleteEventListener.java:190)
  80. at org.hibernate.event.internal.DefaultDeleteEventListener.performDetachedEntityDeletionCheck(DefaultDeleteEventListener.java:178)
  81. at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:105)
  82. at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:72)
  83. at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102)
  84. at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:894)
  85. at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:826)
  86. at org.hibernate.internal.SessionImpl.remove(SessionImpl.java:3261)
  87. at io.quarkus.hibernate.orm.runtime.entitymanager.TransactionScopedEntityManager.remove(TransactionScopedEntityManager.java:134)
  88. at io.quarkus.hibernate.orm.runtime.entitymanager.ForwardingEntityManager.remove(ForwardingEntityManager.java:37)
  89. at io.quarkus.hibernate.orm.panache.runtime.JpaOperations.delete(JpaOperations.java:60)
  90. at io.quarkus.hibernate.orm.panache.PanacheEntityBase.delete(PanacheEntityBase.java:70)
  91. at eu.wiegandt.openworkshoporganizer.OpeningTimesIT.cleanUpDatabase(OpeningTimesIT.java:49)
  92. at eu.wiegandt.openworkshoporganizer.OpeningTimesIT_Subclass.cleanUpDatabase$$superaccessor6(OpeningTimesIT_Subclass.zig:332)
  93. at eu.wiegandt.openworkshoporganizer.OpeningTimesIT_Subclass$$function$$6.apply(OpeningTimesIT_Subclass$$function$$6.zig:47)
  94. at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
  95. at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:119)
  96. at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:92)
  97. at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:32)
  98. at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:53)
  99. at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:26)
  100. at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired_Bean.intercept(TransactionalInterceptorRequired_Bean.zig:168)
  101. at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
  102. at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
  103. at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
  104. at eu.wiegandt.openworkshoporganizer.OpeningTimesIT_Subclass.cleanUpDatabase(OpeningTimesIT_Subclass.zig:276)
  105. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  106. at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  107. at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  108. at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  109. at io.quarkus.test.junit.QuarkusTestExtension.runExtensionMethod(QuarkusTestExtension.java:342)
  110. at io.quarkus.test.junit.QuarkusTestExtension.interceptAfterEachMethod(QuarkusTestExtension.java:303)
  111. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
  112. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
  113. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
  114. at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
  115. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptLifecycleMethod(TimeoutExtension.java:126)
  116. at org.junit.jupiter.engine.extension.TimeoutExtension.interceptAfterEachMethod(TimeoutExtension.java:108)
  117. at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
  118. at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
  119. at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
  120. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
  121. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
  122. at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
  123. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
  124. at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
  125. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeMethodInExtensionContext(ClassBasedTestDescriptor.java:481)
  126. at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$synthesizeAfterEachMethodAdapter$19(ClassBasedTestDescriptor.java:471)
  127. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachMethods$9(TestMethodTestDescriptor.java:231)
  128. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:268)
  129. at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
  130. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$14(TestMethodTestDescriptor.java:268)
  131. at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
  132. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:267)
  133. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachMethods(TestMethodTestDescriptor.java:229)
  134. at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:141)
  135. ... 41 more
  136. Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
  137. at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:103)
  138. at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
  139. at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
  140. at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
  141. at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:67)
  142. at org.hibernate.loader.Loader.getResultSet(Loader.java:2292)
  143. at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2050)
  144. at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2012)
  145. at org.hibernate.loader.Loader.doQuery(Loader.java:953)
  146. at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
  147. at org.hibernate.loader.Loader.doList(Loader.java:2838)
  148. at org.hibernate.loader.Loader.doList(Loader.java:2820)
  149. at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2652)
  150. at org.hibernate.loader.Loader.list(Loader.java:2647)
  151. at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
  152. at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396)
  153. at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
  154. at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1404)
  155. at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1562)
  156. at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1530)
  157. ... 69 more
  158. Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near &quot;{&quot;
  159. Position: 332
  160. at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578)
  161. at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313)
  162. at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331)
  163. at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
  164. at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
  165. at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159)
  166. at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:109)
  167. at io.agroal.pool.wrapper.PreparedStatementWrapper.executeQuery(PreparedStatementWrapper.java:75)
  168. at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57)
  169. ... 84 more
  170. Apr 10, 2020 2:02:11 PM io.quarkus.runtime.Timing printStopTime
  171. INFO: Quarkus stopped in 0.031s

I searched for some information of how to use panache correctly with LocalDateTime but unfortunately I couldn't find any.

答案1

得分: 1

我可以通过调试并查找Hibernate生成的查询来找到错误。
我找到了这个查询:

  1. select distinct openingtim0_.id as id1_1_, openingtim0_.description as descript2_1_, openingtim0_.name as name3_1_, openingtim0_.timeFrom as timefrom4_1_, openingtim0_.timeTo as timeto5_1_ from OpeningTimes openingtim0_ cross join OpeningTimes_daysOfWeek daysofweek1_ where openingtim0_.id=daysofweek1_.OpeningTimes_id and (? in ({non-qualified-property-ref})) and (? between openingtim0_.timeFrom and openingtim0_.timeTo)

在这个时候,我的查询是:

  1. SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN daysOfWeek AND ?2 BETWEEN timeFrom AND timeTo

所以错误不是与LocalTime有关,而是与天数的集合有关。

所以我通过将我的查询更改为:

  1. SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN elements(openings.daysOfWeek) AND ?2 BETWEEN timeFrom AND timeTo

现在它正常工作了 如何在使用 Quarkus Panache 的选择查询中使用 LocalTime

英文:

I could find the error by debugging and looking for what query hibernate creates.
I found this query:

  1. select distinct openingtim0_.id as id1_1_, openingtim0_.description as descript2_1_, openingtim0_.name as name3_1_, openingtim0_.timeFrom as timefrom4_1_, openingtim0_.timeTo as timeto5_1_ from OpeningTimes openingtim0_ cross join OpeningTimes_daysOfWeek daysofweek1_ where openingtim0_.id=daysofweek1_.OpeningTimes_id and (? in ({non-qualified-property-ref})) and (? between openingtim0_.timeFrom and openingtim0_.timeTo)

at this moment my query was:

  1. SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN daysOfWeek AND ?2 BETWEEN timeFrom AND timeTo

So the error isn't something with the LocalTimebut with the collection of days.

So I fixed it by changing my query to:

  1. SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN elements(openings.daysOfWeek) AND ?2 BETWEEN timeFrom AND timeTo

Now it works 如何在使用 Quarkus Panache 的选择查询中使用 LocalTime

huangapple
  • 本文由 发表于 2020年4月10日 20:10:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/61139963.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定