我有两个JPA实体:VirtualLotVirtualFilesVirtualFiles扩展了VirtualInodeVirtualLotVirtualFiles之间存在ManyToMany关系,描述为“一个VirtualLot可以包含VirtualFiles”和“一个VirtualFile可以与多个VirtualLots关联”
实体定义如下:
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”关键字,它将其解释为hibernate(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,大小是我的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);

最佳答案

size是保留关键字,例如Java中的defaultfor。因此,要么在perhabs nodeSize中更改变量的名称,要么将@Column(name = "nodeSize")注释与name属性一起使用,为基本列指定特殊名称。

09-26 17:02