JPA does not support writing to database

I am just trying to find out JSF and JPA, but whenever I try to store an object in a database, it does not seem to write. Here is the code I'm using:

@Named @ManagedBean @SessionScoped public class BestemmingController implements Serializable{ @PersistenceUnit(unitName="RealDolmenTravelShopPU") @PersistenceContext(unitName="RealDolmenTravelShopPU") EntityManagerFactory emf = null; public void submit(){ try{ emf = Persistence.createEntityManagerFactory("RealDolmenTravelShopPU"); EntityManager em = emf.createEntityManager(); //EntityTransaction et = em.getTransaction(); //et.begin(); Bestemming nieuweBestemming = new Bestemming(); Land gezochtLand = em.find(Land.class, selectedLand); nieuweBestemming.setLand(gezochtLand); nieuweBestemming.setNaam(bestemmingNaam); em.persist(nieuweBestemming); //et.commit(); //em.flush(); em.close(); }catch (Exception e){ e.printStackTrace(); }finally{ emf.close(); } } 

I tried using EntityTransaction, but it just stopped my application without any errors or anything else. So I left him, but still he did not forget. So, I tried to split the flash separately, but it did nothing either. I very much doubt why this does not work. It was probably a beginners mistake, but I would love it if someone here could help me.

Thanks in advance!

+4
source share
2 answers

First, can you write in magazines? Starting a transaction when specifying a unit of measure for duration uses JTA, it throws an exception, therefore, most likely, you just missed the exceptions in the container log files.

Secondly, it is a JTA PU, so it needs the JTA transaction that EM is associated with, and you want to enter em, rather than create a factory yourself. Check out the JPA application server examples here to see how they are configured: http://wiki.eclipse.org/EclipseLink/Examples/JPA

0
source

Hey, I found out why the transaction was not executed: the implementation I used did not use JTA, it used the RESOURCE_LOCAL save block. This was what I just looked through when I created my project.

It's good that my friend told me to check the server logs.

0
source

All Articles