package utc; import java.util.ArrayList; import java.util.List; import javax.persistence.Entity; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.OneToMany; import javax.persistence.Persistence; import javax.persistence.TypedQuery; public final class UTCMerge { /** * @param args */ public static void main(String[] args) { EntityManagerFactory emf = Persistence.createEntityManagerFactory("objectdb:$objectdb/db/test.tmp;drop"); EntityManager em = emf.createEntityManager(); for (int pidx = 0; pidx < 100; pidx++) { em.getTransaction().begin(); Parent p = new Parent("Parent " + pidx); em.persist(p); for (int chidx = 0; chidx < (pidx * 100); chidx++) { Child ch = new Child("Child " + chidx); ch.setP(p); em.persist(ch); } em.getTransaction().commit(); } em.close(); // checking it they exists em = emf.createEntityManager(); TypedQuery query = em.createQuery("select p from Parent p", Parent.class); System.out.println("Returned records: " + query.getResultList().size()); em.close(); // now the test em = emf.createEntityManager(); TypedQuery q2 = em.createQuery("select p.id from Parent p", Long.class); for (Long id : q2.getResultList()) { long startTime = System.currentTimeMillis(); em.getTransaction().begin(); Parent p = em.find(Parent.class, id); p.setName("Some changed name" + id); em.merge(p); em.getTransaction().commit(); long stopTime = System.currentTimeMillis(); System.out.println("Transaction for id: " + id + " took: " + (stopTime - startTime) + "ms"); em.clear(); } em.close(); emf.close(); } @Entity public static class Parent { @GeneratedValue @Id private long id; private String name; @OneToMany(targetEntity = Child.class, fetch = FetchType.LAZY, mappedBy = "p") private List childs = new ArrayList(); public Parent(String name) { super(); this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @Entity public static class Child { @GeneratedValue @Id private long id; private String name; @ManyToOne private Parent p; public Child(String name) { super(); this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Parent getP() { return p; } public void setP(Parent p) { this.p = p; } } }