Hibernate EntityManager

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 }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏ml

HDUOJ---What Are You Talking About

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Lim...

398140
来自专栏闻道于事

JavaWeb之ssm框架整合,用户角色权限管理

SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring ...

2.5K40
来自专栏绿巨人专栏

BookNote: Refactoring - Improving the Design of Existing Code

29860
来自专栏一个会写诗的程序员的博客

《JSP极简教程》c:forEach 如何输出序号

11330
来自专栏杂烩

hadoop MapReduce编写一个分组统计并排序查询-排序

执行命令时注意参数位置,正确的执行命令应该是如下(假如打包的jar放在hadoop根目录下的mylib,jar名称为groutcount):

31340
来自专栏码匠的流水账

spring mvc如何计算BEST_MATCHING_PATTERN_ATTRIBUTE

spring-webmvc-4.3.10.RELEASE-sources.jar!/org/springframework/web/servlet/Dispat...

15520
来自专栏测试开发架构之路

注解与依赖注入

1.Product package com.how2java.pojo; import org.springframework.beans.factory.a...

30460
来自专栏算法修养

POJ-2777 Count Color(线段树,区间染色问题)

Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: ...

35540
来自专栏码匠的流水账

聊聊spring cloud gateway的streaming-media-types属性

本文主要研究下spring cloud gateway的streaming-media-types属性

26610
来自专栏码匠的流水账

reactor-netty的TcpClient如何往eventLoop提交task

本文主要研究一下reactor-netty的TcpClient如何往eventLoop提交task

49020

扫码关注云+社区

领取腾讯云代金券