Page 1 of 2

EntityManager is injected but unable to persist?

Posted: Tue Jul 01, 2014 1:59 am
by gowthamgutha
I am able to find using the em.find() method but I am not able to either insert or delete the data. When i call em.remove(myEntity) nothing happens in the database.



I have the entities something like this..

Code: Select all

@Entity
@Table(name = "FIRST_ENTITY")
class FirstEntity
{
private static final Long serialVersionUid=1L;
@Id
@GeneratedValue
private Long id;
@Column(name="MESSAGE",nullable=false)
private String message;
}


and a dao class for this..

Code: Select all

@Repository("myFirstDao")
class FirstDaoImpl implements FirstDao
{
@PersistenceContext(unitName="myUnit")
EntityManager em;

           public FirstEntity save(FirstEntity entity)
           {
           return em.merge(entity);
           }

}


and the service class.

Code: Select all

@Service("myFirstService")
public class MyFirstServiceImpl implements MyFirstService
{
@Resource(name="myFirstDao")
MyFirstDao myFirstDao;

           public FirstEntity save(FirstEntity entity)
           {
           return myFirstDao.save(entity);
           }
}

Note: The entity manager is container managed.

When I am trying to do something like this in another service class..

Code: Select all

@Service("anotherService")
public class AnotherServiceImpl
{

@Resource(name="myFirstService")
FirstService firstService;

      public void myMeth()
      {
      FirstEntity f=new FirstEntity();
      f.setMessage("Hello");

      FirstEntity f1=firstService.save(f);

      System.out.println("The generated id is "+f1);

      }
}


Here, I am getting the generated id, but the entity is not stored into the database. When I am trying to do like this in the FirstDaoImpl - save()

Code: Select all

em.getTransaction().begin();
em.save(entity);
em.getTransaction().commit();


I am getting the following error : Cannot call getTransaction() on a container managed bean

And when I do something like this..

Code: Select all

em.save(entity);
em.flush();


I am getting, no transaction is in progress error.

I would like to know, how could I solve it. Thanks in advance. Hope you will reply as soon as possible.

Re: EntityManager is injected but unable to persist?

Posted: Tue Jul 01, 2014 11:40 am
by phillipuniverse
You definitely have to have a transaction active in order to save anything. The way that we usually do this is front the dao with a service and add @Transactional to the method.

Re: EntityManager is injected but unable to persist?

Posted: Thu Jul 03, 2014 12:25 am
by gowthamgutha
I added, but still no use.

Thank you.

Re: EntityManager is injected but unable to persist?

Posted: Thu Jul 03, 2014 2:51 am
by gowthamgutha
When I put @Transactional i got.

No bean named 'transactionManager' is defined. I did the following to my dao classes and it worked.

Code: Select all

@Transactional(value="blTransactionManager")
class MyDaoImpl
{
@PersistenceContext(unitName="blPU")
private EntityManager em;

       @Transactional(value="blTransactionManager")
       public MyEntity save(MyEntity e)
       {
       return em.merge(e);
       }

       @Transactional(value="blTransactionManager", readOnly=true)
       public List<MyEntity> read()
       {
       // .. code
       }
}


No need of beginTransaction(), commit() and all others! It worked.

Thanks for your reply

Re: EntityManager is injected but unable to persist?

Posted: Fri Nov 13, 2015 11:53 am
by ninja
Thanks a lot for asking this question. It helped a lot.
You surely deserve a treat from me.... :D :)