关系:
公司有地址。多数民众赞成我正在测试这个简单的例子。
这是带有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驱动程序错误吗?