users@glassfish.java.net

Inserting records into MySql using TopLink essentials ORM

From: <glassfish_at_javadesktop.org>
Date: Sat, 02 Feb 2008 09:18:03 PST

I am trying to persist two records into MySql database using standalone TopLink essentials (not running on any container or app server), the ID field is an Auto increment,
using the following method:

public void testAddPropertyTypes() {
                PropertyTypeServiceImpl ptsi = new PropertyTypeServiceImpl();
                
                List<PropertyType> list = new ArrayList<PropertyType>();
                
                PropertyType pt1 = new PropertyType();
                pt1.setTypeDesc("Test Type Desc 1");
                list.add(pt1);
                
                PropertyType pt2 = new PropertyType();
                pt2.setTypeDesc("Test Type Desc 2");
                list.add(pt2);
                
                ptsi.add(list);
}

Which calls this method:

public void add(Collection<PropertyType> propertyTypes) {
                EntityManager em = emf.createEntityManager();


                try {
                        em.getTransaction().begin();

                        for(PropertyType pt : propertyTypes) {
                                em.persist(pt);
                        }

                        em.getTransaction().commit();
                } catch (RuntimeException e) {
                        try {
                                e.printStackTrace();
                                em.getTransaction().rollback();
                        } catch (RuntimeException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                        }
                }
                finally {
                        em.close();
                }

}

When I attempt to do so, I get the following exception, which happens when persisting the second entity:

javax.persistence.EntityExistsException:
Exception Description: Cannot persist detached object [com.israyon.re.model.PropertyType_at_131303f]. <-- this is the ref to the second object
Class> com.israyon.re.model.PropertyType Primary Key> [0]
        at oracle.toplink.essentials.internal.ejb.cmp3.base.EntityManagerImpl.persist(EntityManagerImpl.java:224)
....

If I insert only one record using the same method everything works fine.

the entity looks as follows:

@Entity
@NamedQuery(name="findAll", query="SELECT t FROM PropertyType t")
@Table(name="property_type")
public class PropertyType implements Serializable {
        
        @Id
        @Column(name="TYPE_ID")
        private int typeId;

        @Column(name="DESCRIPTION")
        private String typeDesc;
        
        public PropertyType() {
                super();
                // TODO Auto-generated constructor stub
        }

        public int getTypeId() {
                return typeId;
        }
                
        public String getTypeDesc() {
                return typeDesc;
        }
        
        public void setTypeDesc(String typeDesc) {
                this.typeDesc = typeDesc;
        }
}


Anyone knows what am I doing wrong?

Thank you,

Jon
[Message sent by forum member 'israyon' (israyon)]

http://forums.java.net/jive/thread.jspa?messageID=257136