Enterprise Java Development@TOPIC@
Plain Old Java Objects
Instantiated just like any other Java object
Bike bike = new Bike(1); bike.setMake("trek"); bike.setModel("2200"); bike.setSize(26);
Mapped to database schema
Interact with EntityManager to perform persistence operations
em.persist(bike); Bike bike2 = em.find(Bike.class, 1L); em.remove(bike);
Must
Have a non-private no-arg constructor
Be identified as an Entity (either using class annotations or ORM descriptors)
Have at least one (1) @Id property to be used as a primary key
Figure 46.1. Annotated Class Example
package ejava.examples.orm.core.annotated;
import javax.persistence.*; //brings in JPA Annotations
@Entity
public class Bike {
@Id //tells ORM that this property provides pk simple value
private long id;
private String make;
private String model;
private int size;
public Bike() {} //required non-private default ctor
public Bike(long id) { this.id = id; }
public long getId() {
return id;
}
public String getMake() { return make; }
public void setMake(String make) {
this.make = make;
}
...
}
Tells provider to include in persistence unit | |
Tells provider to use this property as the primary key | |
Default ctor required for provider to instantiate from DB |
Figure 46.2. Mapped Class Example
package ejava.examples.orm.core.mapped;
public class Bike {
private long id; //orm.xml file will map this field to Identity
private String make;
private String model;
private int size;
public Bike() {}
public Bike(long id) { this.id = id; }
public long getId() {
return id;
}
public String getMake() { return make; }
public void setMake(String make) {
this.make = make;
}
...
}
Must have a property to later define as a primary key | |
Must have a default ctor |
Figure 46.3. Mapped Class ORM.xml Example
<?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.orm.core.mapped.Bike"
access="FIELD"
metadata-complete="true"
name="MappedBike">
<table name="Bike"/>
<attributes>
<id name="id"/>
</attributes>
</entity>
</entity-mappings>
Identifies class as an entity | |
Identifies @Id property for a primary key |
Figure 46.4. Mapped Class META-INF/persistence.xml Reference
<?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="ormCore">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<mapping-file>orm/Bike-orm.xml</mapping-file>
<class>ejava.examples.orm.core.annotated.Bike</class>
...
</persistence-unit>
</persistence>
Reference to orm.xml to map Java class(es) to database and make part of persistence unit | |
Reference to class that may/may not be within local archive but should be part of persistence unit |
Figure 46.5. Example Directory Structure
src/ |-- main | |-- java | | `-- ejava ... | | |-- annotated | | | `-- Bike.java | | `-- mapped | | `-- Bike.java | `-- resources | |-- META-INF | | `-- persistence.xml | `-- orm | `-- Bike-orm.xml `-- test `-- resources `-- hibernate.properties
Annotated class | |
Pure-POJO class mapped by orm.xml | |
persistence.xml defines reference to orm.xml | |
orm.xml defines Java class mapping to database | |
Properties file with database runtime properties |
Defaults to unqualified name of entity class (e.g., Bike)
Used to determine database table name
Bike entity -> BIKE table
create table Bike ( id bigint not null, make varchar(255), model varchar(255), size integer not null, primary key (id) )
Used to refer to entity in JPA-QL
select b from Bike b
Can be assigned optional name property
@Entity(name="AnnotatedBike")
class Bike {
<entity class="ejava.examples.orm.core.mapped.Bike"
name="MappedBike">
Table name can be specified separate from entity name
@Entity(name="AnnotatedBike")
@Table(name="COREORM_BIKE")
class Bike {
<entity class="ejava.examples.orm.core.mapped.Bike"
name="MappedBike">
<table name="COREORM_BIKE"/>
FIELD
AccessDirect access to Java variable
PROPERTY
AccessAccessed through Java set/get methods
Placement of @Id class annotation determines default choice when using annotations
Figure 46.6. Defining FIELD Access using Annotations
@Entity
public class Bike {
@Id
private long id;
private String make;
Figure 46.7. Defining PROPERTY Access using Annotations
@Entity
public class Bike {
@Id
public long getId() { return id; }
protected void setId(int id) {
this.id=id;
}
public String getMake() { return make; }
public void setMake(String make) {
this.make = make;
}
Per-property access override thru @Access
@Entity
public class Bike {
@Id
private long id;
private String make;
...
@Access(AccessType.PROPERTY)
public String getMake() { return make; }
public void setMake(String make) {
this.make = make;
}
Can be defined globally
<access>FIELD</access>
Can be defined per-class
<entity class="ejava.examples.orm.core.mapped.Bike"
access="FIELD">
Can be defined per-property
<entity class="ejava.examples.orm.core.mapped.Bike"
access="FIELD">
<attributes>
<id name="id"/>
<basic name="make" access="PROPERY">
</basic>
</attributes>
</entity>
Appropriate for quick prototypes
Most vendors provide tools to create database schema from entity classes
Generated schema will be consistent with Java class model and not optimized for any particular use
Less use of javax.persistence.* annotations because Java defaults mostly sufficient
Add more use of javax.persistence.* annotations to reach desired schema model
Common in most legacy and non-trivial environments
Most vendors provide tools to create entity classes from database schema (in addition to DAOs)
Generated entity classes will be consistent with database table design and not have any bearing on the business needs of the data tier
More use of javax.persistence.* annotations to get desired business objects mapped to desired database schema