六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 290|回复: 0

Hibernate EntityManager,Hibernate Annotations 分别都是

[复制链接]

升级  31.33%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
97
 楼主| 发表于 2013-1-30 01:40:55 | 显示全部楼层 |阅读模式
<div class="cnt">Hibernate EntityManager 是 EJB3 persistence specification 的實作,而 Hibernate Annotations 則有 annotations 去定義 O/R Mapping,可省去外部 XML 定義。

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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091import 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
123456789101112131415161718<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。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表