You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JPA/Hibernate报错:detached entity passed to persist问题求助

Hey there! Let's break down why you're getting that org.hibernate.PersistentObjectException: detached entity passed to persist: model.Employee error and fix it up.

What's causing this error?

The core issue here is how JPA/Hibernate handles entity states, paired with your Employee entity setup:

  • Your eid field uses @GeneratedValue(strategy = GenerationType.AUTO), which tells Hibernate to automatically generate the primary key value for new entities. You don't need to set this manually.
  • The persist() method is only meant for transient entities—brand new objects that don't have a primary key value yet. If you set the eid yourself (either via the constructor with an eid parameter or setEid()), Hibernate sees this entity as detached (like it was already persisted before and is now outside the EntityManager's control) and throws this error because persist() can't handle detached entities.

Looking at your code, it's likely you were setting the eid value in your incomplete CreateEmployee class, which triggered this problem.

How to fix it

Here are the straightforward solutions tailored to your setup:

  1. Let Hibernate handle the primary key (recommended)
    Since you're using @GeneratedValue, stop manually setting the eid entirely. Use the no-arg constructor and skip calling setEid(). Here's what your corrected CreateEmployee code would look like:

    package service;
    import javax.persistence.EntityManager;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.Persistence;
    import model.Employee;
    
    public class CreateEmployee {
        public static void main( String[ ] args ) {
            EntityManagerFactory emfactory = Persistence.createEntityManagerFactory( "sample" );
            EntityManager entitymanager = emfactory.createEntityManager( );
            
            entitymanager.getTransaction( ).begin( );
            
            // Use the no-arg constructor, don't set eid
            Employee employee = new Employee();
            employee.setEname("Alice Smith");
            employee.setSalary(6500.0);
            employee.setDeg("Software Engineer");
            
            entitymanager.persist(employee); // This will work now
            entitymanager.getTransaction( ).commit( );
            
            // Don't forget to close resources!
            entitymanager.close();
            emfactory.close();
        }
    }
    

    You can also optionally modify your Employee class to remove the constructor that takes eid—this will prevent accidental manual key assignment in the future.

  2. If you must set the primary key manually (not recommended for auto-generated keys)
    If you have a specific reason to set eid yourself, replace persist() with merge(). merge() works with detached entities and will either update an existing record or insert a new one if the key doesn't exist:

    Employee employee = new Employee(1, "Bob Brown", 7000.0, "Senior Developer");
    entitymanager.merge(employee); // Use merge instead of persist
    

    Note: This defeats the purpose of using @GeneratedValue, so only do this if you have a clear need.

  3. Bonus: Add auto-table creation to your persistence.xml
    As a JPA beginner, adding this property to your persistence.xml will let Hibernate automatically create/update your database tables, saving you manual setup work:

    <properties>
        <!-- Your existing DB properties -->
        <property name="hibernate.hbm2ddl.auto" value="update"/>
    </properties>
    

Quick recap

This error happens because you tried to use persist() on an entity that already had a primary key value (which Hibernate interpreted as detached). The simplest fix is to let Hibernate generate the key for you by not setting eid manually.

内容的提问来源于stack exchange,提问作者Harvey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:30:14