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.