Skip to main content

Glassfish Persistance API

Posted by tchangu on May 29, 2006 at 10:43 AM PDT

Lately I have been tinkering with Glassfish and happened to look at the persistence API. The persistence API is based on the EJB 3.0 specification and is made available as a standalone component. I must admit that it’s fairly straight forward and a pleasure to use.



After downloading the persistence API from Glassfish Persistence. I started with the examples provided along with the documentation. After playing around with this technology a little bit, I thought I will blog some of my initial learnings. The persistent objects are essentially plain old java objects decorated with Java 5.0 annotations.



The source for a simplistic customer pojo and an order pojo is as follows



Customer
Customer.jpg



Order
Order.jpg



Relationship

The ER model for these objects is as follows

ER.jpg



The customer POJO is mapped to CUSTOMER table

@Entity
@Table(name="CUSTOMER")
public class Customer implements Serializable {

The order POJO is mapped to CUST_ORDER table
@Entity
@Table(name="CUST_ORDER")
public class Order implements Serializable {



The instance variables are mapped to appropriate column name using the @Column annotation type.



The relationship between customer and order mirror the ER relationship. In this particular case we have a bi-directional relationship and since this is bi-directional we have an owning side (order) as well as an inverse side (customer having a collection of orders)



The inverse side of the relationship (i.e. customer) refers to the owning side of the relationship (i.e. order) through the means of mappedBy element of the @OneToMany annotation type. This mappedBy refers the name of the property in the owning side and in this case it is customer


@OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
public Collection getOrders() {
   return orders;
}

The owning side of the relationship (i.e. order) refers to customers using the @ManyToOne annotation type. Also here the DB join column is specified using the name element of @JoinColumn annotation type. In this particular case, the join column is CUSTOMER_CUST_ID


@ManyToOne()
@JoinColumn(name="CUSTOMER_CUST_ID")
public Customer getCustomer() {
   return customer;
}



Client Code


The client code is fairly straight forward
EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Customer customer0 = new Customer();
customer0.setId(10);
customer0.setName("Joe Smith");
em.persist(customer0);

Order order1 = new Order();
order1.setId(100);
order1.setAddress(Main St. Anytown, USA");
order1.setCustomer(customer0);

Order order2 = new Order();
order2.setId(200+i);
order2.setAddress(" 1st St. Random City, USA");
order2.setCustomer(customer0);

customer0.getOrders().add(order1);
customer0.getOrders().add(order2);

em.getTransaction().commit();
em.close();

Couple of interesting things happen here. Since we are doing this for Java SE, the application is responsible for creating and destroying persistence context. In this case the entity manager is created using the createEntityManager method of EntityManagerFactory class. Appropriate persistent unit is provided as a factory method parameter. In this particular case, we are creating the persistence context for the test persistence unit.



The persist() method is called on the entity manager using customer as method argument. Since Customer is a new entity it now becomes a managed entity. This means customer might get persisted to database at or during a transactional commit operation. Since customer maintains a collection of orders and its relationship to the entity Order, is annotated as CascadeType.PERSIST, the framework automatically applies the persist operation to Order entites as well when the transaction is committed.



Running

The persistence.xml need to be present under META-INF directory in the classpath. The persistence XML for this example is as follows. It seems like the persistence framework is shipped with TOPLINK as persistence provider (curious)


<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="test">
        <provider>oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider</provider>
        <class>com.test.model.Customer</class>
        <class>com.test.model.Order</class>
        <properties>
            <property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="toplink.jdbc.url" value="jdbc:mysql://localhost/persistence"/>
            <property name="toplink.jdbc.user" value="vk"/>
            <property name="toplink.jdbc.password" value="password"/>
            <property name="toplink.logging.level" value="INFO"/>
        </properties>
    </persistence-unit>
</persistence>

Enjoy!

Related Topics >>