我有课
@SuppressWarnings("JpaQlInspection")
@Entity
@Table(name = "tickets", indexes = {
@Index(name = "cancelation_Date", columnList = "cancelation_Date"),
@Index(name = "pos_id", columnList = "pos_id"),
@Index(name = "flag", columnList = "flag")
})
@SqlResultSetMapping(
name = "TicketMapping",
entities = @EntityResult(
entityClass = Ticket.class,
fields = {
@FieldResult(name = "id", column = "id"),
@FieldResult(name = "version", column = "version"),
@FieldResult(name = "pos_id", column = "pos_id"),
@FieldResult(name = "cancelation_date", column = "cancelation_date"),
@FieldResult(name = "cost", column = "cost"),
@FieldResult(name = "flag", column = "flag"),
}
)
)
public class Ticket extends BaseEntity {
@Column(name = "pos_id", length = 12)
private String posId;
@Column(name = "cost", length = 10)
private String cost;
@Column(name = "cancelation_date")
private String cancelationDate;
@Column(name = "flag", length = 10)
private String flag;
public String getPosId() {
return posId;
}
public void setPosId(String posId) {
this.posId = posId;
}
public String getCost() {
return cost;
}
public void setCost(String cost) {
this.cost = cost;
}
public String getCancelationDate() {
return cancelationDate;
}
public void setCancelationDate(String cancelationDate) {
this.cancelationDate = cancelationDate;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
}
@MappedSuperclass
public abstract class BaseEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "generator")
@TableGenerator(name = "generator", table = "sequence_list", pkColumnName = "name", allocationSize = 50, initialValue = 3000)
@Column(name = "id")
protected Long id;
@Version
private Long version;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public boolean isNew() {
return (this.id == null);
}
}
我使用的是postgres 9.6。我在以下时遇到错误:
Query query = this.entityManager.createNativeQuery(searchQuery,"TicketMapping");
return query.getResultList();
错误日志:
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1419) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.query.Query.getResultList(Query.java:146) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
at org.springframework.orm.jpa.SharedEntityManagerCreator$DeferredQueryInvocationHandler.invoke(SharedEntityManagerCreator.java:372) ~[spring-orm-4.3.7.RELEASE.jar:4.3.7.RELEASE]
at com.sun.proxy.$Proxy39.getResultList(Unknown Source) ~[na:na]
at vn.com.bgt.lottery.dao.jpa.JpaTicketDao.search(JpaTicketDao.java:29) ~[classes/:na]
at vn.com.bgt.lottery.TicketSearchUtil.search(TicketSearchUtil.java:89) ~[classes/:na]
at vn.com.bgt.lottery.Console.exportTickets(Console.java:72) [classes/:na]
at vn.com.bgt.lottery.Console.main(Console.java:59) [classes/:na]
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:111) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2618) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2598) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2430) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.list(Loader.java:2425) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2153) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:991) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:147) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1410) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
... 11 common frames omitted
Caused by: org.postgresql.util.PSQLException: The column name id was not found in this ResultSet.
at org.postgresql.jdbc.PgResultSet.findColumn(PgResultSet.java:2608) ~[postgresql-42.0.0.jre6.jar:42.0.0.jre6]
at org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2505) ~[postgresql-42.0.0.jre6.jar:42.0.0.jre6]
at com.mchange.v2.c3p0.impl.NewProxyResultSet.getLong(NewProxyResultSet.java:2625) ~[c3p0-0.9.1.2.jar:0.9.1.2]
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:260) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:256) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:246) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:332) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:802) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.java:985) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:943) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:349) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
at org.hibernate.loader.Loader.doList(Loader.java:2615) ~[hibernate-core-5.2.8.Final.jar:5.2.8.Final]
... 19 common frames omitted
答案 0 :(得分:0)
您的查询语法不正确:
Query query = this.entityManager.createNativeQuery(searchQuery, "TicketMapping");
//--------------------------------------------------------------------^^
//This should be the ObjectName.class
所以你的查询应该是这样的:
Query query = this.entityManager.createNativeQuery("YourQuery", ObjectName.class);
或者只是:
Query query = this.entityManager.createNativeQuery("YourQuery");
List<ObjectName> list = query.getResultList();
return list;
答案 1 :(得分:0)
更改您的结果集:
@SqlResultSetMapping(
name = "TicketMapping",
entities = @EntityResult(
entityClass = Ticket.class,
fields = {
@FieldResult(name = "version", column = "version"),
@FieldResult(name = "pos_id", column = "pos_id"),
@FieldResult(name = "cancelation_date", column = "cancelation_date"),
@FieldResult(name = "cost", column = "cost"),
@FieldResult(name = "flag", column = "flag"),
}
)
)
因为您错误告诉您ID存在问题。我认为现在这将解决它。