Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: error

I have 2 entities in a JPA project:

A category and a question. so each Category will have a list of questions and each question will be part of a category (OnetoMany relation). I manage the bi-directional relationship through the set/add methodes in both entities :

Question :

    @ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "Qcategory")
private Category category;

public void setCategory(Category category) {
 this.category = category;

 if (category != null && !category.getQuestions().contains(this)) {
 category.addQuestion(this);
 }
 }

Category :

@OneToMany(cascade = { CascadeType.ALL }, mappedBy = "category")
private List<Question> questions= new ArrayList<Question>();


 public void addQuestion(Question question) {
 this.questions.add(question);

 if (question.getCategory() != this) {
 question.setCategory(this);
 }

 }

I first create a category.

Category category1 = new Category();
category1.setName = "exampleCategory";

I add this to the db through my repository (added in a similar way as the question addOrUpdate as below)

After that I create an question

Question question1 = new Question();

I set the category of the question to category1

question.setCategory = category1;

After this I also try to persist the question to the db by calling the addOrUpdate method below. I then get an error :

....:javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: jpa.entities.Category

I use a repository method like :

@Override
public boolean addOrUpdate(Question question) {
    EntityManagerFactory emf = JPARepositoryFactory
            .getEntityManagerFactory();
    EntityManager em = emf.createEntityManager();
    EntityTransaction tx = em.getTransaction();
    tx.begin();

    Question tempQuestion = null;
    try {
        if (question.getId() != null) {
            tempQuestion = em.find(Question.class,
                    question.getId());
        }

        if (tempQuestion == null) {
            em.persist(question);
        } else {

            tempQuestion .setCategory(question.getCategory());
            ... (other setters)
            tempQuestion = em.merge(question);
        }
    } catch (Exception e) {
        ....logging...      }
    tx.commit();
    em.close();
    emf.close();
    return true;
}

Any suggestion would be more then welcome.

like image 739
Darth Blue Ray Avatar asked Mar 10 '26 12:03

Darth Blue Ray


1 Answers

So you're only allowed to call persist once on an entity. That error means that you've already called persist on that Question object that is being passed in, but you did so in another transaction. If you want to reattach the Question object to the persistence context you need to call merge or reload it from the database.

like image 71
Pace Avatar answered Mar 12 '26 03:03

Pace



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!