如何在hibernate / jpa实体中使用“size”作为字段名?

时间:2013-10-14 10:19:30

标签: java spring hibernate jpa spring-data-jpa

我有两个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混淆了&#34; size&#34;关键字,它将其解释为由hibernate定义的SIZE函数(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暂存器(具有正确的&#39;?&#39;值)。

是否可以告诉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);

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

我不确定这个解决方案,但试一试:

只需使用以下内容更改@Query

@Query(value = "select sum(f.size) from VirtualLot l join l.files f where l.id = ?1", nativeQuery = true)

nativeQuery参数应该将您的查询更改为Oracle本机查询。

请参阅this stackoverflow topic以及此处提供的链接作为参考。

希望这能解决您的问题。