JPA 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) 那么我该如何解决我的问题呢? 提前致谢
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须提交 EntityManager 的事务,更改才会持久化
(尝试
manager.getTransaction().commit()
)发布完整的堆栈跟踪。
you have to commit the EntityManager's transaction that the changes will be made persistent
(try
manager.getTransaction().commit()
)Post the complete stacktrace.
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)