users@glassfish.java.net

Re: Simple JPA demo for Java SE

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Fri, 23 Feb 2007 14:43:00 -0800

It's interesting that this case works, because the spec requires (see 2.1
Requirements on the Entity Class) to be a top-level class.

You also need to add @GeneratedValue(strategy=IDENTITY) to let the JPA provider
know that the database is responsible for the PK values. Otherwise you won't be
able to create more than 1 instance of your entity.

Regards,
-marina

glassfish_at_javadesktop.org wrote:
> 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
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>