Monday, July 25, 2011

JPA2

69
Persistence annotations can be applied at three different levels: class, method, and field

The mapping annotations can be categorized as being in one of two categories: logical
annotations and physical annotations

ACCESSING ENTITY STATE
If we annotate fields, the provider will get and set the fields of the entity using reflection.

If the annotations are set on the getter methods of properties, those getter and setter methods will be invoked by the provider to access and set the state.

@Entity
public class Employee {
@Id private int id;

Mixed access
@Entity
@Access(AccessType.FIELD)
public class Employee {
public static final String LOCAL_AREA_CODE = "613";
@Id private int id;
@Transient private String phoneNum;

public int getId() { return id; }
public void setId(int id) { this.id = id; }

Mapping To A Table

Listing 4-4. Overriding the Default Table Name
@Entity
@Table(name="EMP")
public class Employee { ... }

Listing 4-5. Setting a Schema
@Entity
@Table(name="EMP", schema="HR")
public class Employee { ... }

Listing 4-6. Setting a Catalog
@Entity
@Table(name="EMP", catalog="HR")
public class Employee { ... }

Mapping Simple Types
If the type from the JDBC layer cannot be converted to the Java type of the
field or property, an exception will normally be thrown, although it is not guaranteed.

An optional @Basic annotation can be placed on a field or property to explicitly mark it as being persistent. This annotation is mostly for documentation purposes and is not required for the field or property to be persistent. Because of the annotation, we call mappings of simple types basic mappings. Now that we have seen how we can persist either fields or properties and how they are virtually equivalent in terms of persistence, we will just call them attributes. An attribute is a field or property of a class, and we will use the term attribute from now on to avoid having to continually refer to fields or properties in specific terms.

Column Mappings

Listing 4-7. Mapping Attributes to Columns
@Entity
public class Employee {
@Id
@Column(name="EMP_ID")
private int id;
private String name;
@Column(name="SAL")
private long salary;
@Column(name="COMM")
private String comments;
// ...
}

Lazy Fetching

Listing 4-8. Lazy Field Loading
@Entity
public class Employee {
// ...
@Basic(fetch=FetchType.LAZY)
@Column(name="COMM")
private String comments;
// ...
}

Second, on the surface it might appear that this is a good idea for certain attributes of an entity, but in practice it is almost never a good idea to lazily fetch simple types.

Large Objects

Listing 4-9. Mapping a BLOB Column
@Entity
public class Employee {
@Id
private int id;
@Basic(fetch=FetchType.LAZY)
@Lob @Column(name="PIC")
private byte[] picture;
// ...
}

Enumerated Types

@Entity
public class Employee {
@Id private int id;
private EmployeeType type;
// ...
}

Listing 4-11. Mapping an Enumerated Type Using Strings
@Entity
public class Employee {
@Id
private int id;
@Enumerated(EnumType.STRING)
private EmployeeType type;
// ...
}

Temporal Types

Listing 4-12. Mapping Temporal Types
@Entity
public class Employee {
@Id
private int id;
@Temporal(TemporalType.DATE)
private Calendar dob;
@Temporal(TemporalType.DATE)
@Column(name="S_DATE")
private Date startDate;
// ...
}

Transient State
Attributes that are part of a persistent entity but not intended to be persistent can either be modified with the transient modifier in Java or be annotated with the @Transient annotation.

81

Monday, July 4, 2011

JPA2 - 04 iulie

Dependency lookup


A reference is declared using one of the resource reference annotations: @Resource, @EJB, @PersistenceContext, or @PersistenceUnit.

@Stateless
public class EmployeeServiceBean implements EmployeeService {
@PersistenceContext(unitName="EmployeeService")
EntityManager em;
// ...
}

@Stateful
public class EmployeeServiceBean implements EmployeeService {
@PersistenceUnit(unitName="EmployeeService")
private EntityManagerFactory emf;
private EntityManager em;
@PostConstruct
public void init() {
em = emf.createEntityManager();
}
// ...
}

@Stateless
public class DeptServiceBean implements DeptService {
@EJB(beanName="AuditServiceBean")
AuditService audit;
// ...
}

Transactions
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class ProjectServiceBean implements ProjectService {
// methods in this class manually control transaction demarcation
}

@Stateful
public class ShoppingCartBean implements ShoppingCart {
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public void addItem(String item, Integer quantity) {
verifyItem(item, quantity);
// ...
}
// ...
}

CMT or BMT