You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by xzer <xi...@gmail.com> on 2012/11/15 04:26:43 UTC

how to deal with jdbc4 sql array?

Hi,

I am using openjpa 2.2 with postgresql 9.0, there is a field as a
bigint[]. I can not declare as following:


    @Column(name = "target_id")
    protected long[] targetId;

I searched whole day but did not find any useful information about it.
In openjpa jira, there is a ticket that provided a possible solution,
https://issues.apache.org/jira/browse/OPENJPA-1687, but it failed to
me, openjpa says "targetId isn't supported by declared persistence
strategy "Basic".  Please choose a different strategy". I have no idea
about this message.

And also I analyzed the openjap's source, I saw some sources that
seems related to Sql Array, but I can not find any more.

So, please, can anyone tell me whether it is possible to deal with
jdbc4 sql array and how? Or it is impossible?

Best Regards
yours xzer

Re: how to deal with jdbc4 sql array?

Posted by xiaozhu <xi...@gmail.com>.
yes, I declared my own strategy, I thought it should be available but openjpa
still gives a message as what I mentioned.

Now I declared my column as:

   @Column(name="target_id")
   @Lob
   @Strategy("xx.xx.pgArrayHandler")
   private long[] _targetId
   public long[] getTargetId(){...}
   public void setTargetId(...){...}

it works well, but if I replace the @Lob to @PersistentCollection, I will get
the message as "xx.xx.xx._targetId isn't supported by declared persistence
strategy "Basic".  Please choose a different strategy."

I also think it is strange, but I have no idea about why.


On 2012/11/16 19:10, Krzysztof wrote:
> Did you use the amended Strategy as well? This should replace @Basic strategy
> used by default otherwise..
>
>
>
>
> --
> View this message in context: http://openjpa.208410.n2.nabble.com/how-to-deal-with-jdbc4-sql-array-tp7581756p7581792.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to deal with jdbc4 sql array?

Posted by Krzysztof <ya...@gmail.com>.
Did you use the amended Strategy as well? This should replace @Basic strategy
used by default otherwise.. 




--
View this message in context: http://openjpa.208410.n2.nabble.com/how-to-deal-with-jdbc4-sql-array-tp7581756p7581792.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to deal with jdbc4 sql array?

Posted by xzer <xi...@gmail.com>.
I dug into the jpql implementation of openjpa, and at the
org.apache.openjpa.jdbc.kernel.exps.PCPath, line 465,

    public Class getType() {
        if (_cast != null)
            return _cast;
        Action act = lastFieldAction();
        if (act != null && act.op == Action.GET_XPATH)
            return ((XMLMetaData) act.data).getType();

        FieldMetaData fld = (act == null) ? null : (FieldMetaData) act.data;
        boolean key = act != null && act.op == Action.GET_KEY;
        if (fld != null) {
            switch (fld.getDeclaredTypeCode()) {
                case JavaTypes.ARRAY:
                    if (fld.getDeclaredType() == byte[].class
                        || fld.getDeclaredType() == Byte[].class
                        || fld.getDeclaredType() == char[].class
                        || fld.getDeclaredType() == Character[].class)
                        return fld.getDeclaredType();
                    return fld.getElement().getDeclaredType();
                case JavaTypes.MAP:
                    if (key)
                        return fld.getKey().getDeclaredType();
                    return fld.getElement().getDeclaredType();
                case JavaTypes.COLLECTION:
                    return fld.getElement().getDeclaredType();
                default:
                    return fld.getDeclaredType();
            }
        }
        if (_class != null)
            return _class.getDescribedType();
        return Object.class;
    }


In the above method, it will always return the element type of a array
if it is not a byte/char array. I feel so sad for this limit
and that I can not use a array field as a query condition. Is there
any work around to bypass this limit? Or I have to maintain
a patch for this?

2012/11/16 xzer <xi...@gmail.com>:
> Well, it works for query and update now.
>
>         col.setJavaType(JavaSQLTypes.SQL_ARRAY);
>         col.setType(JavaSQLTypes.SQL_ARRAY);
>
> declare the field as @Lob and set col type in the handler as above
> will work well.
>
> but there is still a problem, I can not use the field as a query condition:
>
> Caused by: java.lang.IllegalArgumentException: Parameter
> "Parameter<long>(2)" declared in "select c from WatchCondition c where
> c._targetType = ?1 and c._testIdList = ?2" is set to value of
> "[J@3dc016fe" of type "[J", but this parameter is bound to a field of
> type "long".
>         at org.apache.openjpa.persistence.AbstractQuery.assertValueAssignable(AbstractQuery.java:616)
> ~[openjpa-2.2.0.jar:2.2.0]
>         at org.apache.openjpa.persistence.AbstractQuery.bindValue(AbstractQuery.java:558)
> ~[openjpa-2.2.0.jar:2.2.0]
>         at org.apache.openjpa.persistence.AbstractQuery.setParameter(AbstractQuery.java:140)
> ~[openjpa-2.2.0.jar:2.2.0]
>         at org.apache.openjpa.persistence.AbstractQuery.setParameter(AbstractQuery.java:46)
> ~[openjpa-2.2.0.jar:2.2.0]
>
> 2012/11/16 xzer <xi...@gmail.com>:
>> @PersistentCollection does not work for me, it always gives a message
>> as "the_field_as_array isn't supported by declared persistence
>> strategy "Basic".  Please choose a different strategy."
>>
>> I follow the description in the jira ticket to declare the field as
>> @Lob, it works for query, but still not work for update.
>>
>> In the jira ticket, it says I should provide an extended
>> PostgresDictionary, how to tell openjpa to use my extended
>> PostgresDictionary?
>>
>> 2012/11/15 Krzysztof <ya...@gmail.com>:
>>> Use your own strategy:
>>>
>>> @PersistentCollection
>>> @Strategy("gaia.cu7.om.dal.dictionary.PostgresArrayHandler")
>>> private double[] timeDecay;
>>>
>>> Example for float8[] below, change toDataStoreValue to deal with
>>> Long/bigint.
>>> PostgresArrayHandler.java
>>> <http://openjpa.208410.n2.nabble.com/file/n7581766/PostgresArrayHandler.java>
>>>
>>> Cheers
>>>
>>>
>>>
>>> --
>>> View this message in context: http://openjpa.208410.n2.nabble.com/how-to-deal-with-jdbc4-sql-array-tp7581756p7581766.html
>>> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to deal with jdbc4 sql array?

