Here is a barebones and complete JPA example with one java source file
Java - 1.5.0_05
Glassfish JPA - FCS release v1 ur1 b14 -
https://glassfish.dev.java.net/downloads/persistence/JavaPersistence.html
Database - MySql Ver 14.12 Distrib 5.0.16, for Win32
1. Create directories with following files
(root)
--- testjpa (dir)
------ TestJpa1.java
--- META-INF (dir)
------ persistence.xml
--- lib (dir)
------ mysql-connector-java-3.1.12-bin.jar (from mysql distribution)
------ toplink-essentials.jar (from glassfish JPA distribution)
2. Create "testbodega" mysql database owned by user "testbodega" with this "test_person" table -
create table TEST_PERSON (
testPersonId bigint not null auto_increment,
lastName varchar(255) not null,
firstName varchar(255) not null,
primary key (testPersonId)
) type=InnoDB;
3. Create META-INF/persistence.xml as follows -
<persistence xmlns="
http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="bodega">
<provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
<class>testjpa.TestJpa1$Person</class>
<properties>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/testbodega"/>
<property name="toplink.jdbc.user" value="testbodega"/>
<property name="toplink.jdbc.password" value=""/>
<!-- Provider-specific settings -->
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
4. Create this Java class with Person persistent object -
package testjpa;
import javax.persistence.*;
import java.util.*;
public class TestJpa1
{
static private java.io.PrintStream out = System.err;
public static void main(String args[])
throws Throwable
{
EntityManagerFactory emf = Persistence.createEntityManagerFactory("bodega");
EntityManager em = emf.createEntityManager();
// persist a Person object
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(new Person("Gandhi", "Mahatma"));
tx.commit();
// run a query by lastName
String queryString =
"SELECT p.lastName, p.firstName"
+ " FROM TestJpa1$Person p WHERE p.lastName = :lastName";
Query query = em.createQuery(queryString);
query.setParameter("lastName", "Gandhi");
List<Person> persons = query.getResultList();
em.close();
out.println("Java Persistence query '"+ queryString
+ "' returns count=" + persons.size());
}
@Entity
@Table (name="test_person")
static public class Person implements java.io.Serializable
{
@Id
private long testPersonId;
private String lastName;
private String firstName;
protected Person() {}
public Person(String lastName, String firstName)
{
this.lastName = lastName;
this.firstName = firstName;
}
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
}
}
5. In the (root) directory compile as follows -
javac -cp .;lib\toplink-essentials.jar testjpa\TestJpa1.java
6. In the (root) directory run test as follows -
java -cp .;lib\toplink-essentials.jar;lib\mysql-connector-java-3.1.12-bin.jar testjpa.TestJpa1
[Message sent by forum member 'jotobjects' (jotobjects)]
http://forums.java.net/jive/thread.jspa?messageID=205059