2015年1月22日 星期四

Hibernate Hello World Example 專案 之 Annotation Mapping

Hibernate Hello World Example 專案 之 Annotation Mapping

請先學習此篇:
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 目錄
























其它文章

標籤

Oracle (149) Oracle DB (144) Oracle_DB (143) Oracle SQL (135) JAVA (82) css-基本類 (65) CSS Selector (58) MySQL (58) jQuery (49) JavaScript-基本類 (39) JavaScript (37) JavaScript HTML DOM (37) JavaScript-HTML_DOM (36) CSS3 (30) JAVA-基本類 (28) jQuery UI (27) Apache (23) Oracle GROUP BY (20) datepicker (20) Android (18) Oracle Date (17) c (17) JAVA-lang套件 (16) Linux (16) Oracle Sub Query (16) Spring-基本類 (16) jQuery-基本類 (16) MySQL-進階系列教學 (15) Android基本類 (14) Grails (14) Oracle join (14) SQLite (13) Spring (13) WIN7-基本類 (13) grails-基本類 (13) CKEditor (12) JAVA-流程控制類 (12) JAVA_Spring (12) PHP (11) linux cent os (11) MySQL-基本系列教學 (10) SQLite for java (10) Windows (10) c/c++ (10) Notepad++ (9) eclipse (9) jQuery-Selector (9) sqldeveloper (9) DB_Toad (8) JAVA_IDE_Eclipse (8) JavaScript-String類 (8) MySQL DB Toad (8) MySQL-DATE相關 (8) MySQL-函式相關 (8) Spring Bean (8) Android Studio (7) Hibernate (7) JAVA-OCWCD (7) JavaScript-陣列類 (7) JAVA-程式分享 (6) JAVA.util套件 (6) JavaScript-數學類 (6) MinGw (6) MySQL-其它類 (6) Spring MVC (6) Apache_Tomcat (5) Apache套件_POI (5) CSS (5) JavaScript-Date物件 (5) JavaScript-其它類 (5) PostgreSQL (5) httpd (5) log4j (5) 基本資訊 (5) CSS Properties (4) Dev-C++ (4) Oracle DDL (4) Servlet (4) apache_Maven (4) Android NDK (3) Eclipse IDE for C/C++ (3) HTML5 (3) Hibernate-基本類 (3) JAVA-問題 (3) JAVA-綀習分享 (3) Linux 指令 (3) Proxy Server (3) Spring Mobile (3) Squid (3) maven (3) zk (3) 生活其它 (3) Bootstrap (2) JAVA_IO (2) JAVA_其它_itext套件 (2) JBoss-問題 (2) Jboss (2) MySQL-語法快速查詢 (2) Spring AOP (2) Spring Batch (2) Spring Boot (2) Spring i18n (2) Subversive (2) Tomcat 8 (2) UML (2) c++ (2) c語言綀習題 (2) jQuery Mobile (2) jQuery-事件處理 (2) jQuery-套件類 (2) svn (2) weblogic (2) Apache_JMeter (1) Apache套件_BeanUtils (1) Apache套件_StringUtils (1) Google API (1) HTML5-基本類 (1) JAVA 7 (1) JAVA 日期 (1) JAVA-OCJP (1) JAVA-WEB (1) JAVA_IDE (1) JAVA其它 (1) JBoss Server (1) JMX (1) JSP (1) Java RMI (1) Java String (1) Joda Time (1) Linux_其它 (1) MySQL教學 (1) Oracle_VirtualBox (1) SQL Server (1) SWT (1) Session (1) Struts 2 (1) Tool (1) TortoiseSVN (1) ZK Studio (1) csv (1) grails-其它類 (1) jQuery-進階 (1) java mail (1) java web (1) jsoup (1) modules (1) tomcat (1) 其它類 (1) 圖片工具 (1) 模擬器 (1) 櫻桃鴨 (1) 泰山 (1) 聊天 (1) 聚餐 (1) 開發工具 (1) 開發資訊 (1) 霸王櫻桃鴨 (1)

精選文章

初學 Java 的 HelloWorld 程式

撰寫一個JAVA程式 public class HelloWorld{ public static void main(String[ ] args){ System.out.println("我第一支Java程式!!"); } } ...