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

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

How to use LocalTime in select query with Quarkus Panache

问题

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

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.

My entity is this:
```java
package org.something.model;

import ...

@Entity
@JsonIgnoreProperties(
    value = {"persistent"},
    ignoreUnknown = true)
@RegisterForReflection
public class OpeningTimes extends PanacheEntityBase {
  private static final String FIND_OPENINGS_BY_DATETIME_QUERY =
      "SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN daysOfWeek AND timeFrom <= ?2 AND timeTo >= ?2";

  ...

  public static List<OpeningTimes> findByDateTime(LocalDateTime dateTime) {
    return find(FIND_OPENINGS_BY_DATETIME_QUERY, dateTime.getDayOfWeek(), dateTime.toLocalTime())
        .list();
  }

  ...

}

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

Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42601
Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: syntax error at or near "{"
  Position: 332

...

Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "{"
  Position: 332
  ...

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


请注意,我已经根据您的要求进行了翻译,去除了代码和内容中的其他部分。如果您有任何进一步的问题或需要进一步的帮助,请随时提问。

<details>
<summary>英文:</summary>

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. 

My entity is this:
```java
package org.something.model;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.quarkus.hibernate.orm.panache.PanacheEntityBase;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;

import static org.hibernate.annotations.CascadeType.ALL;

@Entity
@JsonIgnoreProperties(
    value = {&quot;persistent&quot;},
    ignoreUnknown = true)
@RegisterForReflection
public class OpeningTimes extends PanacheEntityBase {
  private static final String FIND_OPENINGS_BY_DATETIME_QUERY =
      &quot;SELECT DISTINCT openings FROM OpeningTimes openings WHERE ?1 IN daysOfWeek AND timeFrom &lt;= ?2 AND timeTo &gt;= ?2&quot;;

  @Id
  @GeneratedValue(generator = &quot;UUID&quot;)
  @GenericGenerator(name = &quot;UUID&quot;, strategy = &quot;org.hibernate.id.UUIDGenerator&quot;)
  private UUID id;

  @NotNull private String name;
  private String description;
  private LocalTime timeFrom;
  private LocalTime timeTo;

  @ElementCollection
  @Cascade(value = {ALL})
  private Collection&lt;DayOfWeek&gt; daysOfWeek;

  /** DO NOT USE! ONLY FOR JPA! */
  public OpeningTimes() {
    super();
    name = &quot;&quot;;
  }

  @JsonCreator
  public OpeningTimes(
      @JsonProperty(&quot;name&quot;) String name,
      @JsonProperty(&quot;timeFrom&quot;) LocalTime timeFrom,
      @JsonProperty(&quot;timeTo&quot;) LocalTime timeTo,
      @JsonProperty(&quot;daysOfWeek&quot;) Collection&lt;DayOfWeek&gt; daysOfWeek) {
    this.name = name;
    this.timeFrom = timeFrom;
    this.timeTo = timeTo;
    this.daysOfWeek = new HashSet&lt;&gt;(daysOfWeek);
  }

  public OpeningTimes(String name, LocalTime from, LocalTime to, DayOfWeek... daysOfWeek) {
    this(name, from, to, new ArrayList&lt;&gt;(Arrays.asList(daysOfWeek)));
  }

  public static List&lt;OpeningTimes&gt; findByDateTime(LocalDateTime dateTime) {
    return find(FIND_OPENINGS_BY_DATETIME_QUERY, dateTime.getDayOfWeek(), dateTime.toLocalTime())
        .list();
  }

  public LocalTime getTimeFrom() {
    return timeFrom;
  }

  public void setTimeFrom(LocalTime timeFrom) {
    this.timeFrom = timeFrom;
  }

  public LocalTime getTimeTo() {
    return timeTo;
  }

  public void setTimeTo(LocalTime timeTo) {
    this.timeTo = timeTo;
  }

  public Collection&lt;DayOfWeek&gt; getDaysOfWeek() {
    return daysOfWeek;
  }

  public void setDaysOfWeek(Set&lt;DayOfWeek&gt; daysOfWeek) {
    this.daysOfWeek = daysOfWeek;
  }

  public UUID getId() {
    return id;
  }

