Personal notes on Java
For other technologies like HTML, CSS, .NET, PHP, etc. check my other blog

Different ways to persist state within Java:

  • Through the mechanism of serialization, which is the process of converting an object into a sequence of bits.
    By implementing the java.io.Serializable interface, objects can be serialized on a disk, or over a network connection (including Internet), in an independent  format that can be reused across different operating systems
    Drop backs: It has neither a query language nor an infrastructure supporting heavy concurrent access or clustering.
  • Through JDBC (Java Database Connectivity), which is the standard API to access relational databases.
    It can connect to a database, execute SQL statements, and get back a result. Although still widely used, it has tended to be eclipsed by the more powerful object-relational mapping (ORM) tools.
  • Through ORM tools/frameworks: ORM (Object-Relational Mapping) involves delegating access to relational databases to external tools or frameworks, which in turn give an object-oriented view of relational data, and vice versa.
    Mapping tools have a bidirectional correspondence between the database and objects. Several frameworks achieve this, such as:
    • Hibernate - Parallel to the J2EE world, this was a popular open source solution that led to some surprising changes in the direction of persistence: Hibernate, which brought back a lightweight, object oriented persistent model.
    • TopLink;
    • Java Data Objects (JDO) - failed to gain any significant market penetration;
    • Java Persistence API (JPA) - is the preferred technology, as it has been included in Java EE 6.


GENERAL CONCEPTS


Entities: When talking about mapping objects to a relational database, persisting objects, or querying objects, the term “entity” should be used rather than “objects.” Objects are instances that just live in memory. Entities are objects that live shortly in memory and persistently in a database.

ORM: The principle of ORM is to delegate to external tools or frameworks (like JPA or Hibernate) the task of creating a correspondence between objects and tables. JPA maps object to a database through metadata either through:
  • Annotations: The code of the entity is directly annotated with all sorts of annotations that are described in the javax.persistence package.
  • XML descriptors: Instead of (or in addition to) annotations, you can use XML descriptors. The mapping is defined in an external XML file that will be deployed with the entities. This can be very useful when database configuration changes depending on the environment, for example.
Configuration by exception (aka programming by exception): like many other Java EE 6 specifications, JPA uses the concept of configuration by exception: if no metadata (annotations or XML) is defined for the entities then defaults are assumed;


JPA

After years of complaints about Entity CMP 2.x components and in acknowledgment of the success and simplicity of open source frameworks such as Hibernate, the persistence model of the Enterprise Edition was completely rearchitected in Java EE 5. JPA 1.0 was born JPA is an abstraction above JDBC that makes it possible to be independent of SQL. All classes and annotations of this API are in the javax.persistence package.

You can use this API to access and manipulate relational data from:
  • Enterprise Java Beans (EJBs);
  • Web components;
  • Java SE applications;
The main components of JPA are as follows:
  • ORM, which is the mechanism to map objects to data stored in a relational database. With JPA its possible to map from objects to relationships and inheritance;
  • An entity manager API to perform database-related operations, such as CRUD operations. This API allows you to avoid using the JDBC API directly.
  • The JPQL (Java Persistence Query Language), which allows you to retrieve data with an object-oriented query language.
  • Transactions and locking mechanisms when accessing data concurrently provided by Java Transaction API (JTA). Resource-local (non-JTA) transactions are also supported by JPA.
  • Callback and listeners to hook business logic into the life cycle of a persistent object
ANNOTATIONS


A simple entity example:

@Entity
public class Book {
    @Id @GeneratedValue
    private Long id;
    @Column(nullable = false)
    private String title;
    private Float price;
    @Column(length = 2000)
    private String description;
    // Constructors, getters, setters
}

@Column: used in some attributes to customize the default column mapping;
@Entity: makes the class recognized as an entity. Tells the persistence provider that this is an entity class that is mapped to a database and that can be managed by an EntityManager service;
@Table: tells the EJB container to which database table the bean class should map
@GeneratedValue: the value is automatically generated by the persistence provider;
@Id: defines the primary key;

Note: An entity bean is not required to, but if it implements java.io.Serializable allows them to be used as the parameters and return values of the remote interface methods of a session bean. This allows you to use the same class for both persistence and data transfer. [1]






References
[1] - Enterprise Java Beans 3.0 (5th edition)(4.1. Developing an Entity Bean)



Sem comentários:

Enviar um comentário