没有为entitymanager命名的持久性提供程序 - 尝试了一切

时间:2013-01-24 15:53:54

标签: jpa persistence.xml

我是JPA的新手,我得到了这个臭名昭着的错误“没有名为entitymanager的持久性提供程序”。我在谷歌上进行了广泛的搜索,并尝试了所有可用的解决方案,在任何程度上我都不敢。

堆栈跟踪

javax.persistence.PersistenceException: No Persistence provider for EntityManager named suplink
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at com.supinfo.suplink.util.PersistenceManager.getEntityManagerFactory(PersistenceManager.java:13)

的persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="suplink" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/SupLink" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
    </properties>
     <class>com.supinfo.suplink.entity.User</class>
</persistence-unit>

的PersistenceManager

public class PersistenceManager {
private static EntityManagerFactory emf;

private PersistenceManager() { }

public static EntityManagerFactory getEntityManagerFactory() {
    if(emf == null) {
        emf = Persistence.createEntityManagerFactory("suplink");
    }
    return emf;
}

public static void closeEntityManagerFactory() {
    if(emf != null && emf.isOpen()) emf.close();
}
}

感谢您的帮助:)

2 个答案:

答案 0 :(得分:1)

尝试删除transaction-type =“RESOURCE_LOCAL” 在没有此代码的情况下在我的机器上运行

答案 1 :(得分:0)

分级练习祝你好运,试试下面的代码;)

public class PersistenceManager {

    private static final EntityManagerFactory emf;
    private static final ThreadLocal<EntityManager> threadLocal;
    private static final Logger logger;

    static {
        emf = Persistence.createEntityManagerFactory("SupLink");
        threadLocal = new ThreadLocal<EntityManager>();
        logger = Logger.getLogger("SupLink");
        logger.setLevel(Level.ALL);
    }

    public static EntityManager getEntityManager() {
        EntityManager manager = threadLocal.get();
        if (manager == null || !manager.isOpen()) {
            manager = emf.createEntityManager();
            threadLocal.set(manager);
        }
        return manager;
    }

    public static void closeEntityManager() {
        EntityManager em = threadLocal.get();
        threadLocal.set(null);
        if (em != null)
            em.close();
    }

    public static void beginTransaction() {
        getEntityManager().getTransaction().begin();
    }

    public static void commit() {
        getEntityManager().getTransaction().commit();
    }

    public static void rollback() {
        getEntityManager().getTransaction().rollback();
    }

    public static Query createQuery(String query) {
        return getEntityManager().createQuery(query);
    }

    public static void log(String info, Level level, Throwable ex) {
        logger.log(level, info, ex);
    }

}