entitymanager persist不会保存任何内容,也不会抛出任何错误

时间:2017-03-17 13:51:55

标签: entitymanager persist

当我调用entitymanager持久化或合并对象时,我的jpa(eclipselink)出现问题。它不会在数据库中插入任何行,它不会抛出任何错误。但如果我调用entitymanager来查找或执行查询,我会得到很好的结果。所以我认为这是交易的问题。

**My entity:

@Entity
public class Person implements Serializable{ 
    private static final long serialVersionUID = -5088872371835299410L; 
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private long id;
    private String nom;

    public Person() {}
    public long getId() {
        return id;
    }
    public String getNom() {
        return nom;
    }
    public void setNom(String nom) {
        this.nom = nom;
    }

**My dao:

@Repository
public class PersistenceDao implements IPersistenceDao{
    private static EntityManagerFactory emf = null;
    private EntityManager session;
    private EntityTransaction trx;

     private EntityManager getSession(){
        if(null == emf) 
            emf = Persistence.createEntityManagerFactory("Spring07");
        session = emf.createEntityManager();
        return session;
    }

    public EntityTransaction getTrx(){
        if(null == trx || !trx.isActive())
            trx = this.session.getTransaction();
        return trx;
    }

    public void beginTransaction(){
        this.trx.begin();
    }

    public void commit(){
        this.trx.commit();
    }

    public void flush(){ 
        this.session.flush();
    }

    public void rollBack(){ 
        if(null != trx && trx.isActive())
            this.trx.rollback();
    }
}

 *** My service

@Service
public class Service implements IService{
    @Autowired
    IPersistenceDao dao; 

    public Service(){}

    @Override
    public void add(Object obj) { 
        try { 
            dao.beginTransaction(); 
            dao.add(obj);
            dao.flush(); 
            dao.commit(); 
        } catch (Exception e) { 
            dao.rollBack();
        } finally {
            dao.close();
        }
    }

    @Override
    public List<Object> listAll(Class clazz) {
        return dao.listALl(clazz);
    }

    @Override
    public Object find(Class clazz, Object id) {
         return dao.find(clazz, id);
    }

    public Object update(Object obj) {
           Object o = null;
           try {
                logger.info(" Begin transaction");
                dao.beginTransaction();
                logger.info(" persisting obj into db");
                o = dao.update(obj);
                logger.info(" commit");
                dao.commit();  
            } catch (Exception e) { 
                dao.rollBack();
            }  
           return o;
        }
}

*** My applicationConext.xml
  contains just:
  <context:component-scan base-package="com.myproject"/>
this package contains two subpackages dao and service who contains my dao and my service

**** My class Main
public class Main {
    public static void main(String[] args) {
ApplicationContext  context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        IService iservice = (Service) context.getBean("Service");
        Person p = new Person();
        p.setNom("jean");

        iservice.add(p);
}
}

**** My persistence is correct.
because if I do:
Person p = new Person();
p.setNom("rchxxx"); 

 Person p = new Person();
  p.setNom("jean");
EntityManager manager = Persistence.createEntityManagerFactory("Spring07").createEntityManager();

        manager.getTransaction().begin();
        manager.persist(p);
        manager.getTransaction().commit();
        manager.close();

它在数据库中插入一行,但如果使用spring则不会。 我使用@transactional,flush,...总是同样的问题没有插入rw并且没有抛出错误。 请帮忙。

0 个答案:

没有答案