Hibernate EntityManager 是 EJB3 persistence specification 的實作,而 Hibernate Annotations 則有 annotations 去定義 O/R Mapping,可省去外部 XML 定義。
早期 Hibernate 在做 O/R mapping 時需要定義 *.hbm.xml 檔,但在 Java 5.0 導入 annotation 後,現在我們可以用下列方式來定義:
接著在 hibernate.cfg.xml 中改用下列方式,而無需再用 *.hbm.xml:
或設定 EJB3
大致上,Hibernate 負責底層與各種資料庫之間的連線管理、交易處理,Hibernate Annotataions 負責 O/R Mapping,Hibernate EntityManager 負責 EJB3 persistence。
早期 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。