我有兩個JPA實體:VirtualLot
和VirtualFiles
。 VirtualFiles
延伸VirtualInode
。 有描述爲VirtualLot
和VirtualFiles
之間的多對多關係 「A VirtualLot可以包含VirtualFiles」和 的實體 「A VirtualFile可以與多個VirtualLots相關的」 的定義如下:如何在hibernate/jpa實體中使用「size」作爲字段名稱?
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;
}
//[...]
}
在這裏,我要總結使用彈簧數據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);
}
我的問題是休眠/ JPA弄亂與「尺寸」的關鍵字,它會將其解釋爲(http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#ql-collection-expressions)由休眠限定的SIZE
函數
所得查詢是:
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函數?
我已經試過雙引號轉義,並且不起作用。 我使用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);
您的大小字段更改爲另一個名字,並說會發生什麼。 –
它的工作原理,但這不是我要找的解決方案,無論如何,謝謝 – superbob