Java/JPA:带连接的命名本机查询抛出异常
Java/JPA: Named Native Query with Join throws Exception
在我的 Benutzer-Entity
我有以下 OneToMany
关系:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="BENUTZER_ID")
private List<Dokument> dokumente = new ArrayList<>();
在我的 Dokument-Entity
中,我现在想通过搜索 hash-value
和 foreign-key (BenutzerID)
来获得特定的 Dokument
- 我正在尝试这样做这是 NamedNativeQuery
:
@NamedNativeQueries({
@NamedNativeQuery(
name="findDokumentByHashAndBenutzerID",
query="SELECT d.* FROM Dokument d "
+ "JOIN Benutzer b ON d.Benutzer_id = b.Id_Benutzer WHERE d.Benutzer_id = ? AND d.Hash = ?")
})
当我在我的数据库 (H2) 中使用此查询时它有效,但在 Java 中我得到以下异常:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.h2.jdbc.JdbcSQLException: Syntax Fehler in SQL Befehl "FINDDOKUMENTBYHASHANDBENUTZERID[] "; erwartet "FROM, {"
Syntax error in SQL statement "FINDDOKUMENTBYHASHANDBENUTZERID[] "; expected "FROM, {"; SQL statement:
findDokumentByHashAndBenutzerID [42001-197]
Error Code: 42001
Call: findDokumentByHashAndBenutzerID
Query: ReadAllQuery(referenceClass=Dokument sql="findDokumentByHashAndBenutzerID")
at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:377)
at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260)
at org.eclipse.persistence.internal.jpa.QueryImpl.getSingleResult(QueryImpl.java:516)
at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:400)
at ch.insidebfh.persistence.repository.Repository.findByHash(Repository.java:91)
at ch.insidebfh.application.service.ServiceHelper.findByHash(ServiceHelper.java:18)
at ch.insidebfh.application.demo.Sandbox.lambda[=20=](Sandbox.java:92)
at java.util.ArrayList.forEach(Unknown Source)
at ch.insidebfh.application.demo.Sandbox.run(Sandbox.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
根据异常情况我猜你会打电话给
entityManager.createQuery("findDokumentByHashAndBenutzerID");
而不是
entityManager.createNamedQuery("findDokumentByHashAndBenutzerID");
在我的 Benutzer-Entity
我有以下 OneToMany
关系:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval=true)
@JoinColumn(name="BENUTZER_ID")
private List<Dokument> dokumente = new ArrayList<>();
在我的 Dokument-Entity
中,我现在想通过搜索 hash-value
和 foreign-key (BenutzerID)
来获得特定的 Dokument
- 我正在尝试这样做这是 NamedNativeQuery
:
@NamedNativeQueries({
@NamedNativeQuery(
name="findDokumentByHashAndBenutzerID",
query="SELECT d.* FROM Dokument d "
+ "JOIN Benutzer b ON d.Benutzer_id = b.Id_Benutzer WHERE d.Benutzer_id = ? AND d.Hash = ?")
})
当我在我的数据库 (H2) 中使用此查询时它有效,但在 Java 中我得到以下异常:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.h2.jdbc.JdbcSQLException: Syntax Fehler in SQL Befehl "FINDDOKUMENTBYHASHANDBENUTZERID[] "; erwartet "FROM, {" Syntax error in SQL statement "FINDDOKUMENTBYHASHANDBENUTZERID[] "; expected "FROM, {"; SQL statement: findDokumentByHashAndBenutzerID [42001-197] Error Code: 42001 Call: findDokumentByHashAndBenutzerID Query: ReadAllQuery(referenceClass=Dokument sql="findDokumentByHashAndBenutzerID") at org.eclipse.persistence.internal.jpa.QueryImpl.getDetailedException(QueryImpl.java:377) at org.eclipse.persistence.internal.jpa.QueryImpl.executeReadQuery(QueryImpl.java:260) at org.eclipse.persistence.internal.jpa.QueryImpl.getSingleResult(QueryImpl.java:516) at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:400) at ch.insidebfh.persistence.repository.Repository.findByHash(Repository.java:91) at ch.insidebfh.application.service.ServiceHelper.findByHash(ServiceHelper.java:18) at ch.insidebfh.application.demo.Sandbox.lambda[=20=](Sandbox.java:92) at java.util.ArrayList.forEach(Unknown Source) at ch.insidebfh.application.demo.Sandbox.run(Sandbox.java:91) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.DatabaseException
根据异常情况我猜你会打电话给
entityManager.createQuery("findDokumentByHashAndBenutzerID");
而不是
entityManager.createNamedQuery("findDokumentByHashAndBenutzerID");