简体   繁体   中英

Detached entity passed to persisit

I am new to hibernate. I have written a simple program with four files in it. Employee.java it is a bean class with getters n setters in it.

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org dtd/hibernate-configuration-3.0.dtd">      
  <hibernate-configuration>  
          <session-factory>  
        <!-- property name="hbm2ddl.auto">update</property--> 
        <property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>  
        <property name="connection.url">dbUrl</property>  
        <property name="connection.username">uname</property>  
        <property name="connection.password">pass</property >
        <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>  
    <mapping resource="Employee.hbm.xml"/>  
    </session-factory>  
      </hibernate-configuration>

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping>
<class name="hibernate_prj.Employee" table="EMPLOYEE">
  <meta attribute="class-description">
     This class contains the employee detail. 
  </meta>
  <id name="id" type="int" column="empid">
     <generator class="native"/>
  </id>
  <property name="firstName" column="empname" type="string"/>
  <property name="dept" column="empdept" type="string"/>
  <property name="salary" column="empsal" type="int"/>
 </class>
</hibernate-mapping>

StoreData.java

package hibernate_prj;

import org.hibernate.Session;  
import org.hibernate.SessionFactory;  
import org.hibernate.Transaction;  
import org.hibernate.cfg.Configuration;

public class StoreData {

public static void main(String[] args) {  

    Configuration cfg=new Configuration();

        cfg.configure("hibernate.cfg.xml");

    //creating seession factory object  
    @SuppressWarnings("deprecation")
   SessionFactory factory=cfg.buildSessionFactory();  

    //creating session object  
    Session session=factory.openSession();  

    //creating transaction object  
    Transaction t=session.beginTransaction();  

    try{
    Employee e1=new Employee();  

    e1.setId(115);  
    e1.setFirstName("sonoo");  
    e1.setDept("jaiswal");  
    e1.setSalary(100);

    session.persist(e1);//persisting the object  

    t.commit();//transaction is commited  
        session.close();  

      }catch(Exception e)
      {
          System.out.println("E "+e.toString());
      }
    System.out.println("successfully saved");  
 }  
}

I have created table using the following sql statement

 create table employee (empid number(2,2),empname VARCHAR2(30), empdept varchar2(10), empsal number(5,2));

But when I run this program i get the following error:

org.hibernate.PersistentObjectException: detached entity passed to persist: hibernate_prj.Employee

Please help me to resolve this

You have set the id of the employee (to 115) but also configured it to get generated. Now Hibernat thinks the entity is already persisted, since it has an id != null, but it is not in the session ... doesn't like that.

Just removing this line

e1.setId(115); 

should fix the problem.

The persist function is used for update in this case, here your entity doen't exist on your db so the exception is telling you that you are trying to update an object that doen't have a reference on your db (not linked to an existing row on your db). Try to delete the id assignement and if ot working use session.save(e1); so the ID will be assigned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM