danowar
danowar

Reputation: 675

What is the configuration to use for the combination MySQL-Server/Apache Tomcat and a Tapestry project using Hibernate/JPA?

I have a MySQL Server running on an Apache Tomcat.

I also have created a Tapestry project via Quickstart Archetype. I have then added dependencies to tapestry-hibernate and and the mysql connector in the POM.

    <dependency>
        <groupId>org.apache.tapestry</groupId>
        <artifactId>tapestry-hibernate</artifactId>
        <version>${tapestry-release-version}</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.31</version>
    </dependency>

I have created an entity with the appropriate annotations (Entity, Id, Table, Column, etc.).

I do not want to use the Hibernate API, but the Hibernate JPA "subset".

I have a persistence.xml in the java/main/resources/META-INF folder and a hibernate.cfg.xml in the java/main/resources folder.

The persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    version="2.0">
    <persistence-unit name="PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value = "true" />
            <property name="hibernate.format_sql" value = "true" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/someDB" />
            <property name="javax.persistence.jdbc.user" value="someUser" />
            <property name="javax.persistence.jdbc.password" value="somePassword" />
        </properties>   
    </persistence-unit>

</persistence>

My hibernate.cfg.xml:

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<!-- For use of Hibernate with a MySQL-DB over a webserver -->
   <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.hbm2ddl.auto">create</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/someDB"</property>
        <property name="hibernate.connection.username">someUser</property>
        <property name="hibernate.connection.password">somePassword</property>

   <!-- List of XML mapping files -->

</session-factory>
</hibernate-configuration>

I started the tapestry application using jetty:run.

The problem: The app starts, I can view the index page, but the database isn't accessed or modified.

  1. I want the database and tables to be created automatically. I know that hibernate.hbm2ddl.auto=create creates the tables. Does it also create the database or does it have to be created manually?

  2. I only have one entity and the basic tapestry pages in my code. Do I explicitly have to at least define an EntityManager to make the program create the tables?

  3. Do I have to have both the persistence.xml and the hibernate.cfg.xml? It seems some properties are redundant...

  4. If I want to use Hibernate just as a JPA implementation and not the hibernate-specific API, are the properties in the configuration files the correct ones?

Upvotes: 0

Views: 422

Answers (1)

ajaysoni98292
ajaysoni98292

Reputation: 11

First of all add the dependencies in the pom for the mysql -

<dependency>
    <groupId>org.apache.tapestry</groupId>
    <artifactId>tapestry-hibernate</artifactId>
    <version>${tapestry-release-version}</version>
</dependency>

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>${hibernate-tapestry-version}</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>${mysql-connector-version}</version>
</dependency>

and modify your hibernate.cfg.xml file with the following configuration

<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tapestry_db?createDatabaseIfNotExist=true</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">false</property>
        <property name="hibernate.format_sql">true</property>
    </session-factory>
</hibernate-configuration> 

This is working for me.

Upvotes: 0

Related Questions