Generating database with spatial types using annotations.

2 messages Options
Embed this post
Permalink
Ronald Lawrence

Generating database with spatial types using annotations.

Reply Threaded More More options
Print post
Permalink
All,

For those of you that have a need to generate your database, I was
able to get oracle schema creation to work using SchemaUpdate by
adding a new Annotation class called "AdditionalDDL" that I attach to
my persistent classes that use spatial datatypes. This allows me to do
the required insert to the oracle view and the create index statement
for the table.  Here are a couple of snippets from my database setup
class, and the supporting annotation and an example class. It won't
compile from this email, but it should give you the idea. Some notes
about the code: The persistentClassList is just a List<Class<?>> that
contains all the persistent classes in the system. This needs to be
created prior to the call. Also "HibernateDBManager" is just a class
that I manage the configuration and session objects in.

---- snippet from test code to create the database
  public static void createDatabase()
  {
    runExtraDDLBefore();
    SchemaExport schema = new SchemaExport(
      HibernateDBManager.getDefaultConfiguration() );
    schema.setHaltOnError( true );
    schema.create( false, true );
    runExtraDDLAfter();
  }
  public static void runExtradDDLBefore()
  {
    for ( Class< ? > theClass : persistentClassList ) {
      AdditionalDDL ann = theClass.getAnnotation( AdditionalDDL.class );
      if ( ann != null ) {
        for ( String dialect : ann.dialects() ) {
          if ( DatabaseSetupUtility.databaseType.getDialect().equals(
dialect ) ) {
            executeDDL( ann.before() );
            break;
          }
        }
      }
    }
  }

  public static void runExtradDDLBefore()
  {
    for ( Class< ? > theClass : persistentClassList )
    {
      AdditionalDDL ann = theClass.getAnnotation( AdditionalDDL.class );
      if ( ann != null )
      {
        for ( String dialect : ann.dialects() )
        {
          if ( HibernateDBManager.getDialect().equals( dialect ) )
          {
            executeDDL( ann.after(), false );
            break;
          }
        }
      }
    }
  }

  private static void executeDDL( String[] ddl )
  {
    for ( String extraDDL : ddl ) { // get a hibernate session here...
        Session sess = HibernateDBManager.getSession();
        sess.beginTransaction();
        sess.createSQLQuery( extraDDL ).executeUpdate();
        sess.getTransaction().commit();
    }
  }

---- cut here : AdditonalDDL.java ---
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AdditionalDDL
{
  /** the spatial dialects to run the DDL for. If not specified, it
will run for all dialects */
  public abstract String[] dialects() default {};
  /** the strings of ddl to run before schema generation. if not
specified, nothing is run */
  public abstract String[] before() default {};
  /** the strings of ddl to run after schema generation. if not
specified, nothing is run */
  public abstract String[] after() default {};
}
---- cut here : A persistent class ---
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Type;
import com.vividsolutions.jts.geom.Geometry;
@Entity
@Table(name="GEOTEST")
@AdditionalDDL(after={"DELETE from user_sdo_geom_metadata where
table_name = 'GEOTEST'",
  "INSERT INTO user_sdo_geom_metadata VALUES ('GEOTEST', 'location',
MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X', -180, 180, 0.005),
MDSYS.SDO_DIM_ELEMENT('Y', -90, 90, 0.005)), NULL)",
  "CREATE INDEX geotest ON GEOTEST (LOCATION) INDEXTYPE IS
MDSYS.SPATIAL_INDEX"},
dialects={"org.hibernatespatial.oracle.OracleSpatial10gDialect"})
public class GeometryTestObject
{
  private Geometry location;
  private String name;
  public GeometryTestObject() { }
  @Type(type = "org.hibernatespatial.GeometryUserType")
  public Geometry getLocation() {
    return location;
  }
  public void setLocation( Geometry geometry ) {
    this.location = geometry;
  }
  @Id
  public String getName() {
    return name;
  }
  public void setName( String name ) {
    this.name = name;
  }
}


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

Re: Generating database with spatial types using annotations.

Reply Threaded More More options
Print post
Permalink
Cool...thanks for posting this.

On Fri, May 8, 2009 at 4:45 PM, Ron Lawrence <[hidden email]> wrote:

