Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:51:33+00:00 2026-06-09T15:51:33+00:00

I am using Hibernate as JPA provider and in a function, I am creating

  • 0

I am using Hibernate as JPA provider and in a function, I am creating an instance of different entities. Once I call clear() for one entity, I can’t persist() the other entities. My code is pretty more complicated, and at a point I am obliged to call flush() and clear() for one type of entities (and NOT for the other types) in order to free some memory.

A simplification of my code is as follow:

@Transactional
void function()
{
    EntityType1 entity1 = new  EntityType1();
    EntityType2 entity2 = new  EntityType2();

    //...... do operations on entity1
    entity1.persist();
    entity1.flush();
    entity1.clear();

    //...... do operations on entity2
    entity2.persist();
}

When calling entity2.persist() I have the following error:
org.springframework.orm.jpa.JpaSystemException: org.hibernate.PersistentObjectException: detached entity passed to persist

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:51:34+00:00Added an answer on June 9, 2026 at 3:51 pm

    Most likely your entity2 already has an @Id assigned and therefore Hibernate is expecting to Update the existing entity rather than persist a new instance. This is why Hibernate considers entity2 to be detached.

    Calling entity2.merge() will provide you with an associated entity to the Hibernate session. Be warned that merge() returns a new instance of your entity that is the persisted copy.

    Example

    EntityType2 entityPersisted = entity2.merge();
    
    entityPersisted.getSomething();  // your persisted instance
    entity2.getSomething();  // still your detached instance
    

    Calling clear() evicts your entire session cache so all entites you have with an @Id will be considered detached.

    If you only want to remove the one entity from the session cache use evict()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring with Hibernate as a JPA provider and are trying to get
JPA/Hibernate missing column I am using JPA with Hibernate as the provider. I had
I am using JPA 2.0 with hibernate as the jpa-provider. The direction for my
I'm using JPA 1.0 with hibernate as my provider. Inside an entitymanager transaction, if
The JPA 2.0 Provider Hibernate is throwing exception while preparing configuration for entity manager
I am using Hibernate as JPA provider. I have for example this class: @Question(multipleChoiceQuestion)
We are developing a Java EE application using Hibernate as a JPA provider. We
I use EntityManager to save/update my entities in database and Hibernate as jpa provider.
We're using JPA with hibernate as the provider, we have a query that contains
I'm using hibernate as my jpa provider and want it to create a in-memory

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.