Enterprise Java Development@TOPIC@

Chapter 38. JPA Overview

38.1. Background
38.2. EntityManager
38.3. Entity
38.4. JPA Example
38.5. Entity States
38.5.1. Managed
38.5.2. Detached
38.6. Persistence Context
38.7. Persistence Unit
38.8. Example Layout
38.9. Persistence.xml
38.9.1. Application Example
38.9.2. Server Example
38.9.3. Optional hibernate.properties
38.9.4. Sample orm.xml
38.9.5. persistence.xml Elements
38.9.6. Entity Discovery
38.10. Basic Steps
38.11. Entity Manager Methods
38.11.1. Basic CRUD Operations
38.11.2. Membership Operations
38.11.3. State Synchronization Operations
38.11.4. Locking Operations
38.11.5. Query Operations
38.11.6. Other Operations


@javax.persistence.Entity
public class Author {
    @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



<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" 
version="2.0">
    
    <entity class="ejava.examples.daoex.bo.Author" 
            access="FIELD"
            metadata-complete="false"
            name="jpaAuthor">
        <table name="DAO_AUTHOR"/>
        <attributes>
            <id name="id">
                <generated-value strategy="SEQUENCE" 
                    generator="AUTHOR_SEQUENCE"/>                
            </id>
        </attributes>
    </entity>
</entity-mappings>
public interface javax.persistence.EntityManager{
...
}