Understanding JPA, 2
来源:互联网 发布:java如何获取当前日期 编辑:程序博客网 时间:2024/05/21 00:52
Page 2 of 6
JPA: How to?
You'll need three artifacts to implement a JPA-compliant program:
- An entity class
- A persistence.xml file
- A class through which you will insert, update, or find an entity
JPA is all about data persistence, so let's begin our examination of how it works with the data store design. Assume you have a CUSTOMER table, as in Table 1 below.
Table 1. CUSTOMER table schema
The persistence object: Entity
Since JPA is all about entity-relationship mapping, next you need to take a look at the design of the Customer
entity object. The entity object is nothing but a POJO class marked as an entity with the @Entity
annotation, as you can see in Listing 1.
Listing 1. The Customer entity
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Entity(name = "CUSTOMER") //Name of the entity
public class Customer implements Serializable{
private long custId;
private String firstName;
private String lastName;
private String street;
private String appt;
private String city;
private String zipCode;
private String custType;
private Date updatedTime;
// Getters and setters go here
......................
}
The Customer
entity needs to know how to map the attributes (or properties) to theCUSTOMER table. You can do this either through a configuration filecalled orm.xml (similar to a .hbm file in Hibernate) or, as shown inListing 2, through JPA annotations.
Listing 2. The Customer entity with annotations
import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;
@Entity(name = "CUSTOMER") //Name of the entity
public class Customer implements Serializable{
@Id //signifies the primary key
@Column(name = "CUST_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long custId;
@Column(name = "FIRST_NAME", nullable = false,length = 50)
private String firstName;
@Column(name = "LAST_NAME", length = 50)
private String lastName;
// By default column name is same as attribute name
private String street;
@Column(name = "APPT",nullable = false)
private String appt;
// By default column name is same as attribute name
private String city;
@Column(name = "ZIP_CODE",nullable = false)
// Name of the corresponding database column
private String zipCode;
@Column(name = "CUST_TYPE", length = 10)
private String custType;
@Version
@Column(name = "LAST_UPDATED_TIME")
private Date updatedTime;
// Getters and setters go here
......................
}
Take a closer look at the annotations defined in Listing 2.
- The annotations are defined in
javax.persistence
, so you'll need to import that package. @Enitity
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.@Version
signifies a version field in an entity. JPA uses a version field todetect concurrent modifications to a data store record. When the JPAruntime detects multiple attempts to concurrently modify the samerecord, it throws an exception to the transaction attempting to commitlast. This prevents you from overwriting the previous commit with staledata.- By default, all the fields are of type
@Basic
, which are persisted as-is in the database. @GeneratedValue
signifies a strategyto assign a unique value to your identity fields automatically. Thetypes of strategies available are IDENTITY, SEQUENCE, TABLE, and AUTO.The default strategy is auto, the implementation of which is left tothe JPA vendor to implement. (OpenJPA implements it through a tablesequence.)
There are a few points to keep in mind when creating an entity class:
- 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
andjava.lang.Thread
. - If a persistent class inherits from a non-persistent class, the fields of the non-persistent super class cannot be persisted.
- Understanding JPA, 2
- Understanding JPA, 1
- Understanding JPA,3
- Understanding JPA,4
- Understanding JPA, 5
- Understanding JPA, 6
- Understanding COM Apartments(2)
- Session 2: Understanding OpenStack
- Understanding
- Understanding .NET (2nd Edition)
- TRULY Understanding Dynamic Controls (Part 2)
- TRULY Understanding Dynamic Controls (Part 2)
- Day 2 Understanding the CORBA Architecture
- Understanding the Linux Kernel (2nd Edition)
- Understanding MySQL Internals 2:模块图解
- understanding linux usb ehci device driver(2)
- understanding linux usb ehci device driver(2)
- Understanding HDFS Recovery Processes (Part 2)
- AQtime实战
- C# 绘图中的图片闪烁问题解决方法的使用结果
- 关于次贷款与金融危机的英语学习
- 请看废柴俺
- Understanding JPA, 1
- Understanding JPA, 2
- 自定义GridView 介绍
- Understanding JPA,3
- Understanding JPA,4
- Understanding JPA, 5
- Understanding JPA, 6
- 详解.NET中的动态编译
- Head First C# 中文版 图文皆译 第十一章 事件和代理 page490
- seo高手