I have a full Java EE web application with a presentation layer and a database. I'm using derby with glassfish 3.1 and JPA to handle persistence. I've created a Read ok but now I'm having troulbe doing a Create and persisting to the database. I think I'm close but something is not right with the way I'm trying to do the create.
Here is my EAO code:
/**
* Session Bean implementation class XRSSeao
*/
@Stateless
@LocalBean
public class XRSSeao {
@PersistenceContext
EntityManager em;
public XRSSeao() {}
public void addEvent(String update){
Feed feed = new Feed();
feed.setStatus(update);
feed.setId(2);
em.getTransaction().begin();
em.persist(feed);
em.flush();
em.clear();
em.getTransaction().commit();
}
}
This will be called from another EJB. I also don't want to have to set the ID since that is the primary key I want that generated whenever I call the persist method. The error I get when I test it is:
"Caused by: java.lang.IllegalStateException: Exception Description: Cannot use an EntityTransaction while using JTA."
If you don't know what the problem is with this code but can provide an example of simple persisting with autogenerated primary key that would be just as helpful.
This is my read method that is working:
public String lastUpdate(){
String resultString;
Query q = em.createQuery("SELECT x FROM Feed x WHERE x.id = 1");
List<Feed> ListofStatus = q.getResultList(); //alternatively you can use getResultList() for non 1 object is expected.
Feed returnStatusObject = ListofStatus.get(0);
resultString = returnStatusObject.getStatus();
return resultString;
}
If I don't need to use Transaction() I haven't found an example online that does not use it for create.