john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

persistence jpa

orm = object relational mapping (yes, object oriented java needs to map to a relational database for persistence)
i.e. a Relational Database = Apache Derby is open source database written in Java = the reference implementation for JDBC 4.0 and compatible to ANSI-SQL
JDBC: All Java persistence is built on this = the lowest level

jpa = java persistence api = includes a SQL-like Query language for static and dynamic queries (helps abstract away DB vendor specifics)
(implementations include Hibernate, EclipseLink and Apache OpenJPA)
(Note that XML configuration overwrites annotation defined metadata)

dao = data access object (abstraction and modularization of CRUD operation interface)
JDO: Java Data Objects, which is another specification for Java persistence. (e.g., Apache JDO)

Entity = a class that persists  (e.g. Person below) and must define a primary key, must have a non-arg constructor and or not allowed to be final.
(Persistent classes cannot inherit from certain natively-implemented system classes such as java.net.Socket and java.lang.Thread )

The annotation of Id defines it as the primary key, also annotated to be auto-generated

import javax.persistence.Entity;

@Entity
public class Person {

    @Id
    @GeneratedValue
    private Integer id;

    //@Column ( name = "FIRST_NAME" ,  nullable = false , length = 50 )
    private String firstName;       //commented out annotation so by default the Column name will be firstName

    @Version
    @Column(name = "LAST_UPDATED_TIME")
    private Date updatedTime;


The fields of the Entity will be saved in the database unless annotated with @Transient
@Version means this is a field used to determine versioning,
when JPA detects multiple concurrent modifies of the same record it throws an exception to the transaction attempting to commit last


There's also relationship mapping

@OneToOne
@OneToMany
@ManyToOne
@ManyToMany


 javax.persistence.EntityManager provides the operations from and to the database


src/META-INF/persistence.xml   (must be created if it does not already exist, in heroku it was in src/main/resources/META-INF/persistence.xml)

http://www.vogella.de/articles/JavaPersistenceAPI/article.html
http://hsqldb.org/
http://www.h2database.com/html/main.html

  • « MS SQL Examples continued tab delimited
  • jdbc preparestatement »

Published

Apr 8, 2012

Category

java

~287 words

Tags

  • java 252
  • jpa 1
  • persistence 1