一般来讲JPA包含以下几个文件:
1.pojo类
2.映射文件
3.持久层映射文件(Persistence units)
1,2主要用来映射关系数据库中的实体,3主要用来定义JPAProvide,数据库连接定义等操作.
下面来看具体的POJO类的写法
package com.liliang.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Version;
import java.io.Serializable;
import java.util.Date;
//JPA必须拥有一个实体类,对应数据库中的表.
//实体类的要求是:
//1.首先需要用注解的方式在类开头声明:@Entity()声明
//2.必须包含一个空的构造函数,并且必须是public或是protected修饰符.
//3.必须是top-levle类,即enum或是interface是不可以用作当成实体类的.
//4.不可以是final.如果实体类有可能是托管对象,那么就必须实现Serialization接口.
//我们使用注解的方式来实现.
//关于注解的解释:
//1.@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.)
//
//2.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.
//3.The entity class should have a default no-argument constructor.
//4.The entity class should not be final.
//5.Persistent classes cannot inherit from certain natively-implemented system classes such as java.net.Socket and java.lang.Thread.
//6.If a persistent class inherits from a non-persistent class, the fields of the non-persistent super class cannot be persisted.
@Entity(name="Customer")
public class Customer implements Serializable {
/**
* 主机自动生成
*/
private static final long serialVersionUID = -4020535715918096623L;
@Id //代表主键
@Column(name="CUST_ID",nullable=false) //映射表中的一列
@GeneratedValue(strategy = GenerationType.AUTO) //标示符生成策略
private Integer custId;
@Column(name="FIRST_NAME",nullable=true,length=50)
private String first_Name;
@Column(name="LAST_NAME",nullable=false,length=50)
private String last_Name;
@Column(name="STREET",nullable=false,length=50)
private String street;
@Column(name="APPT",nullable=true,length=20)
private String appt;
@Column(name="CITY",nullable=false,length=25)
private String city;
@Column(name="ZIP_CODE",nullable=true,length=10)
private String zipCode;
@Column(name="CUST_TYPE",nullable=true,length=10)
private String custType;
@Version
@Column(name="LAST_UPDATE_TIME",nullable=true)
private Date updateTime;
//默认
Customer() Customer(){};
//最小填充
Customer() Customer(Integer custId, String last_Name, String street, String city){
this.custId = custId;
this.last_Name = last_Name;
this.street = street;
this.city = city;
}
//getter and setter method
好了,下面来看映射文件的写法
<?xml version="1.0" encoding="UTF-8"?>
<!--
1.该xml文件可以包含多个persistent元素,每个元素都可以定义自己的持久层实现类和数据库 2.实体类在<class>标签中声明
-->
<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_1_0.xsd"
version="1.0">
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<!-- 使用OpenJPA持久层实现 -->
<!--
Provider class that supplies EntityManagers for this persistence unit
-->
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<!-- 映射实体类 -->
<class>com.liliang.entity.Customer</class>
<!-- A list of vendor-specific properties. -->
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/hibernate" />
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver" />
<property name="openjpa.ConnectionUserName" value="root" />
<property name="openjpa.ConnectionPassword" value="1d280478" />
<property name="openjpa.Log" value="SQL=TRANCE" />
</properties>
</persistence-unit>
</persistence>
责任编辑:小草