We will now change the default project so that it uses MySQL database (instead of Apache Derby) and Hibernate JPA Provider (instead of EclipseLink). In order to do that, we need to trigger some dependencies into Maven pom.xml file. Here's the list of dependencies we will use:

http://mvnrepository.com/artifact/mysql/mysql-connector-java

http://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api

http://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager

Now edit your pom.xml file so that it looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mastertheboss</groupId>
    <artifactId>Hibernate4Example</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.0-api</artifactId>
            <version>1.0.1.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.0.1.Final</version>
        </dependency>
    </dependencies>
</project>

Next thing we need to change is persistence.xml file, which is located into the resources/META-INF folder of your Maven project. (We have set up to use a JDBC URL named "jdbc:mysql://localhost:3306/mysqlDB")


<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="persistenceUnit"
        transaction-type="RESOURCE_LOCAL">

        <class>com.mastertheboss.domain.Employee</class>
        <class>com.mastertheboss.domain.Department</class>
        <properties>

            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mysqlDB" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="passwd" />
            <property name="hbm2ddl.auto" value="create" />

            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        </properties>

    </persistence-unit>
</persistence>

That's all. Verify that the Test class works using again the following command:

mvn compile exec:java -Dexec.mainClass=com.mastertheboss.jpa.JpaTest

In the next Maven tutorial, we will show how to create your own archetype and publish it on a public repository so that it can be used by the Community of developers.

0
0
0
s2smodern