-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 
Author Message
 Post subject: Hibernate, Posgis and Geotools
PostPosted: Thu Oct 20, 2005 4:23 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Hello All
I hope this is not OT.
I couldn't find anywhere a small explanation of how I can handle the postgis Spatial types ("geometry" for example) in a hibernate mapped class.

I understand that I need to provide a UserType. But what should happen there ? a type case ?
I am missing the bigger picture.

I tried couple of things but none of them worked. I also tried searching here, in PostGis forums and in GeoTools forum. Nada.

Maybe any of you guys did it already ?

Thank you.

Elad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 5:02 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Custom type must work without problems for this stuff, you need "org.postgresql.geometric" package to implement custom types and use functions insted of operators in queries ( custom dialect is used to add more functions)


Top
 Profile  
 
 Post subject: Posgis
PostPosted: Thu Oct 20, 2005 5:11 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Thanks baliukas , for the quick reply.
I have "org.postgresql.geometric" included.
Should I do it like this? :
Code:
  public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
    throws HibernateException, SQLException
  {
      if (rs.wasNull()) return null;
     
      org.postgresql.geometric.PGbox v=(org.postgresql.geometric.PGbox) rs.getObject(names[0]);
      return v;
  }


And what should I return in the sqlTypes() ? maybe Types.BLOB ?

Thanks again !


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 5:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
you need to read from the rs BEFORE you call wasNull() otherwise you will get faulty values if the previous read (from another column) was null

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Postgis
PostPosted: Thu Oct 20, 2005 9:44 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Ok , I think I have a general clue.
What do you think about this solution :
Creating a view in Postgres, the uses the "astext(geom)" function to return the geometry column as text.
Than map this column to a custom UserType, and in its "nullSafeGet" use a class I found that is provided with JTS (comes with Geotools ) : WKTReader.
It can create a JTS Geometry type from this text.

Is this a good way to go ?

Thank you

Elad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 10:25 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
I am not sure postgress will able to use index (it supports R-tree index for this stuff). Just try to implement custom type using "geometric" API and "Types.OTHER"


Top
 Profile  
 
 Post subject: geometric
PostPosted: Thu Oct 20, 2005 10:53 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
baliukas, I am not sure I understand what you mean by "use geometric API".
Could you please supply the core of the nullSafeGet needed ?

Thank you !

Elad


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 20, 2005 11:20 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
Copy, modify and test some hibernate type implementation "BigDecimalType" for example:

Code:
public Object get(ResultSet rs, String name)
   throws HibernateException, SQLException {
      return rs.getObject(name);
   }

   public void set(PreparedStatement st, Object value, int index)
   throws HibernateException, SQLException {
      st.setObject(index, value,Types.OTHER);
   }

   public int sqlType() {
      return Types.OTHER;
   }


public Class getReturnedClass() {
      return PGObject.class;
   }


Top
 Profile  
 
 Post subject: Postgis
PostPosted: Thu Oct 20, 2005 11:28 am 
Beginner
Beginner

Joined: Tue May 10, 2005 4:18 am
Posts: 29
Thank you, baliukas . I will have a close on the example.

In the meantime - I implemented the solution with the
Code:
geometry = new WKTReader().read(rs.getString(names[0]));


and it is working quite fine (at least for "get") !

I understood that you didn't encouraged this pattern. Why ?
What did you mean by "doesn't support index" ?

Thanks again ..


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 3:16 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
I am not sure, but I think it will be a problem with queries. Test it using exotic operators like "&<" , postgres can use R-Tree index for this stuff (with some limitations) and it very important for GIS, but probably function indexes for strings can help too.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.