1 Reply Latest reply on Mar 3, 2009 2:12 PM by olivier SAINT-EVE

    (debian,eclipse 3.4) "detached entity pased to persist"

    olivier SAINT-EVE Newbie

      hi,

      I use eclipse and jboss 4.2.2.
      I created a EAR project (enterprise application project) which contains 2 sub-projects : an EJB project which contains some EJB and a JPA entity, and a java project which contains the client "main" method.
      here is my problem : nothing at the writing of the code, but an error at the deployment :

      javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: source_jpa.customer.Customer
      at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
      at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:218)
      at org.jboss.ejb3.entity.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:182)
      at ejb.customer.CustomerBean.createCustomer(CustomerBean.java:45)


      the createCustomer method is the following :
      public Customer createCustomer(Customer customer, Address homeAddress) {
       if (customer==null)
       throw new ValidationException("customer object is null");
       customer.setHomeAddress(homeAddress);
       em.persist(customer);
      
       return customer;


      I checked the mysql database, and the "t_customer" table exists but it is empty.

      for a better understanding, here is the JPA class:
      package source_jpa.customer;
      
      import java.io.Serializable;
      import java.util.Calendar;
      import java.util.Date;
      import java.util.GregorianCalendar;
      
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.OneToOne;
      import javax.persistence.PostLoad;
      import javax.persistence.PostPersist;
      import javax.persistence.PostUpdate;
      import javax.persistence.PrePersist;
      import javax.persistence.PreUpdate;
      import javax.persistence.Table;
      import javax.persistence.Temporal;
      import javax.persistence.TemporalType;
      import javax.persistence.Transient;
      
      import source_jpa.Address;
      import source_jpa.exception.ValidationException;
      
      //{|}
      @Entity
      @Table(name="t_customer")
      public class Customer implements Serializable{
      
       @Id
       @GeneratedValue(strategy=GenerationType.AUTO)
       private Long id;
      
       @Column(unique=true,nullable=false,length=8)
       private String login;
      
       @Column(nullable=false,length=8)
       private String password;
      
       @Column(nullable=false,length=30)
       private String firstname;
      
       @Column(nullable=false,length=30)
       private String lastname;
      
       private String telephone;
       private String email;
      
       @Column(name="date_of_birth")
       @Temporal(TemporalType.DATE)
       private Date dateOfBirth;
      
       @Transient
       private Integer age;
      
       @OneToOne(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
       @JoinColumn(name="address_fk",nullable=true)
       private Address homeAddress;
      
       @PrePersist
       @PreUpdate
       private void validateData() throws ValidationException{
      
       if (firstname==null || firstname.equals(""))
       throw new ValidationException("Invalid first name");
       if (lastname==null || lastname.equals(""))
       throw new ValidationException("Invalid last name");
       if (login==null || login.equals(""))
       throw new ValidationException("Invalid login");
       if (password==null || password.equals(""))
       throw new ValidationException("Invalid password");
      
       }
      
       @PostLoad
       @PostPersist
       @PostUpdate
       public void calculateAge(){
      
       if (dateOfBirth==null){
       age=null;
       return;
       }
       Calendar birth=new GregorianCalendar();
       birth.setTime(dateOfBirth);
       Calendar now=new GregorianCalendar();
       now.setTime(new Date());
       int adjust=0;
       if (now.get(Calendar.DAY_OF_YEAR)-birth.get(Calendar.DAY_OF_YEAR)<0)
       adjust=-1;
       age=now.get(Calendar.YEAR)-birth.get(Calendar.YEAR)+adjust;
      
       }


      can you help me?

      olivier.



        • 1. Re: (debian,eclipse 3.4)
          olivier SAINT-EVE Newbie

          I forgot the client class :

          public static void main(String[] args) {
          
           try {
          
           Context ctx=new InitialContext();
          
           Object rem0=ctx.lookup("petstore_ear/CustomerBean/remote");
           ejb.customer.CustomerRemote rem =
           (ejb.customer.CustomerRemote) PortableRemoteObject.narrow(rem0,
           ejb.customer.CustomerRemote.class);
          
          
           rem.createCustomer(new Customer(1L,"jambon","rose","jean","bonrause","0018654118","jeanroi@gmail.com",new java.util.Date(1978,02,10),19,null),new source_jpa.Address(1,"4 rue des roses","","METZ","Moselle","57000","FRANCE"));
          
          
           System.out.println ("--- Hello, clients ---");
           for (source_jpa.customer.Customer client : rem.findCustomers()) {
           System.out.println ("client : "+client);
           }
          
           } catch (Exception e) {
           e.printStackTrace();
           }
          
           }