线程“main”中的异常javax.persistence.PersistenceException:无法构建实体管理器工厂

时间:2018-03-22 06:49:26

标签: java maven jpa

我使用Java,Maven和JPA进行练习。 但我遇到这个错误。我错过了什么?

  

记录,执行应用程序后收到的消息:

Exception in thread "main" javax.persistence.PersistenceException: Unable to build entity manager factory
  

这是实体类:

 package objis.dto.entity;

    import java.io.Serializable;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @Entity
    @Table(name = "users")
    public class User implements Serializable {
        @Id
        @GeneratedValue(strategy= GenerationType.AUTO)
        @Column(name="id")
        private Long id;
        @Column(name="identifiant")
        private String login;
        @Column(name="password")
        private String pass;
        private String mail;

        public User() {
        }
        // getter and setter
  

这是数据库语句的dao类:

package dao;

import com.objis.dto.UserDTO;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import objis.dto.entity.User;

public class UserImpl implements UserDao {
 //Ouverture d'une unité de travail jpa
 EntityManagerFactory emf = Persistence.createEntityManagerFactory("dto");
 EntityManager entityManager = emf.createEntityManager();

 @Override
 public User findByLogin(String login) {
  //Création de l'objet Query à l'aide d'un requête JPQL
  Query query = entityManager.createQuery("Select s From users s where u.login= :login");
  query.setParameter("login", login);
  User u = (User) query.getSingleResult();
  entityManager.close();

  return u;
 }

 @Override
 public List findAll() {
  Query query = entityManager.createQuery("Select s From users s");
  List list = query.getResultList();
  entityManager.close();

  return list;
 }

 @Override
 public User addUser(User user) {
  // testons d'abord si l'utilisateur n'existe pas dans la base de données
  User u = findByLogin(user.getLogin());

  if (u == null) {
   entityManager.persist(user);
  } else {
   entityManager.merge(user);
  }
  entityManager.close();

  return user;
 }
  

这里是测试我的dao的主类:

  public class Main {
  public static void main(String[] args) {
   testApp();
  }

  public static void testApp() {
   //
   EntityManagerFactory emf = Persistence.createEntityManagerFactory("dto");
   EntityManager em = emf.createEntityManager();
   //Ouverture d'une transaction
   EntityTransaction transaction = em.getTransaction();
   transaction.begin();
   //
   User u = new User("KRA", "steinerpassword", "kra@yahoo.fr");
   User u2 = new User("Bamba", "bambapassword", "bamba@yahoo.fr");
   User u3 = new User("Boris", "borispassword", "boris@yahoo.fr");
   User u4 = new User("Nicole", "nicolepassword", "nicole@yahoo.fr");
   em.persist(u);
   em.persist(u2);
   em.persist(u3);
   em.persist(u4);
   em.close();
   emf.close();
  }
 }
  

persistence.xml文件,看到错误消息我告诉自己我在这个文件中犯了错误。可能是一个糟糕的配置,但我没有看到确切的位置。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
  xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="dto" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc:mysql://localhost:3306/dto?zeroDateTimeBehavior=convertToNull</jta-data-source>
    <class>objis.dto.entity.User</class>
    <exclude-unlisted-classes>true</exclude-unlisted-classes>
    <properties>
      <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/dto"/>
    </properties>
  </persistence-unit>
</persistence>

0 个答案:

没有答案