You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Lukas Ruetz <lu...@gmx.at> on 2007/04/10 16:21:29 UTC

How to set the type of the DB-field using @Externalizer

Hello list!

I'm trying to use the @Externalizer annotation but have problems
with the resulting type of the DB-field - it's always a byte-array.

The following code is from the docs - the mapping-tool generates
a table with a field 'url' of type byte-array. I tried to add
the @Type annotation to state that I want a VARCHAR-field but the
type doesn't change.

@Entity
public class MyClass {

  @Persistent
  @Externalizer("toExternalForm")
  @Type(String.class)
  private URL url;

}

In my case I want to use this mechanism to store/retrieve date/time
values for Joda Objects which could be expressed in form of a timestamp
as long. But the same here - the DB-field is a byte-array.
@Persistent
@Externalizer("getMillis")
@Type(Long.class)
private DateMidnight    _MyDate;

What do I miss?

I'm using 
openJPA 0.9.7-SNAPSHOT (2007-03-25)
postgresql-JDBC-8.1-408 (JDBC3)
Java 1.5
Postgres 8.1.5.

Thanks in advance
Lukas

Re: How to set the type of the DB-field using @Externalizer

Posted by Lukas Ruetz <lu...@gmx.at>.
Hello!

Am Dienstag, 10. April 2007 19:17 schrieb Abe White:
> I can't reproduce this.  When I leave out the @Persistent annotation
> on the field or replace it with @Basic, I do indeed end up with a
> binary column.  But if I correctly include the @Persistent annotation
> along with the @Externalizer, I get a varchar column.  Can you narrow
> down your entity to the smallest possible class that exhibits the
> problem and post it to the list so we can try to reproduce the error?

Finally it works. After I've reduced the project to one class with
one field and using derby as DB the problem was still the same. So
I found the problem in the ant-file that did the enhance and mapping
tasks.
But I don't understand the problem - maybe you have an explaination.
The problem was the entry '<pathelement path="${java.class.path}"/>' in
the element <classpath>. After I removed this line in both tasks it worked.

Here is the ant-file:
---
<project name="openjpa-test" default="refresh">
    <description>JPA test</description>
    
    <target name="enhance">
      <taskdef name="openjpac" 
classname="org.apache.openjpa.ant.PCEnhancerTask"/>
      <openjpac>
        <config propertiesFile="META-INF/persistence.xml"/>
        <fileset dir="${basedir}">
          <include name="domain/*.java" />
        </fileset>
        <classpath>
           <pathelement location="${basedir}"/>
           <pathelement path="${java.class.path}"/>
        </classpath>
      </openjpac>
    </target>

    <target name="refresh" depends="enhance">
      <taskdef name="mappingtool" 
classname="org.apache.openjpa.jdbc.ant.MappingToolTask"/>
      <mappingtool action="buildSchema">
        <config propertiesFile="META-INF/persistence.xml"/>
        <fileset dir="${basedir}">
          <include name="domain/*.java" />
        </fileset>
        <classpath>
           <pathelement location="${basedir}/"/>
           <pathelement path="${java.class.path}"/>
        </classpath>
      </mappingtool>
    </target>
</project>
---

my classpath is:
lib/commons-collections-3.2.jar
lib/commons-lang-2.1.jar
lib/commons-logging-1.0.4.jar
lib/commons-pool-1.3.jar
lib/derby-10.2.2.0.jar
lib/geronimo-j2ee-connector_1.5_spec-1.0.1.jar
lib/geronimo-jms_1.1_spec-1.0.1.jar
lib/geronimo-jpa_3.0_spec-1.0.jar
lib/geronimo-jta_1.0.1B_spec-1.0.1.jar
lib/openjpa-all-0.9.7-incubating-SNAPSHOT.jar
lib/postgresql-8.1-408.jdbc3.jar
lib/serp-1.11.0.jar

Thanks for your help
Lukas

Re: How to set the type of the DB-field using @Externalizer

Posted by Abe White <aw...@bea.com>.
I can't reproduce this.  When I leave out the @Persistent annotation  
on the field or replace it with @Basic, I do indeed end up with a  
binary column.  But if I correctly include the @Persistent annotation  
along with the @Externalizer, I get a varchar column.  Can you narrow  
down your entity to the smallest possible class that exhibits the  
problem and post it to the list so we can try to reproduce the error?

Notice:  This email message, together with any attachments, may contain information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated entities,  that may be confidential,  proprietary,  copyrighted  and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.

Re: How to set the type of the DB-field using @Externalizer

Posted by Lukas Ruetz <lu...@gmx.at>.
Hi!

Am Dienstag, 10. April 2007 17:01 schrieb Abe White:
> > I'm trying to use the @Externalizer annotation but have problems
> > with the resulting type of the DB-field - it's always a byte-array.
>
> You shouldn't need the @Type annotation -- the type will be inferred
>
> from the return type of the externalizer method.  Are you dropping
> the database table in between attempts? It's possible that if your 
> first attempt resulted in a byte array column because of some
> misconfiguration, OpenJPA will think it is supposed to reuse that
> column on subsequent mapping attempts, because technically it can fit
> anything in a byte-array column.

I always drop the tables between my attempts. I have also tried to drop
the whole DB but that doesn't help.

for
 @Persistent
 @Externalizer("toString")
 private URI uri;

the mapping-tool prints out:
 "uri" has mapping 
strategy "org.apache.openjpa.jdbc.meta.strats.HandlerFieldStrategy"

There are no warning or erros that say that there's something wrong.
Here is my META-INF/persistence.xml :

<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
  <persistence-unit name="openjpa" transaction-type="RESOURCE_LOCAL">
    <class>domain.JPATest</class>
    <properties>
      <property name="openjpa.ConnectionURL" 
value="jdbc:postgresql://localhost/jpatest"/>
      <property name="openjpa.ConnectionDriverName" 
value="org.postgresql.Driver"/>
      <property name="openjpa.ConnectionUserName" value="postgres"/>
      <property name="openjpa.ConnectionPassword" value=""/>
      <property name="openjpa.Log" value="DefaultLevel=TRACE, Tool=TRACE"/>
    </properties>
  </persistence-unit>
</persistence>

Anything else where I could look first?

Thanks
Lukas

Re: How to set the type of the DB-field using @Externalizer

Posted by Abe White <aw...@bea.com>.
> I'm trying to use the @Externalizer annotation but have problems
> with the resulting type of the DB-field - it's always a byte-array.

You shouldn't need the @Type annotation -- the type will be inferred  
from the return type of the externalizer method.  Are you dropping  
the database table in between attempts?  It's possible that if your  
first attempt resulted in a byte array column because of some  
misconfiguration, OpenJPA will think it is supposed to reuse that  
column on subsequent mapping attempts, because technically it can fit  
anything in a byte-array column.

Notice:  This email message, together with any attachments, may contain information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated entities,  that may be confidential,  proprietary,  copyrighted  and/or legally privileged, and is intended solely for the use of the individual or entity named in this message. If you are not the intended recipient, and have received this message in error, please immediately return this by email and then delete it.