0

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.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
Randnum
  • 1,360
  • 4
  • 32
  • 48

1 Answers1

1

You're using EJB/JTA with transaction-type="JTA". The container will then manage the transactions itself. You can control the transactions by @TransactionAttribute and @TransactionAttributeType annotations on the EJB class/methods (which by default should however not be necessary). The tutorials which you've read apparently did not use EJB/JTA, but just application managed transactions with transaction-type="RESOURCE_LOCAL". You should read JPA tutorials which are targeted on use with EJB/JTA.

To fix your problem -I assume that you want to keep using EJB/JTA-, replace

em.getTransaction().begin();
em.persist(feed);
em.flush();
em.clear();
em.getTransaction().commit();

by

em.persist(feed);

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • What do you mean the container will manage the transaction? Does that mean I don't have to actually make calls to the transaction itself I can just create a new entity and because it's been annotated as an @Entity the manager will then know to manage the transaction? – Randnum Nov 14 '11 at 18:39
  • Yes, you don't need to manage them yourself. No, it's because you're using `@Stateless` EJB with `transaction-type="JTA"`. – BalusC Nov 14 '11 at 18:41
  • if Feed class is my @Entity object what would a call like that look like? "Feed feed = new Feed();" ? Would that be all I needed? – Randnum Nov 14 '11 at 18:44
  • 1
    I have no idea what you're asking, but that line of code just creates an **unmanaged** entity. Only when you `persist()`, it will be persisted in the DB and its new ID will be set (if you have the proper `@Id` and `@GeneratedValue` annotations). Normally, you should not need to manually set the ID. – BalusC Nov 14 '11 at 18:45
  • Great, that just worked. In case others are reading this. I just created a new entity then called em.persist(entity); BalusC helpful as always. – Randnum Nov 14 '11 at 20:00