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.

A persistence unit called ‘clusterdb’ is created; the provider (implementation for the<br />

persistence) is set to openjpa (as opposed for example to hibernate). Two classes are<br />

specified – ‘Employee’ and ‘Department’ which relate to the persistent classes that the<br />

application will define. Connector/J is defined as the JDBC connection (together with the<br />

host and the port of the <strong>MySQL</strong> Server to be used). The key to having OpenJPA use<br />

<strong>Cluster</strong>JPA is to set the BrokerFactory to ndb and specify the connect string (host:port) for<br />

the <strong>MySQL</strong> <strong>Cluster</strong> management node. The database is defined to be ‘clusterdb’ for both<br />

the JDBC and <strong>Cluster</strong>J connections.<br />

If not already done so, create the ‘clusterdb’ database (if it already contains tables from<br />

the <strong>Cluster</strong>J tutorial then drop them):<br />

mysql> create database clusterdb;<br />

The next step is to create the persistent class definitions for the Department and Employee<br />

Entities:<br />

Department.java:<br />

import javax.persistence.*;<br />

@Entity(name = "department")<br />

public class Department {<br />

private int Id;<br />

private String Site;<br />

public Department(){}<br />

@Id public int getId() {return Id;}<br />

public void setId(int id) {Id=id;}<br />

@Column(name="location")<br />

public String getSite() {return Site;}<br />

public void setSite(String site) {Site=site;}<br />

public String toString() {<br />

return "Department: " + getId() + " based in " + getSite();<br />

}<br />

}<br />

Using the @Entity tag, the table name is specified to be ‘department’. Note that unlike<br />

<strong>Cluster</strong>J, <strong>Cluster</strong>JPA uses persistent classes (rather than interfaces) and so it is necessary<br />

to define the properties as well as the getter/setter methods. The primary key is defined<br />

using the @Id tag and we specify that the column associated with the Site property should<br />

be called ‘location’ using the @Column tag.<br />

As this is a class, it is possible to add other useful methods – in this case toString().<br />

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

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

Saved successfully!

Ooh no, something went wrong!