本机查询工作sqlDeveloper但使用hibernate失败

时间:2017-08-21 00:53:42

标签: java sql spring hibernate oracle-sqldeveloper

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

运行它

当我使用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日志的查询:

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 个答案:

答案 0 :(得分:1)

更改返回类型后的代码:

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

答案 1 :(得分:0)

Operations类中有一个字段

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Target Name="BuildProjectReferences" BeforeTargets="BuildGenerateSources">
    <MsBuild Projects="@(ProjectReference)"
             Properties="Configuration=$(Configuration);Platform=$(Platform)"/>
  </Target>
</Project>

但查询不返回该字段的任何值。

相同
  ...
  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <!-- this is near the bottom already -->
  <Import Project="..\BuildProjectReferences.targets" /> !<-- add this -->
  ...
</Project>

查询中没有列guichetId,而您的private Date dateOperations; 在类中没有映射字段。

答案 2 :(得分:0)

似乎@Query(value="select o.guichet,count(*) from OPERATIONS o ..正在返回Guichet个对象的列表,但您期望List<Operations>

一个选项是为您的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)); 
    }

希望它可以帮到你。