JPA快速入门

一、关于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 

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">     
  3.     <persistence-unit name="JPA002" transaction-type="RESOURCE_LOCAL">  
  4.         <class>com.tan.jpa.bean.Person</class>  
  5.         <class>com.tan.jpa.bean.Customer</class>  
  6.         <class>com.tan.jpa.bean.Order</class>  
  7.     <properties>  
  8.         <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />  
  9.         <property name="javax.persistence.jdbc.url" value="jdbc:mysql:///tan " />  
  10.         <property name="javax.persistence.jdbc.user" value="root" />  
  11.         <property name="javax.persistence.jdbc.password" value="123456" />  
  12.   
  13.         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />  
  14.         <property name="hibernate.hbm2ddl.auto" value="update" />  
  15.         <property name="hibernate.show_sql" value="true" />  
  16.         <property name="hibernate.format_sql" value="false" />  
  17.   
  18.     </properties>  
  19.   
  20.     </persistence-unit>  
  21. </persistence>  
   

    3.新建各种Bean

Person实体:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Creation : 2015年3月25日 
  3.  */  
  4. package com.tan.jpa.bean;  
  5.   
  6. import java.util.Date;  
  7.   
  8. import javax.persistence.Column;  
  9. import javax.persistence.Entity;  
  10. import javax.persistence.GeneratedValue;  
  11. import javax.persistence.GenerationType;  
  12. import javax.persistence.Id;  
  13. import javax.persistence.Lob;  
  14. import javax.persistence.Table;  
  15. import javax.persistence.Temporal;  
  16. import javax.persistence.TemporalType;  
  17.   
  18. @Entity  
  19. @Table(name = "person")  
  20. public class Person {  
  21.     @Id  
  22.     @GeneratedValue(strategy = GenerationType.AUTO)  
  23.     private Integer id;  
  24.   
  25.     @Column(name = "name", length = 10, nullable = false)  
  26.     private String name;  
  27.   
  28.     @Temporal(TemporalType.DATE)  
  29.     // 指定为日期类型  
  30.     private Date birth;  
  31.   
  32.     @Lob  
  33.     // 指定为处理大文本或二进制数据的映射注解  
  34.     private String info;  
  35.   
  36.     public Person() {  
  37.     }  
  38.   
  39.     public Integer getId() {  
  40.         return id;  
  41.     }  
  42.   
  43.     public void setId(Integer id) {  
  44.         this.id = id;  
  45.     }  
  46.   
  47.     public String getName() {  
  48.         return name;  
  49.     }  
  50.   
  51.     public void setName(String name) {  
  52.         this.name = name;  
  53.     }  
  54.   
  55.     public Date getBirth() {  
  56.         return birth;  
  57.     }  
  58.   
  59.     public void setBirth(Date birth) {  
  60.         this.birth = birth;  
  61.     }  
  62.   
  63.     public String getInfo() {  
  64.         return info;  
  65.     }  
  66.   
  67.     public void setInfo(String info) {  
  68.         this.info = info;  
  69.     }  
  70.   
  71.     public Person(String name, Date birth, String info) {  
  72.         super();  
  73.         this.name = name;  
  74.         this.birth = birth;  
  75.         this.info = info;  
  76.     }  
  77.   
  78.     public Person(String name, String info) {  
  79.         super();  
  80.         this.name = name;  
  81.         this.info = info;  
  82.     }  
  83.   
  84.     @Override  
  85.     public String toString() {  
  86.         return "Person [id=" + id + ", name=" + name + ", birth=" + birth + ", info=" + info + "]";  
  87.     }  
  88.   
  89. }  

Customer实体:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Creation : 2015年3月26日 
  3.  */  
  4. package com.tan.jpa.bean;  
  5.   
  6. import java.util.HashSet;  
  7. import java.util.Set;  
  8.   
  9. import javax.persistence.Column;  
  10. import javax.persistence.Entity;  
  11. import javax.persistence.GeneratedValue;  
  12. import javax.persistence.GenerationType;  
  13. import javax.persistence.Id;  
  14. import javax.persistence.OneToMany;  
  15. import javax.persistence.Table;  
  16.   
  17. @Table(name = "customer")  
  18. @Entity  
  19. public class Customer {  
  20.     @Id  
  21.     @GeneratedValue(strategy = GenerationType.AUTO)  
  22.     private Integer id;  
  23.   
  24.     @Column(name = "Last_Name")  
  25.     private String lastName;  
  26.   
  27.     @OneToMany(mappedBy = "customer")  
  28.     private Set<Order> orders = new HashSet<Order>();  
  29.   
  30.     public Integer getId() {  
  31.         return id;  
  32.     }  
  33.   
  34.     public void setId(Integer id) {  
  35.         this.id = id;  
  36.     }  
  37.   
  38.     public String getLastName() {  
  39.         return lastName;  
  40.     }  
  41.   
  42.     public void setLastName(String lastName) {  
  43.         this.lastName = lastName;  
  44.     }  
  45.   
  46.     public Set<Order> getOrders() {  
  47.         return orders;  
  48.     }  
  49.   
  50.     public void setOrders(Set<Order> orders) {  
  51.         this.orders = orders;  
  52.     }  
  53.   
  54. }  

Order实体:

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /* 
  2.  * Creation : 2015年3月26日 
  3.  */  
  4. package com.tan.jpa.bean;  
  5.   
  6. import javax.persistence.Column;  
  7. import javax.persistence.Entity;  
  8. import javax.persistence.GeneratedValue;  
  9. import javax.persistence.Id;  
  10. import javax.persistence.JoinColumn;  
  11. import javax.persistence.ManyToOne;  
  12. import javax.persistence.Table;  
  13.   
  14. @Table(name = "orders")  
  15. @Entity  
  16. public class Order {  
  17.     @Id  
  18.     @GeneratedValue  
  19.     private Integer id;  
  20.   
  21.     @Column(name = "ORDER_NAME")  
  22.     private String orderName;  
  23.   
  24.     @JoinColumn(name = "CUST_ID")  
  25.     @ManyToOne  
  26.     private Customer customer;  
  27.   
  28.     public Integer getId() {  
  29.         return id;  
  30.     }  
  31.   
  32.     public void setId(Integer id) {  
  33.         this.id = id;  
  34.     }  
  35.   
  36.     public String getOrderName() {  
  37.         return orderName;  
  38.     }  
  39.   
  40.     public void setOrderName(String orderName) {  
  41.         this.orderName = orderName;  
  42.     }  
  43.   
  44.     public Customer getCustomer() {  
  45.         return customer;  
  46.     }  
  47.   
  48.     public void setCustomer(Customer customer) {  
  49.         this.customer = customer;  
  50.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值