一、关于JPA
关于JPA是什么,在这里不做解释,感兴趣的话可以参照:
http://blog.csdn.net/chjttony/article/details/6086298
http://www.cnblogs.com/holbrook/archive/2012/12/30/2839842.html
二、首先而且必要的事搭建JPA环境
1.千里之行始于jar包
2.配置persistence.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <persistence version="2.0" 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_2_0.xsd">
- <persistence-unit name="JPA002" transaction-type="RESOURCE_LOCAL">
- <class>com.tan.jpa.bean.Person</class>
- <class>com.tan.jpa.bean.Customer</class>
- <class>com.tan.jpa.bean.Order</class>
- <properties>
- <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
- <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///tan " />
- <property name="javax.persistence.jdbc.user" value="root" />
- <property name="javax.persistence.jdbc.password" value="123456" />
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
- <property name="hibernate.hbm2ddl.auto" value="update" />
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.format_sql" value="false" />
- </properties>
- </persistence-unit>
- </persistence>
3.新建各种Bean
Person实体:
- /*
- * Creation : 2015年3月25日
- */
- package com.tan.jpa.bean;
- import java.util.Date;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Lob;
- import javax.persistence.Table;
- import javax.persistence.Temporal;
- import javax.persistence.TemporalType;
- @Entity
- @Table(name = "person")
- public class Person {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Integer id;
- @Column(name = "name", length = 10, nullable = false)
- private String name;
- @Temporal(TemporalType.DATE)
- // 指定为日期类型
- private Date birth;
- @Lob
- // 指定为处理大文本或二进制数据的映射注解
- private String info;
- public Person() {
- }
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getBirth() {
- return birth;
- }
- public void setBirth(Date birth) {
- this.birth = birth;
- }
- public String getInfo() {
- return info;
- }
- public void setInfo(String info) {
- this.info = info;
- }
- public Person(String name, Date birth, String info) {
- super();
- this.name = name;
- this.birth = birth;
- this.info = info;
- }
- public Person(String name, String info) {
- super();
- this.name = name;
- this.info = info;
- }
- @Override
- public String toString() {
- return "Person [id=" + id + ", name=" + name + ", birth=" + birth + ", info=" + info + "]";
- }
- }
Customer实体:
- /*
- * Creation : 2015年3月26日
- */
- package com.tan.jpa.bean;
- import java.util.HashSet;
- import java.util.Set;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- @Table(name = "customer")
- @Entity
- public class Customer {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Integer id;
- @Column(name = "Last_Name")
- private String lastName;
- @OneToMany(mappedBy = "customer")
- private Set<Order> orders = new HashSet<Order>();
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public Set<Order> getOrders() {
- return orders;
- }
- public void setOrders(Set<Order> orders) {
- this.orders = orders;
- }
- }
Order实体:
- /*
- * Creation : 2015年3月26日
- */
- package com.tan.jpa.bean;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinColumn;
- import javax.persistence.ManyToOne;
- import javax.persistence.Table;
- @Table(name = "orders")
- @Entity
- public class Order {
- @Id
- @GeneratedValue
- private Integer id;
- @Column(name = "ORDER_NAME")
- private String orderName;
- @JoinColumn(name = "CUST_ID")
- @ManyToOne
- private Customer customer;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getOrderName() {
- return orderName;
- }
- public void setOrderName(String orderName) {
- this.orderName = orderName;
- }
- public Customer getCustomer() {
- return customer;
- }
- public void setCustomer(Customer customer) {
- this.customer = customer;
- }