1

I have the entities something like this..

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

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

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

}

and the service class.

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

@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()

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

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.

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97

3 Answers3

3

Instead of doing Programatic transaction management, you should use declarative. All that means is add another annotation to your service. I'm not sure what framework you're using, but it might be @Transactional if you're using Spring, or if you're using EJB3, add @TransactionAttribute(TransactionAttributeType.REQUIRED). After you do this, take out these lines em.getTransaction().begin(); and em.getTransaction().commit(); Your container should now start/stop a transaction for you. Much easier!

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84
  • I am getting `no bean named transactionManager is defined` error. – JavaTechnical Jul 01 '14 at 04:13
  • @JavaTechnical http://stackoverflow.com/questions/10719646/no-bean-named-transactionmanager-is-defined http://stackoverflow.com/questions/19594397/spring-junit-no-bean-named-transactionmanager-is-defined http://stackoverflow.com/questions/13682111/org-springframework-beans-factory-nosuchbeandefinitionexception-no-bean-named – Ankur Singhal Jul 01 '14 at 04:24
  • Acutally, it worked for one entity. I don't know why for this entity is not working. – JavaTechnical Jul 01 '14 at 04:29
  • There is no transaction manager defined previously for another entity, it worked for that entity but not for this. – JavaTechnical Jul 01 '14 at 04:38
  • Make sure every service you are using is declaratively marked as transactional – Jonathan S. Fisher Jul 01 '14 at 15:36
1

Using Spring declarative transaction management might be a good option, with Spring managing on its own. Also do check for the Transaction boundaries, from where it is getting started and how are you propagating to Service/DAO layers.

This might be useful for you

http://docs.spring.io/spring/docs/3.0.x/spring-framework-reference/html/transaction.html#tx-propagation

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

I did the following and it worked. All that is needed is to specify the name of the transaction manager. When an error like the 'No bean named transactionManager is defined' occurs, most probably the transactionManager would have been named with a different name. Which i didn't note previously.

The transaction manager is defined in an xml file which is a part of the framework I am using (it is archived inside a jar file, so am I unable to know the transaction manager bean name). For pre-defined classes provided in that jar, no @Transactional is defined.

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

           @Transactional(value="myTransactionManager")
           public FirstEntity save(FirstEntity entity)
           {
           return em.merge(entity);
           }

}

Thanks for the valuable reply of the @exabrial and @ankur-singhal

JavaTechnical
  • 8,846
  • 8
  • 61
  • 97