package bug3868; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.DiscriminatorColumn; import javax.persistence.DiscriminatorType; import javax.persistence.DiscriminatorValue; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Inheritance; import javax.persistence.InheritanceType; import javax.persistence.JoinColumn; import javax.persistence.JoinColumns; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.OneToOne; import javax.persistence.Persistence; import javax.persistence.Query; import javax.persistence.Table; public class Fails { private static final int LOOP = 20; private EntityManager eMgr; private static int excCount; public Fails() { Map map = new HashMap(); map.put("toplink.ddl-generation.output-mode", "database"); map.put("toplink.ddl-generation", "drop-and-create-tables"); map.put("toplink.logging.level", "finer"); map.put("eclipselink.ddl-generation.output-mode", "database"); map.put("eclipselink.ddl-generation", "drop-and-create-tables"); map.put("eclipselink.logging.level", "finer"); this.eMgr = Persistence.createEntityManagerFactory("tlem",map).createEntityManager(); } public static void main(String args[]) { Fails f = new Fails(); f.create(); for (int ix = 0; ix < LOOP; ix++) { f.execute(); } System.out.println("Total:" + LOOP + ";Failed:" + excCount); } private void create() { this.eMgr.getTransaction().begin(); BaseNode r1 = this.createResource(null, "/d/"); this.eMgr.getTransaction().commit(); } private void execute() { try { this.eMgr.getTransaction().begin(); Query q2 = this.eMgr.createNamedQuery("findRepositoryObjByPath"); q2.setParameter("path", "/d/"); List list = new ArrayList(13); BaseNode r2 = new Node(null, "/d/d/");//(BaseNode) q2.getSingleResult(); BaseNode orig =r2; //create lots for (int i = 0; i< 12; i++){ if (i%3 == 0){ r2 = new Leaf(null, r2.getPath() + "d"); }else{ r2 = new Node(null, r2.getPath()+"d/"); } list.add(r2); } //randomly persist BaseNode priv = null; while (!list.isEmpty()){ int index = (int)Math.round(Math.random()*(list.size()-1)); BaseNode node = (BaseNode)list.remove(index); eMgr.persist(node); node.setParent(priv); priv = node; } eMgr.persist(orig); Query q = this.eMgr.createNamedQuery("findRepositoryObjByPath"); q.setParameter("path", "/d/dd"); q.getResultList().clear(); this.eMgr.getTransaction().commit(); this.eMgr.getTransaction().begin(); eMgr.createQuery("Delete from BaseNode n where n.path <> '/d/'").executeUpdate(); this.eMgr.getTransaction().commit(); System.out.println("Ok"); } catch (RuntimeException e) { System.out.println("Failed"); excCount++; System.err.println("Failed. Reason:" + e.getMessage()); e.printStackTrace(System.err); throw e; } finally { if (this.eMgr.getTransaction().isActive()) { this.eMgr.getTransaction().rollback(); } } } private BaseNode createResource(Node p, String path) { BaseNode ro; if (path.endsWith("/")) { ro = new Node(p, path); } else { ro = new Leaf(p, path); } this.eMgr.persist(ro); return ro; } private void removeResource(String path) { Query q = this.eMgr.createNamedQuery("findRepositoryObjByPath"); q.setParameter("path", path); BaseNode r = (BaseNode) q.getSingleResult(); this.eMgr.remove(r); } }