Looking for sample using hibernate spatial with JPA

5 messages Options
Embed this post
Permalink
Farrukh Najmi

Looking for sample using hibernate spatial with JPA

Reply Threaded More More options
Print post
Permalink

Hello,

Is there an example of using hibernate spatial with Java persistence API
(JPA) anywhere?
Thanks for any clues.

--
Regards,
Farrukh
_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Farrukh Najmi

Re: Looking for sample using hibernate spatial with JPA

Reply Threaded More More options
Print post
Permalink
Farrukh Najmi wrote:
> Hello,
>
> Is there an example of using hibernate spatial with Java persistence API
> (JPA) anywhere?
> Thanks for any clues.
>
>  

The following archived thread suggests that JPA hibernate spatial works
with JPA:

<http://www.nabble.com/No-Dialect-mapping-for-JDBC-type:-2003-td15459772.html>

According to it, the annotation on entity class looks like:

@Column(name="LOC",columnDefinition="GEOMETRY")
@Type(type="org.*hibernatespatial*.GeometryUserType")
public Point getLocation(){
    return this.location;
}

But I do not know where the annotation class "Type" is defined. It does
not seem to be part of JPA.
In JPA the closest thing seems to be the columnDefinition attribute of
@Column annotation:

<http://java.sun.com/javaee/5/docs/api/javax/persistence/Column.html#columnDefinition()>

What would be the equivalent columnDefinition for:
@Type(type="org.*hibernatespatial*.GeometryUserType")


TIA for your help.


_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Karel Maesen

Re: Looking for sample using hibernate spatial with JPA

Reply Threaded More More options
Print post
Permalink

On 12 May 2008, at 17:39, Farrukh Najmi wrote:

> Farrukh Najmi wrote:
>>
> But I do not know where the annotation class "Type" is defined. It  
> does
> not seem to be part of JPA.

The @Type annotation is  a Hibernate extension to the JPA annotations.

As I understand it, the JPA has no extensibility mechanism such as  
the Hibernate UserTypes. As far as I can see, this makes it  
impossible to persist spatial attributes with only JPA annotations.

See this discussion : http://www.theserverside.com/news/thread.tss?
thread_id=41886#216328.

Regards,

Karel Maesen
_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Farrukh Najmi

Re: Looking for sample using hibernate spatial with JPA

Reply Threaded More More options
Print post
Permalink

Hi Karel,

Thanks for your kind help. I was able to use @Type from
hibernate-annotations and now I can have hibernate-spatial with postgis
dialect create the tables via hibernate entity manager for my spatial
Entity class.

However, when I run a test to persist the spatial entity class:

EntityManager em = ...
Object spatialObject = ...
em.persist(spatialObject);

I get the following exception:

java.lang.IllegalArgumentException: Unknown entity:
com.vividsolutions.jts.geom.Point
        at
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:216)

I feel I am very close to success with using hibernate-spatial with JPA.
What am I missing?

TIA for your help.

Karel Maesen wrote:

> On 12 May 2008, at 17:39, Farrukh Najmi wrote:
>
>  
>> Farrukh Najmi wrote:
>>    
>> But I do not know where the annotation class "Type" is defined. It  
>> does
>> not seem to be part of JPA.
>>    
>
> The @Type annotation is  a Hibernate extension to the JPA annotations.
>
> As I understand it, the JPA has no extensibility mechanism such as  
> the Hibernate UserTypes. As far as I can see, this makes it  
> impossible to persist spatial attributes with only JPA annotations.
>
> See this discussion : http://www.theserverside.com/news/thread.tss?
> thread_id=41886#216328.
>  


--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com


_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
Farrukh Najmi

Re: Looking for sample using hibernate spatial with JPA

Reply Threaded More More options
Print post
Permalink

Doh! I just realized that I was sending the Geometry object to
em.persist instead of the JPA entity instance. Thanks Karel for setting
me straight.

