我有我想要使用Hibernate原生的SQLQuery來運行一個查詢本地查詢工作的SQLDeveloper但無法使用Hibernate
當我使用SQL Developer將它工作正常,但是當Hibernate運行它時,它拋出該異常運行查詢
java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3319) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1926) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
at oracle.jdbc.driver.OracleResultSet.getLong(OracleResultSet.java:1575) ~[ojdbc14-10.2.0.4.0.jar:Oracle JDBC Driver version - "10.2.0.4.0"]
at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:789) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.loader.Loader.processResultSet(Loader.java:972) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.loader.Loader.doQuery(Loader.java:930) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
這是從休眠日誌查詢:
Hibernate: select o.GUICHET,count(*) from OPERATIONS o, Guichet g, Centre c where DATE_OPERATIONS between trunc(sysdate,'mm') and add_months(trunc(sysdate,'mm'),1) and c.centre_id=? and c.centre_id=g.centre_id and g.GUICHET_ID=o.GUICHET group by o.GUICHET
這裏是我的表類定義:
@Entity
@Table(name = "operations")
public class Operations implements Serializable {
private static final long serialVersionUID = 1L;
@GeneratedValue(strategy= GenerationType.AUTO)
@Id
private Long operationsId;
private Date dateOperations;
@ManyToOne()
@JoinColumn(name = "guichet", referencedColumnName = "guichetId")
private Guichet guichet;
編輯:這裏的地方IM定義此方法,並使用它
@Repository
public interface OperationsRepository extends CrudRepository<Operations, Long> {
@Query(value="select o.guichet,count(*) from OPERATIONS o, Guichet g, Centre c where DATE_OPERATIONS between trunc(sysdate,'mm') and add_months(trunc(sysdate,'mm'),1) " +
"and c.centre_id=?1 and c.centre_id=g.centre_id and g.GUICHET_ID=o.GUICHET " +
"group by o.guichet",nativeQuery=true)
Iterable<Operations> operationsStat(Long centreId);
使用此方法的代碼:
@Override
public Iterable<Operations> operationsStat(Long centreId) {
return operationsRepository.operationsStat(centreId);
}
@GetMapping(value="/statistique")
@ResponseBody()
Iterable<Operations> doStatistique()
{
return operationsServiceImpl.operationsStat(new Long(selectedCentre));
}
'值java.sql.SQLException:無效的列name'意味着你的查詢工作確定,但你的resulset是沒有獲取列名稱,因爲該列名稱在查詢中不存在。也許錯誤來自代碼中您嘗試讀取來自自定義查詢的值的部分。你能否提供那個你讀取來自查詢的值的部分? –
在此之前,如果我在select和group by中提供表格列名稱的其餘部分,則查詢將工作並返回結果,但我不會得到我想要的結果。 – kRUMPEZ
即時通訊只是簡單地將這個可迭代的方法返回給Ajax調用,所以我可以循環扔它。沒有訪問java中的任何值 – kRUMPEZ