简体   繁体   中英

Hibernate MySQL connection not working

I just want to try a little hibernate example, but I keep getting this error on runtime:

Exception in thread "main" java.lang.UnsupportedOperationException: The user must supply a JDBC connection

I can not see where I made a mistake. Here is my persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>
<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_1_0.xsd" version="1.0">
    <persistence-unit name="Example" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.hibernatetest.Employee</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.url" value="jdbc:mysql://localhost:3306/TestDB" />
            <property name="javax.persistence.user" value="root" />
            <property name="javax.persistence.password" value="" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hbm2ddl.auto" value="update"></property>         
        </properties>
    </persistence-unit>
</persistence>

I can not see any mistake here. As I use Maven, I added these dependencies to the pom

  <dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.6.0.Final</version>
</dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>3.6.0.Final</version>
        </dependency>

      <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

In my mind, this should do the job.

Last but not least, my Java Code:

 public void storeEmp(Employee empl) {
        try {
            em.getTransaction().begin();
            em.persist(empl);
            em.getTransaction().commit();
        } catch (PersistenceException e) {
            e.printStackTrace();
        }
    }

Employee is a simple POJO following the beans pattern, annotated with @Entity.

I really can not find my mistake?

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/TestDB" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hbm2ddl.auto" value="update"></property>         
        </properties>

Try these properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM