Java – how to use “size” as the field name in Hibernate / JPA entities?

I have two JPA entities: virtuallot and virtualfiles Virtualfiles extends virtualinode

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;
    }
    //[...]
}

Here, I want to use the spring data JPA repository to summarize the size of the virtual files contained in a given virtual lot: virtual lotrepository The query definition is as follows:

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);
}

My problem is that hibernate / JPA confuses the "size" keyword, which is interpreted as the size function defined by hibernate( http://docs.jboss.org/hibernate/orm/4.1/devguide/en-US/html/ch11.html#ql -collection-expressions)

The query result is:

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=?

Which (obviously) doesn't work I hope it's like this:

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=?

Which applies to my SQL register (with the correct '?' value)

Is it possible to tell hibernate / JPA that size is the size attribute of my virtuarinode / virtualfile entity rather than the size function?

I've tried double quotation mark escape, but it doesn't work I use Oracle10g dialect

Edit: if I add a duplicate attribute fsize to my virtualinode entity and use it in my request, it can work, but this is not the solution I'm looking for

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);

Solution

Size is a reserved keyword, such as default or for Java Therefore, either change the name of the variable in perhabs nodesize or specify a special name for the base column using @ column (name = "nodesize") annotation with the name attribute

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>