How To Solve The Issue Jpa/Hibernate: Detached Entity Passed To Persist?

504    Asked by DaniloGuidi in Java , Asked on Aug 2, 2023

What is meant by a detached entity passed to persist? There is a JPA-persisted object structure that has many-to-one relation: The single account has various transactions while a transaction contains a single account. The code is given below:

@Entity
public class Transaction {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne(cascade = {CascadeType.ALL},fetch= FetchType.EAGER)
    private Account fromAccount;
....
@Entity
public class Account {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @OneToMany(cascade = {CascadeType.ALL},fetch= FetchType.EAGER, mappedBy = "fromAccount")
    private Set transactions;

It is possible to make an Account object, include transactions, and persist the account object properly. However, on making the transaction, utilizing a previously present persisted account, and persisting the transaction, there’s an exception like the one below:

Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.paulsanwald.Account at org.hibernate.

Hence. It’s possible to persist the account that has transactions, but not the transaction having an account. I wondered if this was due to the account not being attached, however I am receiving the same exception yet.

if (account.getId()!=null) {
    account = entityManager.merge(account);
}
Transaction transaction = new Transaction(account,"other stuff");
 // the below fails with a "detached entity" message. why?
entityManager.persist(transaction);

What is the way to properly save transactions, linked with a previously persisted account object?

Answered by Diana Campbell

This refers to a regular bidirectional compatibility issue. The setters must be fixed in both parts of the bidirectional connection. After altering the settlers you wish to announce the Entity access type as “Property”. The right way to do so is by moving ALL the explanations from the member properties to the respective getters. Do not mix Field and Property access standards within the entity class until the behavior is not defined by the JSR-317 specs. A detached entity refers to the object having the same ID like the entity on the persistence store. However, it is not a part of the persistence context anymore.

You can also utilize the CascadeType . MERGE rather than the CascadeType . PERSIST or CascadeType . ALL. You can also erase cascades from the child entity. The code will be like the one below:
@Entity class Transaction {
    @ManyToOne // no cascading here!
    private Account account;
}
(FETCHType . EAGER can be erased since it’s the default for @ManytoOne).

The Java Online Certification Training offered at JanBask Training provides experience like offline classes to help candidates master the concepts of Java in a better way to face the tough job market scenario. The course guarantees job success and lets you qualify the certification exam through intensive, and expert-led virtual sessions and hands-on practical assignments. The course will teach you to create and configure the Java codes to help you gather, extract, and examine the data relevant to the customer base. Furthermore, JanBask Training helps you get job-ready and face the tough competitive market with confidence.



Your Answer

Interviews

Parent Categories