JPA EntityManager 的问题

发布于 2024-11-25 09:33:16 字数 2971 浏览 1 评论 0原文

我是 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) 那么我该如何解决我的问题呢? 提前致谢

i am new to JPA, i have a problem with my JPA .
i have used the entity manager as follows:

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: }

my persistence.xml is:

    <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>

i have two problems:
1-when i call the getEntityManager().persist(anObject);
it does not save any thing in database neither produce any error in container log!!
2-every things seems ok but when i redploy the application(without restarting server) every operations of JPA fail with the following exception:

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

if i restart the server every thing is okay!!!

i am using netbeans 6.9.1, glasfish 3.1, eclipselink (JPA 2.0)
so how could i resolve my problems?
thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

明月松间行 2024-12-02 09:33:16
  1. 您必须提交 EntityManager 的事务,更改才会持久化
    (尝试 manager.getTransaction().commit())

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

  1. you have to commit the EntityManager's transaction that the changes will be made persistent
    (try manager.getTransaction().commit())

  2. Post the complete stacktrace.

凉栀 2024-12-02 09:33:16

eclipselink 的默认行为似乎是仅在选择查询之前完成插入查询,当您调用lush() 或在事务结束时(提交时自动刷新),

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

The default behaviour of eclipselink seems to be that insert queries are done only before select queries, when you call flush() or at the end of the transaction (auto-flush on commit)

there are persistence providers with a different behaviour (hibernate for example). but in your case try calling em.flush() or close the transaction (or make sure you container does that properly)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文