How to fetch length of a blob field with JPQL?
I have a JPA entity with a blob field in it. I want to write a JPQL query to fetch the l开发者_运维技巧ength of the entity's blob (I don't want to load the blob into the memory).
For instance, in Oracle, I can use the following SQL query:
SELECT length(blob_field) FROM my_table WHERE id = ?
How can I fetch the blob's length with JPQL?
You cannot. JPQL length-function counts numbers of characters in string, and number of bytes in blob doesn't fit to that definition.
But, you can use length for attributes that are persisted as CLOB (char/Characters array or String as Java type).
//can use length function for CLOBs:
@Lob char[] a;
@Lob Character b;
@Lob String c;
//can not use length function for BLOBs:
@Lob byte[] d;
@Lob Byte e;
@Lob Serializable f;
If using EclipseLink you can use the "FUNC" function in JPQL to define a database function.
See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/Support_for_Native_Database_Functions
精彩评论