  public void setId(UUID id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getDescription() {
    return description;
  }

  public void setDescription(String description) {
    this.description = description;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof OpeningTimes)) return false;
    OpeningTimes that = (OpeningTimes) o;
    return Objects.equals(getId(), that.getId())
        &amp;&amp; Objects.equals(getName(), that.getName())
        &amp;&amp; Objects.equals(getDescription(), that.getDescription())
        &amp;&amp; Objects.equals(getTimeFrom(), that.getTimeFrom())
        &amp;&amp; Objects.equals(getTimeTo(), that.getTimeTo())
        &amp;&amp; Objects.equals(getDaysOfWeek(), that.getDaysOfWeek());
  }

  @Override
  public int hashCode() {
    return Objects.hash(
        getId(), getName(), getDescription(), getTimeFrom(), getTimeTo(), getDaysOfWeek());
  }

  @Override
  public String toString() {
    return &quot;OpeningTimes{&quot;
        + &quot;id=&quot;
        + id
        + &quot;, name=&#39;&quot;
        + name
        + &#39;\&#39;&#39;
        + &quot;, description=&#39;&quot;
        + description
        + &#39;\&#39;&#39;
        + &quot;, timeFrom=&quot;
        + timeFrom
        + &quot;, timeTo=&quot;
        + timeTo
        + &quot;, daysOfWeek=&quot;
        + daysOfWeek
        + &#39;}&#39;;
  }

  public void merge(OpeningTimes openingTimes) {
    this.name = openingTimes.name;
    this.description = openingTimes.description;
    this.timeFrom = openingTimes.timeFrom;
    this.timeTo = openingTimes.timeTo;
    this.daysOfWeek = openingTimes.daysOfWeek;
  }
}

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

Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 0, SQLState: 42601
Apr 10, 2020 2:02:11 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ERROR: syntax error at or near &quot;{&quot;
  Position: 332


javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

	at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:154)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1539)
	at org.hibernate.query.Query.getResultList(Query.java:165)
	at io.quarkus.hibernate.orm.panache.runtime.PanacheQueryImpl.list(PanacheQueryImpl.java:137)
	at eu.wiegandt.openworkshoporganizer.model.OpeningTimes.findByDateTime(OpeningTimes.java:70)
	at eu.wiegandt.openworkshoporganizer.OpeningTimesIT.findByDateTime_InRange_TestEvent(OpeningTimesIT.java:38)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.test.junit.QuarkusTestExtension.runExtensionMethod(QuarkusTestExtension.java:342)
	at io.quarkus.test.junit.QuarkusTestExtension.interceptTestMethod(QuarkusTestExtension.java:281)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestableMethod(TimeoutExtension.java:140)
	at org.junit.jupiter.engine.extension.TimeoutExtension.interceptTestMethod(TimeoutExtension.java:84)
	at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
	at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
	at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeTestMethod$6(TestMethodTestDescriptor.java:205)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeTestMethod(TestMethodTestDescriptor.java:201)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:137)
	at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:71)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:135)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.invokeAll(SameThreadHierarchicalTestExecutorService.java:38)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$5(NodeTestTask.java:139)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$7(NodeTestTask.java:125)
	at org.junit.platform.engine.support.hierarchical.Node.around(Node.java:135)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.lambda$executeRecursively$8(NodeTestTask.java:123)
	at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.executeRecursively(NodeTestTask.java:122)
	at org.junit.platform.engine.support.hierarchical.NodeTestTask.execute(NodeTestTask.java:80)
	at org.junit.platform.engine.support.hierarchical.SameThreadHierarchicalTestExecutorService.submit(SameThreadHierarchicalTestExecutorService.java:32)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor.execute(HierarchicalTestExecutor.java:57)
	at org.junit.platform.engine.support.hierarchical.HierarchicalTestEngine.execute(HierarchicalTestEngine.java:51)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:248)
	at org.junit.platform.launcher.core.DefaultLauncher.lambda$execute$5(DefaultLauncher.java:211)
	at org.junit.platform.launcher.core.DefaultLauncher.withInterceptedStreams(DefaultLauncher.java:226)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:199)
	at org.junit.platform.launcher.core.DefaultLauncher.execute(DefaultLauncher.java:132)
	at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
	Suppressed: java.lang.IllegalArgumentException: Removing a detached instance eu.wiegandt.openworkshoporganizer.model.OpeningTimes#f30bcdfe-c67c-441b-af5d-255a6f5ce871
		at org.hibernate.event.internal.DefaultDeleteEventListener.disallowDeletionOfDetached(DefaultDeleteEventListener.java:190)
		at org.hibernate.event.internal.DefaultDeleteEventListener.performDetachedEntityDeletionCheck(DefaultDeleteEventListener.java:178)
		at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:105)
		at org.hibernate.event.internal.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:72)
		at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:102)
		at org.hibernate.internal.SessionImpl.fireDelete(SessionImpl.java:894)
		at org.hibernate.internal.SessionImpl.delete(SessionImpl.java:826)
		at org.hibernate.internal.SessionImpl.remove(SessionImpl.java:3261)
		at io.quarkus.hibernate.orm.runtime.entitymanager.TransactionScopedEntityManager.remove(TransactionScopedEntityManager.java:134)
		at io.quarkus.hibernate.orm.runtime.entitymanager.ForwardingEntityManager.remove(ForwardingEntityManager.java:37)
		at io.quarkus.hibernate.orm.panache.runtime.JpaOperations.delete(JpaOperations.java:60)
		at io.quarkus.hibernate.orm.panache.PanacheEntityBase.delete(PanacheEntityBase.java:70)
		at eu.wiegandt.openworkshoporganizer.OpeningTimesIT.cleanUpDatabase(OpeningTimesIT.java:49)
		at eu.wiegandt.openworkshoporganizer.OpeningTimesIT_Subclass.cleanUpDatabase$$superaccessor6(OpeningTimesIT_Subclass.zig:332)
		at eu.wiegandt.openworkshoporganizer.OpeningTimesIT_Subclass$$function$$6.apply(OpeningTimesIT_Subclass$$function$$6.zig:47)
		at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
		at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:119)
		at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:92)
		at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:32)
		at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:53)
		at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:26)
		at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired_Bean.intercept(TransactionalInterceptorRequired_Bean.zig:168)
		at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
		at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
		at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
		at eu.wiegandt.openworkshoporganizer.OpeningTimesIT_Subclass.cleanUpDatabase(OpeningTimesIT_Subclass.zig:276)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
		at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
		at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
		at java.base/java.lang.reflect.Method.invoke(Method.java:566)
		at io.quarkus.test.junit.QuarkusTestExtension.runExtensionMethod(QuarkusTestExtension.java:342)
		at io.quarkus.test.junit.QuarkusTestExtension.interceptAfterEachMethod(QuarkusTestExtension.java:303)
		at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
		at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
		at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
		at org.junit.jupiter.engine.extension.TimeoutExtension.intercept(TimeoutExtension.java:149)
		at org.junit.jupiter.engine.extension.TimeoutExtension.interceptLifecycleMethod(TimeoutExtension.java:126)
		at org.junit.jupiter.engine.extension.TimeoutExtension.interceptAfterEachMethod(TimeoutExtension.java:108)
		at org.junit.jupiter.engine.execution.ExecutableInvoker$ReflectiveInterceptorCall.lambda$ofVoidMethod$0(ExecutableInvoker.java:115)
		at org.junit.jupiter.engine.execution.ExecutableInvoker.lambda$invoke$0(ExecutableInvoker.java:105)
		at org.junit.jupiter.engine.execution.InvocationInterceptorChain$InterceptedInvocation.proceed(InvocationInterceptorChain.java:106)
		at org.junit.jupiter.engine.execution.InvocationInterceptorChain.proceed(InvocationInterceptorChain.java:64)
		at org.junit.jupiter.engine.execution.InvocationInterceptorChain.chainAndInvoke(InvocationInterceptorChain.java:45)
		at org.junit.jupiter.engine.execution.InvocationInterceptorChain.invoke(InvocationInterceptorChain.java:37)
		at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:104)
		at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:98)
		at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.invokeMethodInExtensionContext(ClassBasedTestDescriptor.java:481)
		at org.junit.jupiter.engine.descriptor.ClassBasedTestDescriptor.lambda$synthesizeAfterEachMethodAdapter$19(ClassBasedTestDescriptor.java:471)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAfterEachMethods$9(TestMethodTestDescriptor.java:231)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$13(TestMethodTestDescriptor.java:268)
		at org.junit.platform.engine.support.hierarchical.ThrowableCollector.execute(ThrowableCollector.java:73)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.lambda$invokeAllAfterMethodsOrCallbacks$14(TestMethodTestDescriptor.java:268)
		at java.base/java.util.ArrayList.forEach(ArrayList.java:1540)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAllAfterMethodsOrCallbacks(TestMethodTestDescriptor.java:267)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.invokeAfterEachMethods(TestMethodTestDescriptor.java:229)
		at org.junit.jupiter.engine.descriptor.TestMethodTestDescriptor.execute(TestMethodTestDescriptor.java:141)
		... 41 more
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
	at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:103)
	at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:113)
	at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:99)
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:67)
	at org.hibernate.loader.Loader.getResultSet(Loader.java:2292)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2050)
	at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:2012)
	at org.hibernate.loader.Loader.doQuery(Loader.java:953)
	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
	at org.hibernate.loader.Loader.doList(Loader.java:2838)
	at org.hibernate.loader.Loader.doList(Loader.java:2820)
	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2652)
	at org.hibernate.loader.Loader.list(Loader.java:2647)
	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:506)
	at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:396)
	at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:219)
	at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1404)
	at org.hibernate.query.internal.AbstractProducedQuery.doList(AbstractProducedQuery.java:1562)
	at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1530)
	... 69 more
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near &quot;{&quot;
  Position: 332
	at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2578)
	at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2313)
	at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:331)
	at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:448)
	at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:369)
	at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:159)
	at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:109)
	at io.agroal.pool.wrapper.PreparedStatementWrapper.executeQuery(PreparedStatementWrapper.java:75)
	at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:57)
	... 84 more


Apr 10, 2020 2:02:11 PM io.quarkus.runtime.Timing printStopTime
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生成的查询来找到错误。
我找到了这个查询:

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)

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

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

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

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

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:

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:

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:

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:

确定