> All,
>
> For those of you that have a need to generate your database, I was
> able to get oracle schema creation to work using SchemaUpdate by
> adding a new Annotation class called "AdditionalDDL" that I attach to
> my persistent classes that use spatial datatypes. This allows me to do
> the required insert to the oracle view and the create index statement
> for the table.  Here are a couple of snippets from my database setup
> class, and the supporting annotation and an example class. It won't
> compile from this email, but it should give you the idea. Some notes
> about the code: The persistentClassList is just a List<Class<?>> that
> contains all the persistent classes in the system. This needs to be
> created prior to the call. Also "HibernateDBManager" is just a class
> that I manage the configuration and session objects in.
>
> ---- snippet from test code to create the database
>  public static void createDatabase()
>  {
>    runExtraDDLBefore();
>    SchemaExport schema = new SchemaExport(
>      HibernateDBManager.getDefaultConfiguration() );
>    schema.setHaltOnError( true );
>    schema.create( false, true );
>    runExtraDDLAfter();
>  }
>  public static void runExtradDDLBefore()
>  {
>    for ( Class< ? > theClass : persistentClassList ) {
>      AdditionalDDL ann = theClass.getAnnotation( AdditionalDDL.class );
>      if ( ann != null ) {
>        for ( String dialect : ann.dialects() ) {
>          if ( DatabaseSetupUtility.databaseType.getDialect().equals(
> dialect ) ) {
>            executeDDL( ann.before() );
>            break;
>          }
>        }
>      }
>    }
>  }
>
>  public static void runExtradDDLBefore()
>  {
>    for ( Class< ? > theClass : persistentClassList )
>    {
>      AdditionalDDL ann = theClass.getAnnotation( AdditionalDDL.class );
>      if ( ann != null )
>      {
>        for ( String dialect : ann.dialects() )
>        {
>          if ( HibernateDBManager.getDialect().equals( dialect ) )
>          {
>            executeDDL( ann.after(), false );
>            break;
>          }
>        }
>      }
>    }
>  }
>
>  private static void executeDDL( String[] ddl )
>  {
>    for ( String extraDDL : ddl ) { // get a hibernate session here...
>        Session sess = HibernateDBManager.getSession();
>        sess.beginTransaction();
>        sess.createSQLQuery( extraDDL ).executeUpdate();
>        sess.getTransaction().commit();
>    }
>  }
>
> ---- cut here : AdditonalDDL.java ---
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Retention;
> import java.lang.annotation.RetentionPolicy;
> import java.lang.annotation.Target;
> @Target(value={ElementType.TYPE})
> @Retention(RetentionPolicy.RUNTIME)
> public @interface AdditionalDDL
> {
>   /** the spatial dialects to run the DDL for. If not specified, it
> will run for all dialects */
>   public abstract String[] dialects() default {};
>   /** the strings of ddl to run before schema generation. if not
> specified, nothing is run */
>   public abstract String[] before() default {};
>   /** the strings of ddl to run after schema generation. if not
> specified, nothing is run */
>   public abstract String[] after() default {};
> }
> ---- cut here : A persistent class ---
> import javax.persistence.Entity;
> import javax.persistence.Id;
> import javax.persistence.Table;
> import org.hibernate.annotations.Type;
> import com.vividsolutions.jts.geom.Geometry;
> @Entity
> @Table(name="GEOTEST")
> @AdditionalDDL(after={"DELETE from user_sdo_geom_metadata where
> table_name = 'GEOTEST'",
>   "INSERT INTO user_sdo_geom_metadata VALUES ('GEOTEST', 'location',
> MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X', -180, 180, 0.005),
> MDSYS.SDO_DIM_ELEMENT('Y', -90, 90, 0.005)), NULL)",
>   "CREATE INDEX geotest ON GEOTEST (LOCATION) INDEXTYPE IS
> MDSYS.SPATIAL_INDEX"},
> dialects={"org.hibernatespatial.oracle.OracleSpatial10gDialect"})
> public class GeometryTestObject
> {
>   private Geometry location;
>   private String name;
>   public GeometryTestObject() { }
>   @Type(type = "org.hibernatespatial.GeometryUserType")
>   public Geometry getLocation() {
>     return location;
>   }
>   public void setLocation( Geometry geometry ) {
>     this.location = geometry;
>   }
>   @Id
>   public String getName() {
>     return name;
>   }
>   public void setName( String name ) {
>     this.name = name;
>   }
> }
>
>
> -Ron
> _______________________________________________
> hibernatespatial-users mailing list
> [hidden email]
> http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users
>



--
Zeno Consulting, Inc.
home: http://www.zenoconsulting.biz
blog: http://zenoconsulting.wikidot.com
p: 248.894.4922
f: 313.884.2977
_______________________________________________
hibernatespatial-users mailing list
[hidden email]
http://www.hibernatespatial.org/cgi-bin/mailman/listinfo/hibernatespatial-users