Enterprise Java Development@TOPIC@

Chapter 1. JPA Overview

1.1. Background
1.2. EntityManager
1.3. Entity
1.4. JPA Example
1.5. Entity States
1.5.1. Managed
1.5.2. Detached
1.6. Persistence Context
1.7. Persistence Unit
1.8. Example Layout
1.9. Persistence.xml
1.9.1. Application Example
1.9.2. Server Example
1.9.3. Optional hibernate.properties
1.9.4. Sample orm.xml
1.9.5. persistrence.xml Elements
1.9.6. Entity Discovery
1.10. Basic Steps
1.11. Entity Manager Methods
1.11.1. Basic CRUD Operations
1.11.2. Membership Operations
1.11.3. State Synchronization Operations
1.11.4. Locking Operations
1.11.5. Query Operations
1.11.6. Other Operations


@javax.persistence.Entity
public class Author implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue
    private long id;
    private long version=0;
    private String firstName;
    private String lastName;
    private String subject;
    private Date publishDate;
    
    public Author() {
    }
    ...
}        
        

Entity minimum requirements:



Author author = new Author();
author.setFirstName("dr");
author.setLastName("seuss");
author.setSubject("children");
author.setPublishDate(new Date());
log.debug("creating author:" + author);
assertFalse("unexpected initialized id", author.getId() > 0);
log.debug("em.contains(author)=" + em.contains(author));
em.persist(author);
log.debug("created author:" + author);        
assertTrue("missing id", author.getId() > 0);
log.debug("em.contains(author)=" + em.contains(author));
-creating author:ejava.examples.daoex.bo.Author@1d8e9e, id=0, 
    fn=dr, ln=seuss, subject=children, pdate=Mon Sep 17 00:22:25 EDT 2012, version=0
-em.contains(author)=false
-created author:ejava.examples.daoex.bo.Author@1d8e9e, id=50, 
    fn=dr, ln=seuss, subject=children, pdate=Mon Sep 17 00:22:25 EDT 2012, version=0
-em.contains(author)=true
|-- ejava
|   `-- examples
|       `-- daoex
|           |-- AuthorDAO.class
|           |-- bo
|           |   `-- Author.class
|           |-- DAOException.class
|           `-- jpa
|               `-- JPAAuthorDAO.class
`-- META-INF
    |-- orm.xml
    `-- persistence.xml


<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
version="2.0">

    <persistence-unit name="jpaDemo">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <mapping-file>META-INF/orm.xml</mapping-file>
        <properties>
            <!-- standard properties -->
            <property name="javax.persistence.jdbc.url" value="jdbc:h2:./target/h2db/ejava"/>
            <property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
            <property name="javax.persistence.jdbc.user" value="sa"/>
            <property name="javax.persistence.jdbc.password" value=""/>

            <!-- hibernate-specific properties -->
            <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <!-- set to 0 to improve error messages when needed
            <property name="hibernate.jdbc.batch_size" value="0"/>            
             -->
        </properties>
    </persistence-unit>
</persistence>

Above example defines properties for entity manager to establish physical connections to database

public interface javax.persistence.EntityManager{
...
}