JPA 학습 노트. - 1 부.

일반적으로 JPA 는 다음 과 같은 몇 가지 파일 을 포함 합 니 다.
1. pojo 류
2. 맵 파일
3. 영구 층 맵 파일 (영구 단위)
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>

좋은 웹페이지 즐겨찾기