Now that I am able to persist a spatial JPA entity, next I will learn
how to query it using spatial queries.

So here is my summary of what it took to use JPA so far:

0. Enable your postegres db to be postgis capable:

Unbuntu specific instructions:
<http://postgis.com/support/wiki/index.php?PostgisOnUbuntu>

1. Create an entity class with a Geometry (or sub-class property):


package org.freebxml.omar.jaxb.bindings.rim._4_0.geo;

import com.vividsolutions.jts.geom.Geometry;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.hibernate.annotations.Type;


@Entity(name = "mypackage.GeometryValueType")
@Table(name = "GEOMETRYVALUETYPE")
public class GeometryValueType
{

    protected Geometry value;

    @Basic
    @Column(name="VALUE_",columnDefinition="GEOMETRY")
    @Type(type="org.hibernatespatial.GeometryUserType")  
    public Geometry getValue() {
        return value;
    }

    public void setValue(Geometry value) {
        this.value = value;
    }

}

2. Add your entity class to your persistence.xml

3. In your persistence.properties set hibernate.dialect as follows:

hibernate.dialect=org.hibernatespatial.postgis.PostgisDialect

Thanks again Karel for your help :-)


Karel Maesen wrote:

> Hi,
>
> Since the tables can be created by hibernate,  I guess the annotations
> are picked up properly by the entity manager. Despite this, the entity
> manager fails to use the information when persisting the object.
>
> Could you try to type the Object to the mapped class. So instead of:
>     Object spatialObject  = ...
>     em.persist(spatialObject);
>
> write:
>
>     <Classname of annotated class> spatialObject = ...
>     em.persist(spatialObject);
>
> If that doesn't help, could you provide me with the source code of the
> class you annotated?
>
> Regards,
>
> Karel
>
> On 14 May 2008, at 18:09, Farrukh Najmi wrote:
>
>> Hi Karel,
>>
>> I would be very grateful if you could please help me over this
>> hopefully simple bump.
>> Thanks.
>>
>>
>> Farrukh Najmi wrote:
>>> Hi Karel,
>>>
>>> Thanks for your kind help. I was able to use @Type from
>>> hibernate-annotations and now I can have hibernate-spatial with
>>> postgis dialect create the tables via hibernate entity manager for
>>> my spatial Entity class.
>>>
>>> However, when I run a test to persist the spatial entity class:
>>>
>>> EntityManager em = ...
>>> Object spatialObject = ...
>>> em.persist(spatialObject);
>>>
>>> I get the following exception:
>>>
>>> java.lang.IllegalArgumentException: Unknown entity:
>>> com.vividsolutions.jts.geom.Point
>>>         at
>>> org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:216)
>>>
>>>
>>> I feel I am very close to success with using hibernate-spatial with
>>> JPA. What am I missing?
>>>
>>> TIA for your help.
>>>
>>> Karel Maesen wrote:
>>>
>>>> On 12 May 2008, at 17:39, Farrukh Najmi wrote:
>>>>
>>>>
>>>>> Farrukh Najmi wrote:
>>>>>     But I do not know where the annotation class "Type" is
>>>>> defined. It  does
>>>>> not seem to be part of JPA.
>>>>>
>>>> The @Type annotation is  a Hibernate extension to the JPA annotations.
>>>>
>>>> As I understand it, the JPA has no extensibility mechanism such as  
>>>> the Hibernate UserTypes. As far as I can see, this makes it  
>>>> impossible to persist spatial attributes with only JPA annotations.
>>>>
>>>> See this discussion : http://www.theserverside.com/news/thread.tss?
>>>> thread_id=41886#216328.
>>>>
>>>
>>>
>>>
>>
>>
>> --
>> Regards,
>> Farrukh Najmi
>>
>> Web: http://www.wellfleetsoftware.com
>>
>>
>


--
Regards,
Farrukh Najmi

Web: http://www.wellfleetsoftware.com


_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users