Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

toplink/jpa problem

  Asked By: Trupti    Date: Dec 24    Category: Java    Views: 1186
  

I'm trying to persist one object, for example (User) .After persisting the first object or inserting the first row without problem and while trying to insert a new row for second times or persist new object i encountered to this exception from my web application.

javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [persistobject.User@14fd55e].
Class> persistobject.User Primary Key> [0]

Database = mysql / web application / glassfish

i've tried many ways to solve this problem but !?

if you think you need to see my codes tell me, i'll be sending.

Somebody tells me what the exception's saying? and what should i do.

Share: 

 

9 Answers Found

 
Answer #1    Answered By: Jake Williams     Answered On: Dec 24

please post the code you persist through it and your entity's properties

and also the full stack trace of the exception  you've got

 
Answer #2    Answered By: Muriel Dunn     Answered On: Dec 24

I am not expert in JPA field,and maybe this is not related to your problem,but i do not think so.
Because this about Hibernate(JPA) ,another implementation of JPA and i think it maybe solves your problem.
Please refer to javanotepad.blogspot.com/.../...-in-hibernate.html
Please let me know whether this helps you or not.

 
Answer #3    Answered By: Trae Thompson     Answered On: Dec 24

it is possibly because you have set the primary  key of the object,
while in your definition you have defined as auto generated.

 
Answer #4    Answered By: Rochelle Elliott     Answered On: Dec 24

but i know that my database  (MySQL) supports Auto Increment so i didnt use @GeneratedValue
as you see the code my persistence object  is very simple but i dont y does it work just for first time :D

my persistence  file

@Entity
@Table(name="user_table",schema="myschema")
public class user  {

@Id
@Column(name="ID",nullable=false)
private int id;

@Column(name="USERNAME",nullable=false)
private String username;

@Column(name="PASSWORD",nullable=false)
private String password;

@Column(name="EMAIL",nullable=true)
private String email;

public User() { }

public User(int id) { this.id = id; }

public int getId() { return this.id; }

public void setId(int id) { this.id = id; }

public String getUsername() { return this.username; }

public void setUsername(String username) { this.username = username; }

public String getPassword() { return this.password; }

public void setPassword(String password) { this.password = password; }

public String getEmail() { return this.email; }

public void setEmail(String email) { this.email = email; }

my stateless EJB

try {
//this.emf = null;
this.emf = Persistence.createEntityManagerFactory("test");
this.manager = emf.createEntityManager();
this.manager.getTransaction().begin();
this.manager.persist(user);
this.manager.getTransaction().commit();
} catch (EntityExistsException ex1) {
System.out.println(ex1 + " AZ TARAFE INITIAL SERVICE BEAN");
}
finally {
try {
this.manager.close();
} catch (Exception ee) {
System.out.println(ee );
return false;
}

my persistence.xml file
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>ref</jta-data-source>
<class>persistobject.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties/>
</persistence-unit>

</persistence>

 
Answer #5    Answered By: Silvia Chapman     Answered On: Dec 24

first before closing EM try to flush it by calling this.manager.flush()
second, where do you create your entity User? in other words, if you
create the user  entity once, this is considered a already attached
entity.
I think you should create User each time you are going to persist

em.beginTransaction()
User user = new User() //this line is between begin transaction and commit
user.setId(10L);
...
em.persist(user);

em.commit();

if you're not sure whether this user is a new user or an already
persisted user then I recommend you to use user = em.merge(user);
instead, this is usually happens when you pass your entity through a
method argument.

 
Answer #6    Answered By: Ty Thompson     Answered On: Dec 24

thanks for everything. i could solve  my problem  by myself. but it's so funny i just added this annotation @GeneratedValue(strategy=GenerationType.SEQUENCE)
and my problem is solved.
i red somewhere if your database  supports Auto Increment it's not necessary to add this annotation @GeneratedValue.
but It seems i had to add this annotation.
anyways thanks again and again

 
Answer #7    Answered By: Grady Stewart     Answered On: Dec 24

It was not funny! a small change may end up in a big problem
regardless of that, the code you were posted was different and there
was no @GeneratedValue !

 
Answer #8    Answered By: Brendan Smith     Answered On: Dec 24

congratulations for solving your problem!
the spec says if you have a auto generated id you should set the id field value manually, and if you have the auto generated id you should not set it your self.

 
Answer #9    Answered By: Faiza Mian     Answered On: Dec 24

Look at your database  table description  (in MySQL use command "desc TABLE_NAME") and see if its "id" column is "auto_increment"?
If not, modify (alter) your table and make id column auto_increment.

Then in your entity add this annotation to id property:
@GeneratedValue(strategy=GenerationType.AUTO)

 
Didn't find what you were looking for? Find more on toplink/jpa problem Or get search suggestion and latest updates.




Tagged: