3

I'm trying to persist an object, after that i want to add 2 lists to that object and then update it (Since i can't persist the object with the lists).

All beign done inside a loop, the first iteration works just fine, from the second i get a EntityNotFoundException saying that the ID wasn't found to do the update.

private Foo foo;
private FooDao dao;

for(int i = 0 ; i<10 ; i++){
  foo = new Foo();
  foo.setVar(i);

  dao.save(foo);

  generateLists(); //creates a new list every interaction 
  foo.setCatList(catList);
  foo.setBarList(barList);

  dao.update(foo);
}

If i remove the Lists and the update it works fine.

The object:

@Entity
public class Foo {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  @Basic(optional = false)
  private Integer id;

  @Basic(optional = false)
  @NotNull
  private String var;

  @OneToMany(cascade = CascadeType.ALL)
  private List<Bar> barList;

  @OneToMany(cascade = CascadeType.ALL)
  private List<Cat> catList;

  //Getters and Setters
}

The DAO methods:

public void save(Foo foo) {
  this.manager.joinTransaction();
  this.manager.persist(foo);
  //this.manager.flush(); Tried this, but didn't work
}

public void update(Foo foo) {
  this.manager.joinTransaction();
  this.manager.merge(foo);
}

The error:

ERROR [org.jboss.as.ejb3] (EJB default - 1) javax.ejb.EJBTransactionRolledbackException: Unable to find foo with id 4
ERROR [org.jboss.as.ejb3.invocation] (EJB default - 1) JBAS014134: EJB Invocation failed on component FooDao for method public void FooDao.atualiza(Foo): javax.ejb.EJBTransactionRolledbackException: Unable to find Foo with id 4

ps: Using this generic approach to simplify, if needed i'll post my solution(or the mess that i call solution)

mortalis
  • 2,060
  • 24
  • 34
prabello
  • 556
  • 2
  • 14
  • 31
  • https://stackoverflow.com/questions/73234549/javax-persistence-entitynotfoundexception-unable-to-find-entity-with-id – KJEjava48 Aug 05 '22 at 07:07

4 Answers4

2

Because you are adding same lists again and again.Since you have OneToMany the second transaction says you already persisted that list.

An workaround would to change relation to ManyToMany

singhakash
  • 7,891
  • 6
  • 31
  • 65
  • I'm sorry, I forgot to add that the lists change, and the error I get is when he tries to update saying it didn't find the ID he persisted on save. – prabello Mar 04 '15 at 19:27
  • @bacoco are you sure that the list you generate second time dint have same Bar or Cat object? – singhakash Mar 04 '15 at 19:32
  • @singhaskash yeah, the lists are new with different values, and those lists are not yet persisted, i'm trying to persist then with the foo update with cascade. – prabello Mar 04 '15 at 19:57
  • Yes, only on the second iteration that the update fails. I get a javax.persistence.EntityNotFoundException: Unable to find object with id X – prabello Mar 04 '15 at 20:07
  • https://stackoverflow.com/questions/73234549/javax-persistence-entitynotfoundexception-unable-to-find-entity-with-id – KJEjava48 Aug 05 '22 at 07:06
1

Adding this:

@NotFound(action = NotFoundAction.IGNORE)

with @ManyToOne or @OneToOne is helpful because ...ToOne is FetchType.EAGER by default.

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10
0

I believe problem is with the transactions obtain the transaction and commit within the save method in your dAO.

this.manager.getTransaction().commit();

If an error comes it is because there is no active transaction it is why the flush fails. Be sure you are within a transaction, consider use EntityTransaction to begin , commit and end the transaction around your DAO methods.

If you don't have an active TX you won't be able to write entities to the database, and when you try to merge something the entity won't be found then this is the reason for what you are getting EntityNotFoundException

--- UPDATE

When you do the merge, what is the ID that the Foo object have ? I believe is not the correct one , or at least the last id inserted in the database, so what I suggest is find first and then merge.

Use

<T> T find(java.lang.Class<T> entityClass,
           java.lang.Object primaryKey)

Using the id, and verify the instance have the correct key

Koitoer
  • 18,778
  • 7
  • 63
  • 86
  • I'm sorry, you mean the join transaction? I didn't fully understood – prabello Mar 04 '15 at 19:31
  • Hibernate and JPA works together with transaction (JTA) that defines when touch the datase, it is required to flush something to the database have an active transaction before you write something in write operations, read more about it here – Koitoer Mar 04 '15 at 19:33
  • Check this example, before hibernate write something to the database, you shjould begin your TX, if you dont never will be save something in the database, and therefore the id that you are looking for will never exists.http://www.mkyong.com/hibernate/quick-start-maven-hibernate-mysql-example/ – Koitoer Mar 04 '15 at 19:35
  • I may not not have expressed myself right, the flush doesn't fail or generate an error, i tryed the em.flush() but it did not changed the result. As for the transaction it is being managed by the container, thus not being necessary the begin of the transaction. If i try to use .getTransaction i will get a java.lang.IllegalStateException: A JTA EntityManager cannot use getTransaction(), since JTA is managing the transaction. – prabello Mar 04 '15 at 20:39
  • Just to confirm that the transaction is working, if i remove the update and the childs, everything works, all the foo's are persisted, but they don't have the cats and bars associated. – prabello Mar 04 '15 at 20:40
  • The ID that the merge is using is the one generated on the save. foo.id is null, after save foo.id = 1 On the update it tries to update using the foo.id = 1, but i get the error saying javax.persistence.EntityNotFoundException: Unable to find foo with id 1 Foo is not yet saved on the DB, but "it should at least be" on the EM so when he tries to commit all the data he knows the ID's and such. – prabello Mar 04 '15 at 20:59
  • persistence context is open and close as the transactions does. – Koitoer Mar 04 '15 at 21:12
0

I got similar issue. Fixed by updating @ManyToOne annotation to:

    @NotNull
    @JoinColumn(nullable = false)
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private ParentEntityClass parent;

added: nullable = false, optional = false, fetch = FetchType.LAZY and, in addition to these - @NotNull (javax.validation.constraints.NotNull - probably not necessary, just to prevent null-ables :) )

Taraskin
  • 561
  • 9
  • 15