Java Persistence API(JPA)是EJB3.0规范之一,定义了对数据库数据进行持久化操作的接口。HIbernate
使用HIbernate Annotation和Hibernate EntityManager实现JPA。
下载HIbernate EntityManager:
得到的jar包:
hibernate-entitymanager.jar
hibernate-archive-browing.jar
Product.java
1 package com.b510.examples; 2 3 import javax.persistence.Column; 4 import javax.persistence.Entity; 5 import javax.persistence.FetchType; 6 import javax.persistence.GeneratedValue; 7 import javax.persistence.Id; 8 import javax.persistence.JoinColumn; 9 import javax.persistence.ManyToOne; 10 import javax.persistence.Table; 11 import org.hibernate.annotations.GenericGenerator; 12 13 @Entity 14 @Table(name = "product", catalog = "users") 15 public class Product implements java.io.Serializable { 16 17 private static final long serialVersionUID = -1546206493725028472L; 18 private Integer id; 19 private Category category; 20 private String name; 21 private String price; 22 private String descripton; 23 24 public Product() { 25 } 26 27 public Product(Category category, String name, String price, 28 String descripton) { 29 this.category = category; 30 this.name = name; 31 this.price = price; 32 this.descripton = descripton; 33 } 34 35 @GenericGenerator(name = "generator", strategy = "increment") 36 @Id 37 @GeneratedValue(generator = "generator") 38 @Column(name = "id", unique = true, nullable = false) 39 public Integer getId() { 40 return this.id; 41 } 42 43 public void setId(Integer id) { 44 this.id = id; 45 } 46 47 // 延迟加载:多对一方式 48 // 关联信息:外键name = "category_id" 49 @ManyToOne(fetch = FetchType.LAZY) 50 @JoinColumn(name = "category_id") 51 public Category getCategory() { 52 return this.category; 53 } 54 55 public void setCategory(Category category) { 56 this.category = category; 57 } 58 59 @Column(name = "name", length = 500) 60 public String getName() { 61 return this.name; 62 } 63 64 public void setName(String name) { 65 this.name = name; 66 } 67 68 @Column(name = "price", length = 10) 69 public String getPrice() { 70 return this.price; 71 } 72 73 public void setPrice(String price) { 74 this.price = price; 75 } 76 77 @Column(name = "descripton", length = 500) 78 public String getDescripton() { 79 return this.descripton; 80 } 81 82 public void setDescripton(String descripton) { 83 this.descripton = descripton; 84 } 85 86 }
src/META-INF/persistence.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <persistence version="1.0" 3 xmlns:persistence="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.23.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence persistence_1_0.xsd "> 5 6 <persistence-unit name="entityManager"> 7 <provider>org.hibernate.ejb.HibernatePersisttence 8 </provider> 9 <properties> 10 <property name="hibernate.archive.autodetection" value="class, hbm" /> 11 <property name="hibernate.show_sql" value="true" /> 12 <property name="hibernate.formate_sql" value="true" /> 13 <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 14 <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" /> 15 <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3307/users" /> 16 <property name="hibernate.connection.username" value="root" /> 17 <property name="hibernate.connection.password" value="root" /> 18 </properties> 19 </persistence-unit> 20 </persistence> 21 22 23
测试代码:
HibernateTestEntityManager.java
1 /** 2 * 3 */ 4 package com.b510.examples; 5 6 import javax.persistence.EntityManager; 7 import javax.persistence.EntityManagerFactory; 8 import javax.persistence.EntityTransaction; 9 import javax.persistence.Persistence; 10 11 /** 12 * 13 * @author XHW 14 * 15 * @date 2011-7-20 16 * 17 */ 18 public class HibernateTestEntityManager { 19 20 /** 21 * @param args 22 */ 23 public static void main(String[] args) { 24 new HibernateTestEntityManager().add(); 25 } 26 27 public void add() { 28 EntityManagerFactory emf = Persistence 29 .createEntityManagerFactory("entityManager"); 30 EntityManager em = emf.createEntityManager(); 31 Product p = new Product(); 32 33 p.setName("Hibernate EntityManager"); 34 p.setPrice("12"); 35 p.setDescripton("Hibernate EntityManager test"); 36 37 EntityTransaction tx = em.getTransaction(); 38 tx.begin(); 39 // 保存p对象 40 em.persist(p); 41 tx.commit(); 42 } 43 44 }