一般来讲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")