31.07.2013 Views

MySQL Cluster Tutorial - cdn.oreillystatic.com

MySQL Cluster Tutorial - cdn.oreillystatic.com

MySQL Cluster Tutorial - cdn.oreillystatic.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Main.java (part 1):<br />

import java.util.List;<br />

import javax.persistence.EntityManager;<br />

import javax.persistence.EntityManagerFactory;<br />

import javax.persistence.EntityTransaction;<br />

import javax.persistence.Persistence;<br />

import javax.persistence.Query;<br />

import java.io.*;<br />

public class Main {<br />

public static void main (String[] args) throws java.io.IOException {<br />

EntityManagerFactory entityManagerFactory =<br />

Persistence.createEntityManagerFactory("clusterdb");<br />

EntityManager em = entityManagerFactory.createEntityManager();<br />

EntityTransaction userTransaction = em.getTransaction();<br />

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));<br />

System.out.println("The tables will now have been created - check through<br />

SQL.");<br />

System.out.println("mysql> use clusterdb;");<br />

System.out.println("mysql> show tables;");<br />

System.out.println("Hit return when you are done");<br />

String ignore = br.readLine();<br />

As part of creating the EntityManagerFactory and EntityManager, OpenJPA creates the<br />

tables for the two classes specified for the ‘clusterdb’ persistence unit. While the<br />

application waits for the user to press return, this can be checked:<br />

mysql> use clusterdb<br />

mysql> show tables;<br />

+---------------------+<br />

| Tables_in_clusterdb |<br />

+---------------------+<br />

| department |<br />

| employee |<br />

+---------------------+<br />

After hitting return, the application can create an Employee object and then persist it – at<br />

which point it will be stored in the ‘employee’ table. A second Employee object is then<br />

created and populated with the data read back from the database (using a primary key look<br />

up on the Id property with a value of 1):<br />

Copyright © 2010, Oracle and/or its affiliates. All rights reserved. 64/81

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!