简体   繁体   English

本机查询可以使用sqlDeveloper,但无法使用休眠模式

[英]Native query works sqlDeveloper but fails using hibernate

i have a query that i want to run it using hibernate native sqlQuery 我有一个查询,我想使用休眠的本机sqlQuery运行它

when i run query using sql developer it works fine but when hibernate run it ,it throws this exception 当我使用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]

this is the query from hibernate logs : 这是来自休眠日志的查询:

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

here's my table class definition : 这是我的表类定义:

@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;

Edit : here's the code where im defining this method and using it 编辑:这是我在其中定义和使用此方法的代码

@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);

Using this method : 使用此方法:

@Override
    public Iterable<Operations> operationsStat(Long centreId) {
        return operationsRepository.operationsStat(centreId);
    }


@GetMapping(value="/statistique")
    @ResponseBody()
    Iterable<Operations> doStatistique()
    {
        return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
    }

Code after changing the return type : 更改返回类型后的代码:

@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<Object> operationsStat( Long centreId);

then this : 然后这个:

@Override
    public Iterable<Object> operationsStat(Long centreId) {
        return operationsRepository.operationsStat(centreId);
    }


@GetMapping(value="/statistique")
    @ResponseBody()
    Iterable<Object> doStatistique()
    {
        return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
    }

There is a field in the Operations class Operations类中有一个字段

private Date dateOperations;

But the query does not return any value for the field. 但是查询不会为该字段返回任何值。 The same for 一样

private Guichet guichet;

No column guichetId in the query and your count(*) has no mapped field in the class. 查询中没有列guichetId,并且您的count(*)在类中没有映射的字段。

It seems that @Query(value="select o.guichet,count(*) from OPERATIONS o .. is returning a List of Guichet objects but you are expecting a List<Operations> . 看来@Query(value="select o.guichet,count(*) from OPERATIONS o ..正在返回Guichet对象的列表,但您期望使用List<Operations>

One options is to create an external pojo for your Operations statics (using for example a package name: com.myorg.model) 一种选择是为Operations静态对象创建外部pojo(例如,使用包名称:com.myorg.model)

package com.myorg.model;
 public class OperationsStas {
  private Guichet guichet;
  private Long   count;

  public OperationsStas(Guichet guichet, Long count) {
    this.guichet = guichet;
    this.count  = count;
  }

} }

The repo could be like this: 回购可能是这样的:

@Repository
public interface OperationsRepository extends CrudRepository<Operations, Long> {

    @Query(value="select new com.myorg.model.com.myorg.model.OperationsStats(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)
    List<OperationsStas> operationsStat( Long centreId);

And also the control have to change 而且控制也必须改变

@Override
    public List<OperationsStas> operationsStat(Long centreId) {
        return operationsRepository.operationsStat(centreId);
    }


@GetMapping(value="/statistique")
    @ResponseBody()
    List<OperationsStas> doStatistique()
    {
        return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
    }

Hope it can help you. 希望它能对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM