/* javac -classpath /usr/local/mysql/share/mysql/java/*.jar:/usr/local/openjpa/openjpa-1.2.1.jar:/usr/local/openjpa/lib/geronimo-jpa_3.0_spec-1.0.jar:. Main.java Employee.java Department.java java -Djava.library.path=/usr/local/mysql/lib -classpath /usr/local/mysql/share/mysql/java/*:/usr/local/mysql/share/java/*:/usr/local/openjpa/openjpa-1.2.1.jar:/usr/local/openjpa/lib/*:/usr/local/connectorj/mysql-connector-java-5.1.12-bin.jar:. Main mysql> create database clusterdb; mysql> alter table clusterdb.employee engine=ndb; mysql> alter table clusterdb.department engine=ndb; mysql> describe clusterdb.employee; +--------------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | municipality | varchar(255) | YES | | NULL | | | department | int(11) | YES | | NULL | | | ended | varchar(255) | YES | | NULL | | | first | varchar(255) | YES | | NULL | | | last | varchar(255) | YES | | NULL | | | started | varchar(255) | YES | | NULL | | | dept | int(11) | YES | | NULL | | +--------------+--------------+------+-----+---------+-------+ mysql> describe department; +----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | location | varchar(255) | YES | | NULL | | +----------+--------------+------+-----+---------+-------+ */ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.EntityTransaction; import javax.persistence.Persistence; import javax.persistence.Query; import java.io.*; public class Main { public static void main (String[] args) throws java.io.IOException { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("clusterdb"); EntityManager em = entityManagerFactory.createEntityManager(); EntityTransaction userTransaction = em.getTransaction(); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Check engine used for clusterdb.employee & change to ndb if needed."); System.out.println("mysql> alter table clusterdb.employee engine=ndb;"); System.out.println("mysql> alter table clusterdb.department engine=ndb;"); System.out.println("Hit return when you are done"); String ignore = br.readLine(); userTransaction.begin(); Employee emp = new Employee(); emp.setId(1); emp.setDepartment(666); emp.setFirst("Billy"); emp.setLast("Fish"); emp.setStarted("1st February 2009"); em.persist(emp); userTransaction.commit(); userTransaction.begin(); Employee theEmployee = em.find(Employee.class, 1); userTransaction.commit(); System.out.println(theEmployee.toString()); System.out.println("Chance to check the database before City is set"); System.out.println("Hit return when you are done"); ignore = br.readLine(); userTransaction.begin(); theEmployee.setCity("London"); theEmployee.setDepartment(777); userTransaction.commit(); System.out.println("Chance to check the City is set in the database"); System.out.println("Hit return when you are done"); ignore = br.readLine(); Department dept; userTransaction.begin(); for (int i=700;i<800;i++) { emp = new Employee(); dept = new Department(); emp.setId(i+1000); emp.setDepartment(i); emp.setFirst("Billy"); emp.setLast("No-Mates-"+i); emp.setStarted("1st February 2009"); em.persist(emp); dept.setId(i); dept.setSite("Building-"+i); em.persist(dept); } userTransaction.commit(); userTransaction.begin(); Query q = em.createQuery("select x from Employee x where x.department=777"); Query qd; for (Employee m : (List) q.getResultList()) { System.out.println(m.toString()); qd = em.createQuery("select x from Department x where x.id=777"); for (Department d : (List) qd.getResultList()) { System.out.println(d.toString()); } } userTransaction.commit(); System.out.println("Last chance to check the database before all entries are deleted."); System.out.println("Hit return when you are done"); ignore = br.readLine(); userTransaction.begin(); Query delEmp = em.createQuery("DELETE FROM Employee e"); int deleted = delEmp.executeUpdate(); System.out.println(deleted + " Employees deleted."); userTransaction.commit(); /* userTransaction.begin(); Query delDept = em.createQuery("DELETE FROM Department d"); deleted = delDept.executeUpdate(); System.out.println(deleted + " Departments deleted."); userTransaction.commit(); */ em.close(); entityManagerFactory.close(); } }