我有两个 JPA 实体:VirtualLot
和VirtualFiles
。VirtualFiles
延伸VirtualInode
。在“一个 VirtualLot 可以包含 VirtualFiles”和“一个 VirtualFile 可以与多个 VirtualLots 关联”之间存在 ManyToMany 关系,VirtualLot
实体
定义如下VirtualFiles
:
VirtualFile.java
@Entity
@Table(name = "FIL_FILE")
public class VirtualFile extends VirtualInode {
//[...]
}
VirtualInode.java
@Entity
@Table(name = "INO_INODE")
@Inheritance(strategy = InheritanceType.JOINED)
public class VirtualInode implements Serializable {
private Long id; /* The PK */
private long size = 0L; /* The size */
// [...]
/**
* Default constructor
*/
public VirtualInode() {
super();
}
/**
* @return the id
*/
@Id
@Column(name = "INO_ID", nullable = false)
public Long getId() {
return id;
}
/**
* @return the size
*/
@Column(name = "INO_SIZE", nullable = false)
public long getSize() {
return size;
}
//[...]
}
VirtualLot.java
@Entity
@Table(name = "VLT_VIRTUAL_LOT")
public class VirtualLot implements Serializable {
private Long id; /* The PK */
private Collection<VirtualFile> files;
/**
* Default constructor
*/
public VirtualLot() {
super();
}
/**
* @return the id
*/
@Id
@Column(name = "VLT_ID", nullable = false)
public Long getId() {
return id;
}
/**
* @return the files
*/
@ManyToMany
@JoinTable(name = "R001_FIL_VLT", joinColumns = @JoinColumn(name = "VLT_ID", referencedColumnName = "VLT_ID"), inverseJoinColumns = @JoinColumn(name = "INO_ID", referencedColumnName = "INO_ID"))
public Collection<VirtualFile> getFiles() {
return files;
}
//[...]
}
在这里,我想使用 Spring Data JPA 存储库对给定 VirtualLot 中包含的 VirtualFiles 的大小求和:VirtualLotRepository
。查询定义如下:
public interface VirtualLotRepository extends JpaRepository<VirtualLot, Long>
{
@Query("select sum(f.size) from VirtualLot l join l.files f where l.id = ?1")
long sumFilesSize(long lotId);
}
我的问题是 Hibernate/JPA 与“size”关键字混淆,它将它解释SIZE
为由 hibernate 定义的函数(http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11 .html#ql-collection-expressions)
结果查询是:
select
sum((select
count(virtualfil2_.VLT_ID)
from
R001_FIL_VLT files1_,
FIL_FILE virtualfil2_
inner join
INO_INODE virtualfil2_1_
on virtualfil2_.INO_ID=virtualfil2_1_.INO_ID
where
virtuallot0_.VLT_ID=files1_.VLT_ID
and files1_.INO_ID=virtualfil2_.INO_ID)) as col_0_0_
from
VLT_VIRTUAL_LOT virtuallot0_
inner join
R001_FIL_VLT files1_
on virtuallot0_.VLT_ID=files1_.VLT_ID
inner join
FIL_FILE virtualfil2_
on files1_.INO_ID=virtualfil2_.INO_ID
inner join
INO_INODE virtualfil2_1_
on virtualfil2_.INO_ID=virtualfil2_1_.INO_ID
where
virtuallot0_.VLT_ID=?
这(显然)不起作用。我希望它是这样的:
select
sum(virtualfil2_1_.INO_SIZE)
from
VLT_VIRTUAL_LOT virtuallot0_
inner join
R001_FIL_VLT files1_
on virtuallot0_.VLT_ID=files1_.VLT_ID
inner join
FIL_FILE virtualfil2_
on files1_.INO_ID=virtualfil2_.INO_ID
inner join
INO_INODE virtualfil2_1_
on virtualfil2_.INO_ID=virtualfil2_1_.INO_ID
where
virtuallot0_.VLT_ID=?
哪个适用于我的 SQL 暂存器(具有适当的“?”值)。
是否可以告诉 Hibernate/JPA size 是我的 VirtualInode/VirtualFile 实体的 size 属性而不是 SIZE 函数?
我已经尝试过双引号转义,但这不起作用。我使用 Oracle10g 方言。
编辑:fsize
如果我向我的实体
添加一个重复的属性VirtualInode
并在我的请求中使用它,它可以工作,但这不是我正在寻找的解决方案。
VirtualInode.java
@Column(name = "INO_SIZE", nullable = false, updatable = false, insertable = false)
public long getFsize() {
return getSize();
}
public void setFsize(final long size) {
setSize(size);
}
VirtualLotRepository.java
@Query("select sum(f.fsize) from VirtualLot l join l.files f where l.id = ?1")
long sumFilesSize(long lotId);