Spring Data DSL查询中的Hibernate SQLGrammarException

时间:2014-08-15 17:15:47

标签: hibernate jpa spring-data-jpa

我应该进行一个查询,选择合作伙伴拥有的所有终端所做的所有交易:选择合作伙伴的条件是他的终端有一个特定的终端。

我从Hibernate查询中收到此异常:

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

这是查询:

@Query(value = "SELECT T FROM Transaction as T WHERE T.terminal IN (select P.terminals FROM Partner P WHERE :terminal IN (P.terminals))  AND T.transactionShopCloseAt IS NULL")
public List <Transaction> getAllTransactionsWithoutClosureForTerminal(@Param("terminal")Terminal terminal);

这些是涉及的课程:

TRANSACTION:

@Entity
@Table(name = "TRANSACTION")
@GenericGenerator(name = "pk_id_generator", strategy = "uuid2")
public class Transaction extends Identifiable<String, Long> {

@Column(name = "SHOP_CLOSURE_AT")
@Temporal(TemporalType.TIMESTAMP)
private Date transactionShopCloseAt;

@ManyToOne
@JoinColumn(name = "TERMINAL_ID", referencedColumnName = "ID")
private Terminal terminal;
}

PARTNER

@Entity
@Table(name = "PARTNER")
@GenericGenerator(name = "pk_id_generator", strategy = "uuid2")
public class Partner extends Identifiable<String, Long> {

@NotEmpty
@Column(name = "NAME", nullable = false)
private String name;

@OneToMany(mappedBy = "owner")
private Set<Terminal> terminals = new HashSet<>();

TERMINAL

@Entity
@Table(name = "TERMINAL")
@GenericGenerator(name = "pk_id_generator", strategy = "uuid2")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)    
public class Terminal extends Identifiable<String, Long> {

@NotNull
@Column(name = "TERMINAL_ID", nullable = false, length = 16)
private String terminalID;

@NotNull
@Column(name = "TERMINAL_HW_ID", nullable = false, length = 64)
private String terminalHWID;

@ManyToOne //(cascade= CascadeType.ALL)
@JoinColumn(name = "PARTNER_ID", referencedColumnName = "ID")
private Partner owner;

MAPPED SUPERCLASS IDENTIFIABLE:

@MappedSuperclass
public abstract class Identifiable<I extends Serializable, V>  {

@Id
@GeneratedValue(generator = "pk_id_generator")
@Column(name = "ID", nullable = false, length = 36)
private I ID;

public I getID() {
    return ID;
}

你知道可以做什么以及应该如何做HQuery?谢谢你!

例外:

org.apache.cxf.interceptor.Fault: org.hibernate.exception.SQLGrammarException: could not extract ResultSet; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123) 
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "."
Position: 683
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:559)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:302)
at sun.reflect.GeneratedMethodAccessor213.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at     org.postgresql.ds.jdbc23.AbstractJdbc23PooledConnection$StatementHandler.invoke(AbstractJdbc23PooledConnection.java:453)
at com.sun.proxy.$Proxy18.executeQuery(Unknown Source)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:80)
... 128 more

1 个答案:

答案 0 :(得分:1)

WHERE :terminal IN (P.terminals)

无效。替换为

WHERE :terminal member of P.terminals

AFAIK,

T.terminal IN (select P.terminals FROM Partner P

也无效。替换为

T.terminal.id in (select terminal.id from Partner P inner join P.terminals terminal