一般來講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> |
本文出自 「stoneli88」 博客http://stoneli88.blog.51cto.com/333387/215209
[火星人 ] 編程基礎:JPA學習筆記(一)已經有1027次圍觀