Define Entity classes
Define Entity classes
JPA is all about entity-relationship mappings. We use the JPA annotations on POJO's to define your entity objects/classes.
The entity classes are defined in a separate module as part of a model architecture in the Logisticx demo as several use cases will use these classes.
We define several Entity classes - Order Entity, OrderStatus Entity and LineItem Entity.
Example 1.0 Order Entity class
package com.iona.fuse.demo.logisticx.model; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import javax.persistence.*; import javax.xml.bind.annotation.XmlRootElement; @Entity(name = "Ord") @XmlRootElement(name = "Order") public class Order { @Id @GeneratedValue @Column(name="ORDER_ID") private Long orderId; private Calendar orderDate; @OneToMany(cascade=CascadeType.ALL) private List<LineItem> lineItems = new ArrayList(); private long customerId; @OneToOne(cascade=CascadeType.ALL) private OrderStatus orderStatus; private String customerPoNumber; public Order() { } .... }
- The annotations are defined in the java.persistence package so you need to import that package.
- @Entity signifies that a particular class is an entity class. If the entity name is different from the table name, then the @Table annotation is used; otherwise, it isn't required.
- @Column provides the name of the column in a table if it is different from the attribute name. (By default, the two names are assumed to be the same.)
- @Id signifies the primary key. By default, all fields are of type @Basic, which are persisted as-is in the database.
- @GeneratedValue signifies a strategy to assign a unique value to your identity fields automatically. The type of strategies available are IDENTITY, SEQUENCE, TABLE, and AUTO. The default strategy is auto. Open JPA implements it through a table sequence.
Note there are a few things to keep in mind :
- JPA allows persistent classes to inherit from non-persistent classes, persistent classes to inherit from other persistent classes, and non-persistent classes to inherit from persistent classes.
- The entity class should have a default no-argument constructor.
- The entity class should not be final.
- Persistent classes cannot inherit from certain natively-implemented system classes such as java.net.socket and java.lang.Thread. If a persistent class inherits from a non-persistent class, the fields of the non-persistent super class cannot be persisted.
Example 1.1 OrderStatus Entity Class
package com.iona.fuse.demo.logisticx.model; import javax.persistence.OneToOne; import javax.persistence.*; import javax.persistence.Entity; @Entity public class OrderStatus { @Id @GeneratedValue private Long statusId; @OneToOne @JoinColumn(name="ORDER_ID") private Order order; private StatusCode statusCode; private String comments; public enum StatusCode { ORDER_PLACED, ORDER_FULFILLED, ORDER_SHIPPED, ORDER_DELIVERED, ORDER_POSTPONED }; public OrderStatus() { setStatusCode(StatusCode.ORDER_PLACED); } /** * A constructor to set a different <tt>statusCode</tt>. E.g.: * <p> * <code> * OrderStatus status = new OrderStatus(OrderStatus.ORDER_POSTPONED); * </code> * * @param statusCode */ public OrderStatus(StatusCode statusCode) { this.statusCode = statusCode; } .... }
In the above examples we must also address the relationship between the Order and OrderStatus entities. For each order, there will be an OrderStatus; and, similarly, each OrderStatus is associated with an order. These two tables are related with a one-to-one mapping, joined with the help of the foreign key ORDER_ID. JPA uses the @OneToOne annotation to define this relationship.
The @OneToOne and the @JoinColumn annotations are internally resolved by the persistence provider.
Every relationship has two sides and it is upto the owning side to propagate the update of the relationship to the database. Usually this is the side with the foreign key. The inverse side maps to the owning side. In the Logisticx demo the OrderStatus is the owning side. You must use the cascade option if you want any insert, update or delete operations on the Order entity to be propagated to the OrderStatus object
We also define a One To Many relationship between an order and lineItem. Each order can consist of different items and each lineItem will represent information for that specified item. We use the @OneToManyannotation to define this relationship.
Example 1.2 @OneToMany annotation
@OneToMany(cascade=CascadeType.ALL) private List<LineItem> lineItems = new ArrayList();
