[GIS] Running hibernate-spatial in Eclipse

hibernate-spatialjavapostgis

I have a problem with Hibernate Spatial.
I followed the tutorial 4.x, and I created my project using maven.
after correcting some minor errors in pom.xml and in persistence.xml, executing the project (via mave) works perfectly.

my pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.hibernatespatial.tutorials</groupId>
    <artifactId>event-tutorial</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>event-tutorial</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <hibernate.version>4.0.0.Final</hibernate.version>
    </properties>

    <build>
         <!-- we dont want the version to be part of the generated war file name -->
         <finalName>${artifactId}</finalName>
         <plugins>
               <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>

        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.5.1</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <includeGroupIds>org.hibernate</includeGroupIds>
                <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>

                <includeGroupIds>org.postgis</includeGroupIds>
                <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>

                <includeGroupIds>postgresql</includeGroupIds>
                <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>

                <includeGroupIds>org.slf4j</includeGroupIds>
                <outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
            </configuration>
          </execution>
         </executions>
        </plugin>
         </plugins>
    </build>


    <dependencies>

        <!-- Hibernate Spatial for postgis. This will include Hibernate Spatial Core and JTS -->
        <!--<dependency>
            <groupId>org.hibernatespatial</groupId>
            <artifactId>hibernate-spatial-postgis</artifactId>
            <version>1.1</version>
        </dependency>
    -->
          <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-spatial</artifactId>
            <version>4.0-M1</version>
        </dependency>
     <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <!-- the Postgis JDBC driver -->
        <dependency>
            <groupId>org.postgis</groupId>
            <artifactId>postgis-jdbc</artifactId>
            <version>1.3.3</version>
        </dependency>

        <!-- the postgresql driver -->
        <dependency>
            <groupId>postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>8.4-701.jdbc3</version>
        </dependency>

        <!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
               <dependency>
                   <groupId>org.slf4j</groupId>
                   <artifactId>slf4j-simple</artifactId>
                   <version>1.5.11</version>
               </dependency>

    </dependencies>


    <!-- add repositories for JTS and Hibernate Spatial and Hibernate (JBoss) -->
    <repositories>
        <repository>
            <id>OSGEO GeoTools repo</id>
            <url>http://download.osgeo.org/webdav/geotools</url>
        </repository>
        <repository>
            <id>Hibernate Spatial repo</id>
            <url>http://www.hibernatespatial.org/repository</url>
        </repository>
        <!-- add JBOSS repository for easy access to Hibernate libraries -->
        <repository>
            <id>JBOSS</id>
            <url>https://repository.jboss.org/nexus/content/repositories/releases/</url>
        </repository>
    </repositories>
</project>

and my persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
        version="2.0">
    <persistence-unit name="org.hibernate.events.jpa" transaction-type="RESOURCE_LOCAL">
       <properties>
           <property name="hibernate.dialect" value="org.hibernate.spatial.dialect.postgis.PostgisDialect"/>

           <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
       <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5433/mydb"/> <!-- my port is 5433 -->
           <property name="hibernate.connection.username" value="myusername"/>
           <property name="hibernate.connection.password" value="mypassword"/>
           <property name="hibernate.connection.pool_size" value="5"/>

           <property name="hibernate.show_sql" value="true"/>
           <property name="hibernate.format_sql" value="true"/>

           <property name="hibernate.max_fetch_depth" value="5"/>

           <property name="hibernate.hbm2ddl.auto" value="update"/>

       </properties>
    </persistence-unit>
</persistence>

Regarding the java classes, they are exactly the same of the tutorial.

Esecution and uotput:

$ mvn exec:java -Dexec.mainClass="event.EventManager" -Dexec.args="store POINT(10 5)"

PostGres shell:

WGis=# select * from event;           
             id |          date           |                  location                  |  title   
            ----+-------------------------+--------------------------------------------+----------
              1 | 2013-12-07 15:44:21.653 | 010100000000000000000024400000000000001440 | My Event
(1 row)
WGis=#

At this point, after bringing the project into eclipse, I load the following libraries:
enter image description here

but as I run the project gives me this error.

dic 07, 2013 4:40:24 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
dic 07, 2013 4:40:24 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.6.Final}
dic 07, 2013 4:40:24 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
dic 07, 2013 4:40:24 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
dic 07, 2013 4:40:24 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
dic 07, 2013 4:40:24 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 5
dic 07, 2013 4:40:24 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: true
dic 07, 2013 4:40:24 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000401: using driver [org.postgresql.Driver] at URL [jdbc:postgresql://localhost:5433/WGis]
dic 07, 2013 4:40:24 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=pierfrancesco, password=****, autocommit=true, release_mode=auto}
dic 07, 2013 4:40:25 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect
dic 07, 2013 4:40:25 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
dic 07, 2013 4:40:25 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
dic 07, 2013 4:40:25 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
dic 07, 2013 4:40:25 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
dic 07, 2013 4:40:25 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
dic 07, 2013 4:40:25 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
dic 07, 2013 4:40:25 PM org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: event.Event
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:878)
    at event.EventManager.createAndStoreEvent(EventManager.java:45)
    at event.EventManager.main(EventManager.java:24)

How can I solve it?

Best Answer

I have found a solution to my problem by following the advice in How to auto-register entities with JPA/Hibernate: Unknown entity.

I simply added the tag Within my persistence.xml in this way

 ...
<persistence-unit name="org.hibernate.events.jpa" transaction-type="RESOURCE_LOCAL">
<class>event.Event</class>
<properties>
 ...