Khalender
Khalender

Reputation: 21

JPA persistence issue

I am having trouble in recording my database in JPA. My settings are set for JPA and I am using hibernate as a service. I have the following code.

EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
List<Beer> beiren = FindBeers();

List<Beer> aa = beiren.stream().map(x-> { 
    x.setPrice(x.getPrice()*1.02F);
    return x;
}).collect(Collectors.toList());

Collections.copy(beiren,aa);
beiren.stream().forEach(System.out::println);
tx.commit();
em.close();
emf.close();

when I change the price value in beiren list I am expecting it to have an effect in the database when the entity manager is closed. since it is in a kind of persistent field I guess. if everything I do correctly here, I might have something else wrong in my class constructions.

could you please help me? what am I not doing correctly? thank you...

Upvotes: 2

Views: 81

Answers (1)

Khalid Shah
Khalid Shah

Reputation: 3232

Before Commit add this line. If you are looking for something in EntityManager which will save or update a collection, the answer is no. You will have to loop. EntityManager.merge() API will update if record is existing or else will insert a new record.

aa.stream().foreach(em::merge);

Hope it will work.

Upvotes: 1

Related Questions