zznj1123 发表于 2013-1-30 01:40:55

Hibernate EntityManager,Hibernate Annotations 分别都是

<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;    @Transientprivate 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)@JoinColumnprivate Level level;   @ManyToOne@JoinColumnprivate 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。
页: [1]
查看完整版本: Hibernate EntityManager,Hibernate Annotations 分别都是