1

关系:

公司有地址。这就是我正在测试这个简单示例的全部内容。

这是具有 SqlResultSetMappings 的地址实体。

@Entity
@Table(name="addresses")
@SqlResultSetMappings({
       @SqlResultSetMapping(name = "addresses.simpleQuery", entities = {
               @EntityResult(entityClass = Company.class,
                       fields = {
                            @FieldResult(name = "id", column = "company_id")
                       }
               ),
               @EntityResult(entityClass = Address.class)
       })
})
public class Address {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO)
     private Integer id;

      @Version
      private Integer version;
      private Long createdAt;
      private Long updatedAt;
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name="company_id")
      private Company company;

      //some other fields like city, state etc

      //getters and setters
}

这是公司实体

@Entity
@Table(name="companies")
public class Company {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    @Version
    private Integer version;
    private Long createdAt;
    private Long updatedAt;

    @OneToMany(mappedBy = "company", fetch = FetchType.LAZY)
    private Set<Address> addressSet;    

    //some other fields like name

    //getters and setters
}

希望是执行一个简单的本机查询并像这样映射结果集:

List queryResult = entityManager.createNativeQuery("
select c.id as company_id, name, a.id as id 
from companies c, addresses a 
where c.id=a.company_id limit 3", "addresses.simpleQuery").getResultList();

瞧……我执行了查询,结果爆炸了

Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
    at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
    at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
    at org.hibernate.loader.Loader.doList(Loader.java:2556)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:353)
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1873)
    at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:311)
    at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:141)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)
    at org.hibernate.jpa.internal.QueryImpl.getResultList(QueryImpl.java:449)
    ... 65 more
Caused by: java.sql.SQLException: No such column :created_2_2_0_
    at org.mariadb.jdbc.internal.common.queryresults.ColumnNameMap.getLabelIndex(ColumnNameMap.java:78)
    at org.mariadb.jdbc.internal.common.queryresults.ColumnNameMap.getIndex(ColumnNameMap.java:26)
    at org.mariadb.jdbc.MySQLResultSet.findColumn(MySQLResultSet.java:479)
    at org.mariadb.jdbc.MySQLResultSet.getLong(MySQLResultSet.java:170)
    at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:74)
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338)
    at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2969)
    at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1695)
    at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1627)
    at org.hibernate.loader.Loader.getRow(Loader.java:1514)
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:725)
    at org.hibernate.loader.Loader.processResultSet(Loader.java:952)
    at org.hibernate.loader.Loader.doQuery(Loader.java:920)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    ... 74 more

现在,在有人问我为什么要为此使用本机查询之前,我想补充一点,我有一个更复杂的用例,它以类似的方式爆发(一个更复杂的查询)。所以我创建了上面的测试来重现这个问题,看看我是否能得到一些指示。

难道我做错了什么?为什么选择 created_2_2_0 呢?我的选择语句中什至没有它。这可能是 mariadb 驱动程序错误吗?

4

0 回答 0