請先學習此篇:
Hibernate Hello World Example 專案 之 XML Mapping
一、用上一篇專案來改之。
修改程式
Message.java
HibernateUtil.java
hibernate.cfg.xml
刪除
message.hbm.xml
二、修改 Message.java
Message.java 程式內容:
package com.levin.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; /** * Model class for Message.java */ @Entity @Table(name = "MESSAGE") public class Message implements java.io.Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue @Column(name = "MESSAGE_ID") private Integer messageId; @Column(name = "MESSAGE_CODE") private String messageCode; @Column(name = "MESSAGE_NAME") private String messageName; public Message() { } public Message(String messageCode, String messageName) { this.messageCode = messageCode; this.messageName = messageName; } public Integer getMessageId() { return messageId; } public void setMessageId(Integer messageId) { this.messageId = messageId; } public String getMessageCode() { return messageCode; } public void setMessageCode(String messageCode) { this.messageCode = messageCode; } public String getMessageName() { return messageName; } public void setMessageName(String messageName) { this.messageName = messageName; } }
三、修改 HibernateUtil.java
主要修改內容:
將原來的這段:
return new Configuration().configure().buildSessionFactory();
改為這段程式:
return new AnnotationConfiguration().configure().buildSessionFactory();
HibernateUtil.java 程式內容:
package com.levin; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return new AnnotationConfiguration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } public static void shutdown() { // Close caches and connection pools getSessionFactory().close(); } }
四、修改 hibernate.cfg.xml
主要修改內容:
將原來的這段:
<mapping resource="message.hbm.xml"></mapping>
改為這段程式:
<mapping class="com.levin.model.Message"></mapping>
hibernate.cfg.xml 程式內容:
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> <property name="hibernate.connection.url">jdbc:postgresql://127.0.0.1:5433/hibernateTest</property> <property name="hibernate.connection.username">postgres</property> <property name="hibernate.connection.password">postgres</property> <property name="hibernate.bytecode.use_reflection_optimizer">false</property> <property name="connection_pool_size">1</property> <!-- validate | update | create | create-drop 如果沒要用 set value="none". --> <!-- validate 程式執行驗證資料對應表相關資料。 update 如果table已存在,就更新,不存在則新增 create 每次執行就自動刪除所有對應的table,再新增所有對應的table create-drop 新增所有對應的table , 程式關閉前刪除所有對應的table --> <property name="hbm2ddl.auto">create</property> <property name="show_sql">true</property> <mapping class="com.levin.model.Message"></mapping> </session-factory> </hibernate-configuration>
五、 本例範例資料
圖1
六、相關參考
Struts + Spring + Hibernate 目錄
其它文章
沒有留言:
張貼留言