Title: Can't get JPA with mysql running
Tags: hibernate;tomcat;jpa;maven;jsf-2
Question: I am trying to setup a project using CDI-weld, JSF 2-mojarra, RichFaces 4, JPA-hibernate, Bean-Validation-hibernate, maven 2, Tomcat 7 and Eclipse Juno.

I thought it would be interesting to feel out the synergy of the latest stuff (except for maven, to lazy for getting into maven 3)

I got everything working like a charm except for the last piece in the puzzle, adding JPA. There's a lot of information on how to set it up and after trying for a while now I just feel very confused. I have it narrowed down to this basically:

(from this post: what dependencies my project should have if I&#39;m using JPA in Hibernate?)

you need: 
slf4j-jdk14 and hibernate-entitymanager as artifacts and that's it? I tried a bunch of different setups but I got nothing of it to work. It just won't generate the tables.

(For now i'm trying with these artifacts and I figured i'll worry about versions later)

```    <dependency&gt;
        <groupId&gt;org.hibernate</groupId&gt;
        <artifactId&gt;hibernate-entitymanager</artifactId&gt;
        <version&gt;3.5.1-Final</version&gt;
    </dependency&gt;

    <dependency&gt;
        <groupId&gt;org.slf4j</groupId&gt;
        <artifactId&gt;slf4j-jdk14</artifactId&gt;
        <!-- version 1.5.8 is the latest version that will work with the slf4j-api 
            that's currently bundled with hibernate-parent --&gt;
        <version&gt;1.5.8</version&gt;
    </dependency&gt;
```

persistance.xml is located here: src - main - wewbapp - WEB-INF - classes - META-INF - persistance.xml.

Tried a bunch of locations with no luck.

Content is as follows:

```<?xml version="1.0" encoding="UTF-8"?&gt;
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://svn.apache.org/repos/asf/geronimo/components/geronimo-schema-javaee_6/trunk/src/main/xsd/persistence_2_0.xsd"
    version="2.0"&gt;
    <persistence-unit name="pu"&gt;
        <provider&gt;org.hibernate.ejb.HibernatePersistence</provider&gt;
        <class&gt;com.test.User</class&gt;
        <properties&gt;
            <!-- Auto detect annotation model classes --&gt;
            <property name="hibernate.archive.autodetection" value="class" /&gt;
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/&gt;
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt;
            <property name="hibernate.hbm2ddl.auto" value="update" /&gt;
            <property name="hibernate.show_sql" value="false" /&gt;
            <property name="hibernate.format_sql" value="true" /&gt;
            <property name="hibernate.connection.username" value="root" /&gt;
            <property name="hibernate.connection.password" value="1111" /&gt;
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost/woot" /&gt;
        </properties&gt;

    </persistence-unit&gt;
</persistence&gt;
```

Borrowed test Class:

```/**
 * User account that is required for logging in and posting bookmarks.
 * 
 * @author Andy Gibson
 *
 */
@Entity
@Table(name = "USERS")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(length = 24)
    private String username;

    @Column(length = 24,name="userPassword")
    private String password;

    @Column(length = 24)
    private String firstName;

    @Column(length = 24)
    private String lastName;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

}
```

Tomcat log:

```apr 21, 2012 10:58:54 EM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: E:\Programming\Setup\Glassfish\jdk7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;E:\Programming\Setup\Glassfish\jdk7\jre\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\Microsoft Windows Performance Toolkit\;E:\Programming\Setup\Glassfish\jdk7\bin;C:\Program Files (x86)\Groovy\Groovy-1.8.0\bin;C:\Program Files (x86)\Calibre2\;E:\Programming\Setup\apache-maven-2.2.1\bin;E:\Program\SVN\bin;E:\Programming\grails-2.0.0\\bin;.
apr 21, 2012 10:58:54 EM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:woot' did not find a matching property.
apr 21, 2012 10:58:54 EM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
apr 21, 2012 10:58:54 EM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
apr 21, 2012 10:58:54 EM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 900 ms
apr 21, 2012 10:58:54 EM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
apr 21, 2012 10:58:54 EM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.22
apr 21, 2012 10:58:55 EM org.apache.catalina.core.StandardContext addApplicationListener
INFO: The listener "com.sun.faces.config.ConfigureListener" is already configured for this context. The duplicate definition has been ignored.
apr 21, 2012 10:58:55 EM org.jboss.weld.bootstrap.WeldBootstrap <clinit&gt;
INFO: WELD-000900 1.1.5 (Final)
apr 21, 2012 10:58:55 EM org.jboss.weld.bootstrap.WeldBootstrap startContainer
INFO: WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
apr 21, 2012 10:58:56 EM org.jboss.weld.environment.tomcat7.Tomcat7Container initialize
INFO: Tomcat 7 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
apr 21, 2012 10:58:56 EM org.jboss.interceptor.util.InterceptionTypeRegistry <clinit&gt;
WARNING: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
apr 21, 2012 10:58:56 EM org.jboss.interceptor.util.InterceptionTypeRegistry <clinit&gt;
WARNING: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
apr 21, 2012 10:58:56 EM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.1.7 (SNAPSHOT 20120206) for context '/rdwebref'
apr 21, 2012 10:58:58 EM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
apr 21, 2012 10:58:59 EM org.hibernate.validator.util.Version <clinit&gt;
INFO: Hibernate Validator 4.0.2.GA
apr 21, 2012 10:58:59 EM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
apr 21, 2012 10:59:00 EM org.hibernate.validator.engine.resolver.DefaultTraversableResolver detectJPA
INFO: Instantiated an instance of org.hibernate.validator.engine.resolver.JPATraversableResolver.
apr 21, 2012 10:59:00 EM org.richfaces.cache.CacheManager getCacheFactory
INFO: Selected fallback cache factory
apr 21, 2012 10:59:00 EM org.richfaces.cache.lru.LRUMapCacheFactory createCache
INFO: Creating LRUMap cache instance using parameters: {javax.servlet.jsp.jstl.fmt.localizationContext=resources.application, javax.faces.STATE_SAVING_METHOD=client, javax.faces.DEFAULT_SUFFIX=.xhtml}
apr 21, 2012 10:59:00 EM org.richfaces.cache.lru.LRUMapCacheFactory createCache
INFO: Creating LRUMap cache instance of 512 items capacity
apr 21, 2012 10:59:00 EM org.richfaces.application.InitializationListener onStart
INFO: RichFaces Core Implementation by JBoss by Red Hat, version v.4.2.0.Final
apr 21, 2012 10:59:00 EM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
apr 21, 2012 10:59:00 EM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
apr 21, 2012 10:59:00 EM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6130 ms
```

I did create the database called "woot" and password and username is correct. I have gotten mysql to work with Grails 2 previously.

Hope someone can help me out here! cheers

Comment: Do you create an EntityManagerFactory? That should generate some error messages if something is wrong.


Here is another answer: Basically, there are two ways of using JPA:


In an EJB3 Container, such as Apache Geronimo, JBoss AS, Glassfish, etc. Tomcat is just a Web Container, so this option is unavailable for it.


Using in this mode has two advantages:

a. If you use Session Beans, the Entity Manager can be injected by the EJB framework, no Weld required;

b. The transaction control is done automatically by the EJB Containter (JTA).


The so-called "Resource Local" mode. In this mode you have to create the EntityManagerFactory on your own, as well as taking care of all resources, including:

The EntityManager
Transactions



This is the most common way to use JPA if you are not dealing with an EJB Container. 
In order to avoid boilerplate code and to make things a lot easier, an Injection Manager is advised. Spring, Guice, Pico are great choices, but Weld is good too, and slightly better since it implements CDI, the Java's standard for injection.

My best bet is to look for more info about how to use Weld to inject the EntityManager in your Tomcat applications. I found this link with a long discussion about it:

https://community.jboss.org/thread/179355

In short: use Seam 3.

Good luck

Comment for this answer: Well my problem is I can't get it to generate the tables, maybe this never came through

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:04/21/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<hibernate><tomcat><jpa><maven><jsf-2>', 'Title': "Can't get JPA with mysql running", 'ViewCount': 809, 'id': 10263175, 'user_AboutMe': None, 'user_AccountId': 940763.0, 'user_DisplayName': 'Karl Kildén', 'user_Reputation': 2395.0, 'user_Views': 529.0, 'user_id': 968878.0}}