Posted by xzer <xi...@gmail.com>.
Well, it works for query and update now.

        col.setJavaType(JavaSQLTypes.SQL_ARRAY);
        col.setType(JavaSQLTypes.SQL_ARRAY);

declare the field as @Lob and set col type in the handler as above
will work well.

but there is still a problem, I can not use the field as a query condition:

Caused by: java.lang.IllegalArgumentException: Parameter
"Parameter<long>(2)" declared in "select c from WatchCondition c where
c._targetType = ?1 and c._testIdList = ?2" is set to value of
"[J@3dc016fe" of type "[J", but this parameter is bound to a field of
type "long".
	at org.apache.openjpa.persistence.AbstractQuery.assertValueAssignable(AbstractQuery.java:616)
~[openjpa-2.2.0.jar:2.2.0]
	at org.apache.openjpa.persistence.AbstractQuery.bindValue(AbstractQuery.java:558)
~[openjpa-2.2.0.jar:2.2.0]
	at org.apache.openjpa.persistence.AbstractQuery.setParameter(AbstractQuery.java:140)
~[openjpa-2.2.0.jar:2.2.0]
	at org.apache.openjpa.persistence.AbstractQuery.setParameter(AbstractQuery.java:46)
~[openjpa-2.2.0.jar:2.2.0]

2012/11/16 xzer <xi...@gmail.com>:
> @PersistentCollection does not work for me, it always gives a message
> as "the_field_as_array isn't supported by declared persistence
> strategy "Basic".  Please choose a different strategy."
>
> I follow the description in the jira ticket to declare the field as
> @Lob, it works for query, but still not work for update.
>
> In the jira ticket, it says I should provide an extended
> PostgresDictionary, how to tell openjpa to use my extended
> PostgresDictionary?
>
> 2012/11/15 Krzysztof <ya...@gmail.com>:
>> Use your own strategy:
>>
>> @PersistentCollection
>> @Strategy("gaia.cu7.om.dal.dictionary.PostgresArrayHandler")
>> private double[] timeDecay;
>>
>> Example for float8[] below, change toDataStoreValue to deal with
>> Long/bigint.
>> PostgresArrayHandler.java
>> <http://openjpa.208410.n2.nabble.com/file/n7581766/PostgresArrayHandler.java>
>>
>> Cheers
>>
>>
>>
>> --
>> View this message in context: http://openjpa.208410.n2.nabble.com/how-to-deal-with-jdbc4-sql-array-tp7581756p7581766.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to deal with jdbc4 sql array?

Posted by xzer <xi...@gmail.com>.
@PersistentCollection does not work for me, it always gives a message
as "the_field_as_array isn't supported by declared persistence
strategy "Basic".  Please choose a different strategy."

I follow the description in the jira ticket to declare the field as
@Lob, it works for query, but still not work for update.

In the jira ticket, it says I should provide an extended
PostgresDictionary, how to tell openjpa to use my extended
PostgresDictionary?

2012/11/15 Krzysztof <ya...@gmail.com>:
> Use your own strategy:
>
> @PersistentCollection
> @Strategy("gaia.cu7.om.dal.dictionary.PostgresArrayHandler")
> private double[] timeDecay;
>
> Example for float8[] below, change toDataStoreValue to deal with
> Long/bigint.
> PostgresArrayHandler.java
> <http://openjpa.208410.n2.nabble.com/file/n7581766/PostgresArrayHandler.java>
>
> Cheers
>
>
>
> --
> View this message in context: http://openjpa.208410.n2.nabble.com/how-to-deal-with-jdbc4-sql-array-tp7581756p7581766.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to deal with jdbc4 sql array?

Posted by Krzysztof <ya...@gmail.com>.
Use your own strategy:

@PersistentCollection
@Strategy("gaia.cu7.om.dal.dictionary.PostgresArrayHandler")
private double[] timeDecay;

Example for float8[] below, change toDataStoreValue to deal with
Long/bigint.
PostgresArrayHandler.java
<http://openjpa.208410.n2.nabble.com/file/n7581766/PostgresArrayHandler.java>  

Cheers



--
View this message in context: http://openjpa.208410.n2.nabble.com/how-to-deal-with-jdbc4-sql-array-tp7581756p7581766.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.