-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: detached entity passed to persist
PostPosted: Wed Aug 24, 2011 1:06 am 
Newbie

Joined: Thu Dec 02, 2010 7:59 am
Posts: 2
package com.cnb.autodb;

package com.cnb.autodb;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

/**
* @author erpula
*
*/
@Entity
@Table(name = "EMPLOYEE")
public class Employee implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private int id;

@Column(name = "emp_name")
private String emp_name;

@Column(name = "is_manager")
private boolean is_manager;

@Column(name = "emp_sex")
private String emp_sex;
/**
* @return the id
*/

public int getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(int id) {
this.id = id;
}
/**
* @return the emp_name
*/
public String getEmp_name() {
return emp_name;
}
/**
* @param empName the emp_name to set
*/
public void setEmp_name(String empName) {
emp_name = empName;
}
/**
* @return the is_manager
*/
public boolean isIs_manager() {
return is_manager;
}
/**
* @param isManager the is_manager to set
*/
public void setIs_manager(boolean isManager) {
is_manager = isManager;
}
/**
* @return the emp_sex
*/
public String getEmp_sex() {
return emp_sex;
}
/**
* @param empSex the emp_sex to set
*/
public void setEmp_sex(String empSex) {
emp_sex = empSex;
}

}


persistence.xml

<persistence-unit name="Employee">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.cnb.autodb.Employee</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sravan" />
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="mysql-cnb" />
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>



Main Class


public static void main(String[] args) {
// TODO Auto-generated method stub
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("Employee");
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
Employee employee = new Employee();
employee.setId(1);
employee.setEmp_name("PRASHANTH");
employee.setEmp_sex("M");
employee.setIs_manager(false);
entityManager.persist(employee);
}


returning an error code

[color=#FF0000]log4j:WARN No appenders could be found for logger (org.hibernate.type.BasicTypeRegistry).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.cnb.autodb.Employee
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:226)
at com.cnb.autodb.Test.main(Test.java:22)
Caused by: org.hibernate.PersistentObjectException: detached entity passed to persist: com.cnb.autodb.Employee
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:220)
... 1 more









Could Some One Help Me out :(


Top
 Profile  
 
 Post subject: Re: detached entity passed to persist
PostPosted: Wed Aug 24, 2011 2:07 am 
Newbie

Joined: Sun Aug 21, 2011 5:24 pm
Posts: 7
In your entity, the "id" attribute is configured as a generated value. Therefore, Hibernate expects the "id" of the object to be null when you call EntityManager#persist. Commenting the line in which you set the "id" should solve the problem.


Top
 Profile  
 
 Post subject: Re: detached entity passed to persist
PostPosted: Thu Aug 25, 2011 12:07 am 
Newbie

Joined: Thu Dec 02, 2010 7:59 am
Posts: 2
Thanx my problem is solved i hav already done the same .... :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.