JPA 2.1中的@ConstructorResult映射不适用于Hibernate 4.3.5.Final [英] @ConstructorResult mapping in JPA 2.1 does not work properly with Hibernate 4.3.5.Final
问题描述
我尝试将本机查询映射到非实体类.我有以下实体:
I try to map a native query to a non-Entity class. I have the following Entity:
@Entity
@Table(name = "Groups")
@SqlResultSetMapping(name = "groupList", classes = {
@ConstructorResult(targetClass = GroupListEntry.class, columns = {
@ColumnResult(name = "id", type = Long.class),
@ColumnResult(name = "name", type = String.class),
@ColumnResult(name = "typeId", type = Long.class),
@ColumnResult(name = "lastUpdateDate", type = Date.class)
})
})
public class GroupEntity implements Serializable {
...
}
我希望打个电话
final Query query = getEntityManager().createNativeQuery(q.toString(), "groupList");
final List<GroupListEntry> result = Collections.checkedList(query.getResultList(), GroupListEntry.class);
将导致使用以下构造函数创建GroupListEntry
对象(如@ColumnResult
注释中所述)
will result in creating GroupListEntry
objects using the following constructor (as it is described in @ColumnResult
annotations)
public GroupListEntry(
final Long groupId,
final String name,
final Long typeId,
final Date lastUpdateDate) {
...
}
但是事实证明,预期的构造函数依赖于基础数据库... 在集成测试中,我使用HSQLDB,而JPA/Hibernate尝试调用:
but it turned out that the expected constructor is dependent on the underlying database... In integration tests I use HSQLDB and JPA/Hibernate tries to call:
public GroupListEntry(
final BigInteger groupId,
final String name,
final BigInteger typeId,
final Date lastUpdateDate) {
...
}
在使用MSSQL的开发环境中,它调用:
whereas on a development environment, where MSSQL is used, it calls:
public GroupListEntry(
final Integer groupId,
final String name,
final Integer typeId,
final Date lastUpdateDate) {
...
}
显然,每个@ColumnResult
中的指定类型都将被忽略.我只想拥有一个构造器,而不是三个.
Apparently the specified type in each @ColumnResult
is simply ignored. I want to have only one constructor instead of three.
下面是我的pom.xml
文件的一部分:
Below is a fragment of my pom.xml
file:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.5.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
我想念什么吗?
执行查询后,写一些注释比手动映射要优雅得多,但是我不能强迫它正常工作.
Writing some annotations is much more elegant than manual mapping after executing a query, but I cannot force it to work properly.
此致
M
推荐答案
答案是您的期望是正确的,即Hibernate 4.3.5符合以下条件:
The answer is that your expectation was correct, namely the following is true for Hibernate 4.3.5:
显然,每个@ColumnResult中的指定类型都将被忽略.
Apparently the specified type in each @ColumnResult is simply ignored.
对此有一个正式的Hibernate错误: HHH-8237-@ColumnResult的类型映射为忽略.如您所见,此错误已在Hibernate 4.3.6版中修复.
There is an official Hibernate bug for this: HHH-8237 - Type mapping of @ColumnResult is ignored. As you can see there, this bug was fixed in Hibernate version 4.3.6.
这篇关于JPA 2.1中的@ConstructorResult映射不适用于Hibernate 4.3.5.Final的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!