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 开发者_JS百科{
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
you have to commit the EntityManager's transaction that the changes will be made persistent (try
manager.getTransaction().commit()
)Post the complete stacktrace.
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)
精彩评论