JPA EntityManager的问题

时间:2011-07-20 13:49:25

标签: jpa-2.0 eclipselink entitymanager

我是JPA的新手,我的JPA有问题。 我使用了实体管理器如下:

1: package com.icesoft.icefaces.samples.datatable.jpa;
    2: 
    3: import java.util.logging.Level;
    4: import java.util.logging.Logger;
    5: 
    6: import javax.persistence.EntityManager;
    7: import javax.persistence.EntityManagerFactory;
    8: import javax.persistence.Persistence;
    9: import javax.persistence.Query;
   10: /**
   11:  * @author MyEclipse Persistence Tools
   12:  */
   13: public class EntityManagerHelper {
   14:     
   15:     private static final EntityManagerFactory emf; 
   16:     private static final ThreadLocal<EntityManager> threadLocal;
   17:     private static final Logger logger;
   18:     
   19:     static {
   20:         emf = Persistence.createEntityManagerFactory("tutorialPU");         
   21:         threadLocal = new ThreadLocal<EntityManager>();
   22:         logger = Logger.getLogger("tutorialPU");
   23:         logger.setLevel(Level.ALL);
   24:     }
   25:         
   26:     public static EntityManager getEntityManager() {
   27:         EntityManager manager = threadLocal.get();      
   28:         if (manager == null || !manager.isOpen()) {
   29:             manager = emf.createEntityManager();
   30:             threadLocal.set(manager);
   31:         }
   32:         return manager;
   33:     }
   34:     
   35:      public static void closeEntityManager() {
   36:         EntityManager em = threadLocal.get();
   37:         threadLocal.set(null);
   38:         if (em != null) em.close();
   39:     }
   40:     
   41:     public static void beginTransaction() {
   42:         getEntityManager().getTransaction().begin();
   43:     }
   44:     
   45:     public static void commit() {
   46:         getEntityManager().getTransaction().commit();
   47:     }  
   48:     
   49:     public static Query createQuery(String query) {
   50:         return getEntityManager().createQuery(query);
   51:     }
   52:     
   53:     public static void log(String info, Level level, Throwable ex) {
   54:         logger.log(level, info, ex);
   55:     }
   56:     
   57: }

我的persistence.xml是:

    <persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">

      <persistence-unit name="OrderEJB" type="JTA">
        <jta-data-source>movieDatabase</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>   
      </persistence-unit>
    </persistence>

我有两个问题: 1 - 当我调用getEntityManager()。persist(anObject); 它不保存数据库中的任何东西既不会在容器日志中产生任何错误! 2 - 每件事似乎都没问题但是当我重新部署应用程序(没有重新启动服务器)时,JPA的每个操作都会失败并出现以下异常:

java.lang.IllegalArgumentException: The type [null] is not the expected
[EntityType] for the key class [class entity.MyClass].

如果我重新启动服务器,那么每件事都没问题!

我正在使用netbeans 6.9.1,glasfish 3.1,eclipselink(JPA 2.0) 那怎么能解决我的问题呢? 提前谢谢

2 个答案:

答案 0 :(得分:0)

  1. 您必须提交EntityManager的事务,以使更改成为持久性的 (尝试manager.getTransaction().commit()

  2. 发布完整的堆栈跟踪。

答案 1 :(得分:0)

eclipselink的默认行为似乎是插入查询仅在select查询之前,调用flush()或在事务结束时(在提交时自动刷新)

存在具有不同行为的持久性提供程序(例如,hibernate)。但在你的情况下尝试调用em.flush()或关闭事务(或确保容器正确执行)