JPA学习笔记-第一部分

一般来讲JPA包含以下几个文件:
1.pojo类
2.映射文件
3.持久层映射文件(Persistence units)
1,2主要用来映射关系数据库中的实体,3主要用来定义JPAProvide,数据库连接定义等操作.

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;
//默认
public Customer(){};
//最小填充
public 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
public Integer getCustId() {
return custId;
}
public void setCustId(Integer custId) {
this.custId = custId;
}
public String getFirst_Name() {
return first_Name;
}
public void setFirst_Name(String first_Name) {
this.first_Name = first_Name;
}
public String getLast_Name() {
return last_Name;
}
public void setLast_Name(String last_Name) {
this.last_Name = last_Name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getAppt() {
return appt;
}
public void setAppt(String appt) {
this.appt = appt;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getCustType() {
return custType;
}
public void setCustType(String custType) {
this.custType = custType;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
}



<?xml version="1.0"?>
<persistence>
<persistence-unit name="testjpa" transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>entity.Customer</class>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:derby://localhost:1527/D:\OpenJPA\Derby\testdb;create=true"/>
<property name="openjpa.ConnectionDriverName"
value="org.apache.derby.jdbc.ClientDriver"/>
<property name="openjpa.ConnectionUserName" value="admin"/>
<property name="openjpa.ConnectionPassword" value="admin"/>
<property name="openjpa.Log" value="SQL=TRACE"/>
</properties>
</persistence-unit>
</persistence>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值