`
zznj1123
  • 浏览: 123224 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Hibernate EntityManager,Hibernate Annotations 分别都是

阅读更多
Hibernate EntityManager 是 EJB3 persistence specification 的實作,而 Hibernate Annotations 則有 annotations 去定義 O/R Mapping,可省去外部 XML 定義。

早期 Hibernate 在做 O/R mapping 時需要定義 *.hbm.xml 檔,但在 Java 5.0 導入 annotation 後,現在我們可以用下列方式來定義:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import java.util.Date;
import java.util.Set;
 
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;
 
import org.cyberjos.sylphie.render.Renderable;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
 
/**
 * This is an object that contains data related to the 'Post' table.
 * 
 * @author Joseph S. Kuo
 * @version $Revision: 1.26 $, $Date: 2007/02/22 10:50:10 $
 * @see BaseObject
 * @since 0.1a
 */
@Entity
@org.hibernate.annotations.Entity(dynamicUpdate = true)
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {
  "path"
}))
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@SequenceGenerator(name="POST_SEQ", sequenceName="post_id_seq")
public class Post extends BaseObject implements Renderable{
  /**
   * The name of the column 'id' in the table.
   */
  public static final String PROP_ID = "id";
 
  // a lot of PROP_xxx constants ...
 
  @Id
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="POST_SEQ")
  private Integer id;
 
  @Column(length = 300)
  private String path;
  
  @Transient
  private String link;
 
  private String title;
 
  @Lob
  @Column(nullable = true)
  private String content;
 
  @Temporal(TemporalType.TIMESTAMP)
  private Date postTime;
 
  @Column(nullable = true)
  private int viewTimes;
 
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn
  private Level level;
 
  @ManyToOne
  @JoinColumn
  private Category category;
 
  @OneToMany(mappedBy = Referer.PROP_POST)
  @OrderBy(Referer.PROP_LINK_TIMES + ORDER_BY_DESC)
  private Set<Referer> referers;
 
  @OneToMany(mappedBy = Ping.PROP_POST)
  // @LazyCollection(LazyCollectionOption.TRUE) defaults to true
  @OrderBy(Ping.PROP_PING_TIME)
  private Set<Ping> pings;
 
  // a lot of fields ...
 
  // a lot of getter and setter methods ...
}


接著在 hibernate.cfg.xml 中改用下列方式,而無需再用 *.hbm.xml:
1
<mapping class="org.cyberjos.sylphie.bean.Post" />


或設定 EJB3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<persistence 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_1_0.xsd"
   version="1.0">
   <persistence-unit name="sylphie" transaction-type="JTA">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>java:/SylphieDatabaseDS</jta-data-source>
      <jar-file>sylphie.jar</jar-file>
      <class>org.cyberjos.sylphie.bean.Post</class>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
         <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
         <property name="hibernate.connection.username" value="xxxx"/>
         <property name="hibernate.connection.password" value="xxxx"/>
         <property name="hibernate.connection.url" value="jdbc:postgresql:sylphiedb"/>
      </properties>
   </persistence-unit>
</persistence>


大致上,Hibernate 負責底層與各種資料庫之間的連線管理、交易處理,Hibernate Annotataions 負責 O/R Mapping,Hibernate EntityManager 負責 EJB3 persistence。
分享到:
评论

相关推荐

    hibernate jar包:hibernate-commons-annotations-4.0.1.Final.jar等

    hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...

    hibernate-annotations-3.4.0.GA and hibernate-entitymanager-3.4.0.GA

    本篇将详细探讨`hibernate-annotations-3.4.0.GA`和`hibernate-entitymanager-3.4.0.GA`这两个版本中的核心知识点,尤其是它们在注释和枚举映射方面的应用。 首先,`hibernate-annotations`是Hibernate提供的一套...

    Hibernate core+annotations+entitymanager 指南小集合

    本指南小集合将详细探讨Hibernate的核心(Core)、注解(Annotations)以及实体管理器(EntityManager)这三个关键组件。 **一、Hibernate Core** Hibernate Core是Hibernate框架的基础部分,它提供了基本的ORM功能...

    hibernate-annotations jar包

    使用注解Annotation时用的jar包。 包括: hibernate-commons-annotations-3.3.0.ga.jar hibernate-entitymanager.jar ejb3-persistence.jar hibernate-annotations.jar

    hibernate-annotations 相关jar包

    1. 引入Hibernate和Hibernate Annotations相关jar包:包括hibernate-annotations.jar、hibernate-commons-annotations.jar、hibernate-entitymanager.jar以及JPA规范所需的jar包。 2. 创建实体类:在实体类上添加@...

    hibernate所需jar包

    hibernate所需jar包,antlr-2.7.7.jar,dom4j-1.6.1.jar,geronimo-jta_1.1_spec-1.1.1.jar,hibernate-commons-annotations-5.0.1.Final.jar,hibernate-core-5.0.12.Final.jar,hibernate-entitymanager-5.0.7....

    hibernate annotations

    **标题:“Hibernate Annotations”** ...熟练掌握Hibernate Annotations对于任何Java EE开发者来说都是必要的技能。通过深入学习和实践,可以更好地利用这个强大的工具来优化数据库访问层的代码。

    hibernate-annotations-3.4.0.jar

    在实际项目中,结合其他Hibernate组件如Hibernate Core、Hibernate EntityManager等,可以构建出高效、灵活的Java持久化解决方案。学习并熟练掌握这些注解,对于提升Java开发人员的技能水平至关重要。

    hibernate配置文件中所有jar包(包括数据库连接等)

    hibernate-commons-annotations-3.2.0.Final/ hibernate-core-5.3.0.Final-sources/ hibernate-entitymanager-5.3.0.Final/ hibernate-jpa-2.0-api-1.0.1.Final/ hibernate-release-5.0.7.Final/ jandex-2.0.5.Final...

    hibernate-jar.zip

    hibernate-commons-annotations-3.2.0.Final.jar,hibernate-commons-annotations-5.1.2.Final.jar,hibernate-commons-annotations-5.1.2.Final-sources.jar,hibernate-core-5.4.33.jar,hibernate-entitymanager-...

    开发hibernate-annotations-3.4必须的包

    对应的文件可能为`hibernate-annotations.jar`或`hibernate-entitymanager.jar`,因为从3.4版本开始,Hibernate Annotations与Hibernate EntityManager合并了。 3. **Hibernate Common Annotations**: 包含了常用的...

    hibernate jar包程序文件

    hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...

    hibernate,hibernate3,Middlegen-Hibernate-r5

    "hibernate3"指的是Hibernate的第三个主要版本,该版本在性能和功能上都有显著提升。它引入了Criteria查询,这是一种更灵活的查询方式,允许开发者动态构建查询条件,增强了代码的可读性和可维护性。此外,Hibernate...

    Hibernate lib

    总的来说,"Hibernate lib"压缩包是一个非常实用的资源,包含了进行Hibernate开发所需的各种库,对于初学者或有经验的开发者来说都是一个宝贵的参考资料。通过学习和使用这些库,可以深入理解ORM的概念,提高开发...

    Struts2+Spring2.5+Hibernate3.3整合开发之Sring与Hibernate整合

    hibernate-entitymanager.jar lib\test\log4.jar、slf4j-log4j12.jar Spring安装包下的: dist\spring.jar lib\c3p0\c3p0-0.9.12.jar lib\aspecti\aspectweaver.jar、aspectjrt.jar lib\cglib\cglib-nodep-2.1...

    hibernate-release-4.1.4

    【描述】中的"hibernate的jar包"指的是Hibernate框架的运行库文件,这些JAR文件包含了Hibernate的所有核心API、实现和依赖库,如Hibernate Commons Annotations、Hibernate EntityManager、Hibernate Core等。...

    hibernate-annotations-3.4.0.CR2正式版.zip

    在Hibernate 3.4.0.CR2版本中,引入了对Java Persistence API (JPA)的全面支持,特别是其注解(Hibernate Annotations)部分,极大地简化了数据持久化的工作。本文将详细介绍Hibernate注解以及在实际项目中的应用。 ...

    hibernate-annotations3.4.0.GA.zip

    《Hibernate Annotations 3.4.0.GA:深入解析与应用》 Hibernate是Java领域中广泛应用的对象关系映射(ORM)框架,它极大地简化了数据库操作。Hibernate Annotations是Hibernate的一个重要组成部分,它允许开发者...

    hibernate3必要jar包

    - **hibernate-entitymanager.jar**:扩展了hibernate-core,提供了对JPA规范的实现。 - **hibernate-commons-annotations.jar**:包含通用的注解,用于支持Hibernate和JPA的注解。 3. **配置文件**:`hibernate....

    hibernate5需要的相关jar包

    hibernate-commons-annotations-5.0.1.Final.jar hibernate-core-5.0.7.Final.jar hibernate-entitymanager-5.0.7.Final.jar hibernate-jpa-2.1-api-1.0.0.Final.jar hibernate5-needed-jars.rar jandex-2.0.0...

Global site tag (gtag.js) - Google Analytics