• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Detached Entity Exception in JPA (Detached entity passed to persist)

 
Terseer Shaguy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,

Please I have being bugged down by the detached entity exception I have tried lots of things all to no avail
below is an extract from my entity class .

package com.taysay.drivetru.entity;

import static org.jboss.seam.ScopeType.SESSION;

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;
import javax.persistence.TableGenerator;

import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;



@Entity
@Name("demo")
@Scope(SESSION)
@Table(name="DEMO")
public class Demo implements Serializable {


private int id;
private String name;


@TableGenerator(name="demo_Gen",
table="ID_GEN",
pkColumnName="GEN_NAME",
valueColumnName="GEN_VAL",
pkColumnValue="demoid_Gen",
initialValue=10000,
allocationSize=100)
@Id @GeneratedValue(strategy=GenerationType.TABLE,generator="demo_Gen")
@Column(name="DEMO_ID")
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


}


// Action class

package com.taysay.drivetru.actions;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;

import com.taysay.drivetru.entity.Demo;


//@Stateless
@Stateful
@Name("demoManager")
public class DemoActionBean implements DemoManager{

// @PersistenceContext
@PersistenceContext (type=PersistenceContextType.EXTENDED)
private EntityManager em;

@In (required=false) @Out (required=false)
private Demo demo;

public void save()
{
System.out.println(": THE SAVE DEMO :"+demo.getId());


em.persist(demo);


}


@Destroy @Remove
public void destroy() { }


}

// Interface

package com.taysay.drivetru.actions;

public interface DemoManager {

public void save();

public void destroy();

}


now I came don to this simple class when I had issues in some other class so wrote a simple class with the aim of
having a more manageable situation.

initially I had my EntityManager em; annotated with the // @PersistenceContext

but when the exception was thrown i did this (the EXTENDED clause)
@PersistenceContext (type=PersistenceContextType.EXTENDED)

Yet this ugly exception wont let me be , I am working from eclipse ganymede Version: 3.4.2


Please can some one tell me what exactly i am doing wrong, what happen is that I am generating my Id from a table
now at the first instance of application start off I get to make a successfull insertion to the back end (MYSQL 5) but subsequent attempts lead to this ugly guy throwing exceptions at me. prior to a successful insertion the ID is 0 but subsequently it carries the values of the just persisted id .

Please I urgently need help, Asap


Thanks in earnest for your invaluable help. Thank you Have a wonderful day.


Have a bug free week .

Taysay
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read this regarding the use of urgent in online forums like JavaRanch.

There's alot of code there, and I'm not really sure where the problem is. Could you perhaps simplify it a bit, and focus on exactly where the problem is, and what the exception is, and perhaps we can narrow it down and help you out.

-Cameron McKenzie
 
Terseer Shaguy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


@Cameron Wallace McKenzie and All the House

So sorry I misled you guys, in my bid to give a clear picture of my challenge.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We're here to help, and we want to help. That's what the boards all about.

If you can really help us focus on what the problem is, we'll all pitch in! That's what we love to do around here.

Step through your code and let us know which line is causing all the problems. Maybe that'll help us all focus.

Kindest regards.

-Cameron McKenzie
 
Terseer Shaguy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,


I am having a challenge with the detached entity passed to persist thrown when ever I
call the persist() with my id greater than 0, please see code extract below.


public void save()
{
System.out.println(": THE SAVE DEMO :"+demo.getId());


em.persist(demo); // Here


}

sometimes when I do this

public void save()
{
System.out.println(": THE SAVE DEMO :"+demo.getId());

if(1 < demo.getId() )
{

em.persist(demo);

}
else
{
em.merge(demo);

}


}


Thank you for your time.

have a bug free day.

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can try to use flush() instead of persist/merge OR either use refresh() or attach the entity again.
 
Terseer Shaguy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your responce, just that earlier on I tried using

flush() which threw the detached entity exception while
refresh() threw an exception relating to unmanaged entity.

thank you
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this, to "refresh" or attach an object:



normally, I you could use merge instead of persist - merge would also attach the entity again.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you want to save the entity? What about just calling the same method before calling flush or refresh. That will reattach it to the session.
 
Terseer Shaguy
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry this approach of refreshing the entity before carrying out another operation on
the entity manager object gives me " Entity not managed" and calling the same method before calling the refresh method goes back to the detached entity exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic