一尘不染

如何在休眠/ jpa实体中使用“大小”作为字段名称?

hibernate

我有两个JPA实体:VirtualLotVirtualFilesVirtualFiles延伸VirtualInode。两者之间存在ManyToMany关系,VirtualLot并被VirtualFiles描述为
“一个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;
    }
    //[...]
}

在这里,我要总结使用弹簧数据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”关键字,它将其解释为SIZEhibernate定义的函数(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,大小是我的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);

阅读 262

收藏
2020-06-20

共1个答案

一尘不染

size是Java
defaultforJava中的保留关键字。因此,要么在perhabs中更改变量的名称,要么对属性nodeSize使用@Column(name = "nodeSize")Annotation,name为基本列指定特殊名称。

2020-06-20