views:

101

answers:

2

For a weekend project I was trying to run JPA 2 with Hibernate 3.5. Please note that I am not getting any compile errors or runtime exceptions (when I deploy the war on Tomcat). Below is my code -

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="postage" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"></property>
            <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postage"></property>
            <property name="hibernate.connection.username" value="postage"></property>
            <property name="hibernate.connection.password" value="postage"></property>
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"></property>
            <property name="hibernate.hbm2ddl.auto" value="create-drop"></property>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.ejb.cfgfile" value="/hibernate.cfg.xml"></property>
        </properties>
    </persistence-unit>
</persistence>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;
<hibernate-configuration>
    <session-factory>
        <property name="current_session_context_class">thread</property>
        <!-- mapping files -->
        <mapping class="net.rocky.postage.domain.Post"/>
        <mapping class="net.rocky.postage.domain.User"/>
    </session-factory>
</hibernate-configuration>

I have 2 simple Entity classes -

@Entity
@Table(name="Post")
public class Post implements Serializable{

    @Id
    @GeneratedValue
    private long id;

    private String description;

    private String comments;

    @OneToOne
    private User postedBy;

And

@Entity
@Table(name="PostageUser")
public class User implements Serializable{

    @Id
    @GeneratedValue
    private long id;

    private String username;

    private String password;

Am I missing something here. I have spent my whole saturday trying all combinations. Please help me.

Follow-up 1: By not working I mean - When I deploy the app on Tomcat, I do not see 2 tables created in Postgres (as I have given create-drop in hbm2ddl).

Follow-up 2: Thanks for your response. I cannot even get Hibernate to log messages. Here is my config:

log4j.appender.S=org.apache.log4j.ConsoleAppender
log4j.appender.S.layout=org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
log4j.rootCategory=DEBUG, S

Also, in maven I have added -

         <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-log4j12</artifactId>
             <version>1.6.1</version>
         </dependency>
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.16</version>
         </dependency>

still no log messages from hibernate

+1  A: 

I couldn't spot any obvious mistake in the mappings or configuration (although you should typically use a datasource when running in container but your configuration can work). But here are some suggestions or ideas:

  • Tell us more about the structure and the packaging of your app (where is the persistence.xml?).
  • Enable DEBUG logging :
    • for the category org.hibernate.tool.hbm2ddl
    • and maybe even the whole org.hibernate category until you get it working
  • Run a JUnit test (out-container), it will make debugging easier.

Follow-up: I don't see any logger for Hibernate defined in your logging configuration. You need to add the following for the category org.hibernate.tool.hbm2ddl:

log4j.logger.org.hibernate.hbm2ddl=debug

I'd also like to know where your persistence.xml is located in your Maven project.

References

Pascal Thivent
A: 

I finally got this working .. i changed my dependencies to -

     <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>3.3.2.GA</version>
    </dependency>       
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.3.2.GA</version>
    </dependency>       <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-annotations</artifactId>
        <version>3.3.1.GA</version>
    </dependency>

earlier it was - org.hibernate hibernate-core 3.5.x

Remember 3.5 doc says that it has merged EM and Annotations in core, which is why i wanted to use it.


When I configured the datasource through spring, it gave me class not found org.hibernate.ejb.HibernatePersistence (this was my JPA provider)

On further investigation i found that the JBoss maven repo (https://repository.jboss.org/nexus/content/groups/public) had the hibernate 3.5 jars but the jar is wrong (say size X MB) but when you download hibernate binaries from the site they are of size Y MB. Also, the pom of this jar does not define the correct dependencies (which you will get if you download the bundled distribution directly from Hibernate site).

For me this was kinda deal-breaker and I had to switch back to 3.3 to get this working flawlessly.

Maybe this will help someone and possibly Hibernate 3.5 on Maven will be fixed. This issue was also raised in Hibernate forums.

RockyJ