有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java本机查询在sqlDeveloper中工作,但在使用hibernate时失败

我有一个查询,我想使用hibernate native sqlQuery运行它

当我使用SQLDeveloper运行查询时,它工作正常,但当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日志的查询:

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;

编辑:这是我定义和使用这个方法的代码

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

共 (3) 个答案

  1. # 1 楼答案

    Operations类中有一个字段

    private Date dateOperations;
    

    但是查询不会为字段返回任何值。同样的

    private Guichet guichet;
    

    查询中没有列guichetId,并且count(*)在类中没有映射字段

  2. # 2 楼答案

    更改退货类型后的代码:

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

    那么这个,

    @Override
        public Iterable<Object> operationsStat(Long centreId) {
            return operationsRepository.operationsStat(centreId);
        }
    
    
    @GetMapping(value="/statistique")
        @ResponseBody()
        Iterable<Object> doStatistique()
        {
            return operationsServiceImpl.operationsStat(new Long(selectedCentre)); 
        }
    
  3. # 3 楼答案

    似乎@Query(value="select o.guichet,count(*) from OPERATIONS o ..正在返回一个Guichet对象列表,但您需要一个List<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;
      }
    

    }

    回购协议可以是这样的:

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

    控制也必须改变

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

    希望它能帮助你