Eclipse快速上手Hibernate--5. 組件映射

來源:互聯網
上載者:User
    這篇文章主要說的是在Hibernate中的組件(Component)映射,可以參考Hibernate官方文檔的第7章。至於環境設定,可以參考這個系列的前面幾篇文章。  1. 建立項目 ·  建立一個Java項目:ComponentMapping,注意選中“建立單獨的源檔案夾和輸出檔案夾”,同時添加“使用者庫”:hibernate。   2. 編寫類檔案 ·  建立一個類,包名:javamxj.hibernate.component,類名:Person。
Person.java

/* * Hibernate - 組件(Component)映射 * 建立日期 2005-4-10 * @author javamxj(分享java快樂) * @link Blog: htpp://javamxj.mblogger.cn * htpp://blog.csdn.net/javamxj/ */package javamxj.hibernate.component;/** * @hibernate.class */public class Person {private Long id;private String username;private Address address;/** * @hibernate.id *generator-class="hilo" *unsaved-value="null" */public Long getId() {return id;}public void setId(Long id) {this.id = id;}/** * @hibernate.property * length="15" * unique="true" * not-null="true" */public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}/** * @hibernate.component */public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}}
·  Person類調用了Address類,注意在“getAddress()”方法上的“ @hibernate.component”標記。 ·  Address類只含有一些“ @hibernate.property”標記,沒有將其獨立映射為一個表。
Address.java

package javamxj.hibernate.component;public class Address {private String country;private String city;private String street;private String zipCode;public Address() {}public Address(String country, String city, String street, String zipcode) {super();this.country = country;this.city = city;this.street = street;this.zipCode = zipcode;}/** * @hibernate.property * length = "12" */public String getCity() {return city;}public void setCity(String city) {this.city = city;}/** * @hibernate.property * length = "12" */public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}/** * @hibernate.property * length = "6" */public String getZipCode() {return zipCode;}public void setZipCode(String number) {this.zipCode = number;}/** * @hibernate.property * length = "12" */public String getStreet() {return street;}public void setStreet(String street) {this.street = street;}public String toString(){return ("居住在"+ country + city+"市"+ street+"區" +"\n\t郵遞區號: "+ zipCode);}}
  3. 運行任務 ·  複製《Eclipse快速上手Hibernate--4. 繼承映射(1)》文中的build.xml到項目目錄下。·  雙擊“generate-hbm”任務,會發現在包中多了一個Animal.hbm.xml檔案,在src目錄下會多了一個hibernate.cfg.xml檔案,如果沒有,按F5鍵重新整理一下。
Person.hbm.xml

<?xml version="1.0" encoding="GBK"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"><hibernate-mapping> <class name="javamxj.hibernate.component.Person" dynamic-update="false" dynamic-insert="false" select-before-update="false" optimistic-lock="version" > <id name="id" column="id" type="java.lang.Long" unsaved-value="null" > <generator class="hilo"> <!-- To add non XDoclet generator parameters, create a file named hibernate-generator-params-Person.xml containing the additional parameters and place it in your merge dir. --> </generator> </id> <property name="username" type="java.lang.String" update="true" insert="true" access="property" column="username" length="15" not-null="true" unique="true" /> <component name="address" class="javamxj.hibernate.component.Address" > <property name="city" type="java.lang.String" update="true" insert="true" access="property" column="city" length="12" /> <property name="country" type="java.lang.String" update="true" insert="true" access="property" column="country" length="12" /> <property name="zipCode" type="java.lang.String" update="true" insert="true" access="property" column="zipCode" length="6" /> <property name="street" type="java.lang.String" update="true" insert="true" access="property" column="street" length="12" /> </component> <!-- To add non XDoclet property mappings, create a file named hibernate-properties-Person.xml containing the additional properties and place it in your merge dir. --> </class></hibernate-mapping>
  · 運行MySql伺服器,然後雙擊“schemaexport”任務,在項目根目錄下,會產生一個“schema-export.sql”檔案。schema-export.sql
drop table if exists Persondrop table if exists hibernate_unique_keycreate table Person ( id bigint not null, username varchar(15) not null unique, city varchar(12), country varchar(12), zipCode varchar(6), street varchar(12), primary key (id))create table hibernate_unique_key ( next_hi integer )insert into hibernate_unique_key values ( 0 )
 ·  切換到資料庫中,會發現已經自動產生了資料表Person 5. 測試程式 ●   這一次不同於前面的幾個執行個體,這次先實現一個HibernateUtil輔助類,用來管理Hibernate的Session,這樣測試類別的代碼就比較簡單了,可以參考Hibernate官方文檔的第1章。 ·  建立一個類,包名:javamxj.hibernate.util,類名:HibernateUtil,代碼如下:
HibernateUtil.java

package javamxj.hibernate.util;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;import net.sf.hibernate.SessionFactory;import net.sf.hibernate.Transaction;import net.sf.hibernate.cfg.Configuration;public class HibernateUtil {private static Log log = LogFactory.getLog(HibernateUtil.class);private static SessionFactory sessionFactory;private static final ThreadLocal threadSession = new ThreadLocal();private static final ThreadLocal threadTransaction = new ThreadLocal();public static SessionFactory getSessionFactory() {if (sessionFactory == null) {try {// Create the SessionFactorysessionFactory = new Configuration().configure().buildSessionFactory();} catch (HibernateException ex) {ex.printStackTrace();throw new RuntimeException("Configuration problem: "+ ex.getMessage(), ex);}}return sessionFactory;}public static Session currentSession() throws HibernateException {Session s = (Session) threadSession.get();// Open a new Session, if this Thread has none yetif (s == null) {s = getSessionFactory().openSession();log.debug("###Opening new Session for this thread:" + s);threadSession.set(s);} else {log.debug("###Session was existed:" + s);}return s;}public static void closeSession() throws HibernateException {Session s = (Session) threadSession.get();threadSession.set(null);if (s != null) {log.debug("###Closing Session of this thread. " + s);s.close();}}public static void beginTransaction() throws HibernateException {Transaction tx = (Transaction) threadTransaction.get();try {if (tx == null) {tx = currentSession().beginTransaction();log.debug("###Starting new database transaction in this thread:"+ tx);threadTransaction.set(tx);} else {log.debug("###Tx was existed:" + tx);}} catch (HibernateException ex) {throw ex;}}public static void commitTransaction() throws HibernateException {Transaction tx = (Transaction) threadTransaction.get();try {if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {log.debug("###Committing database transaction of this thread.");tx.commit();}threadTransaction.set(null);} catch (HibernateException ex) {rollbackTransaction();throw ex;}}public static void rollbackTransaction() throws HibernateException {Transaction tx = (Transaction) threadTransaction.get();try {threadTransaction.set(null);if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()) {log.debug("###Tyring to rollback database transaction of this thread.");tx.rollback();}} catch (HibernateException ex) {throw ex;} finally {closeSession();}}}
  ·  好了,然後在包javamxj.hibernate.component下建立一個ComponentDemo.java類。
ComponentDemo.java

package javamxj.hibernate.component;import java.util.Iterator;import java.util.List;import javamxj.hibernate.util.HibernateUtil;import net.sf.hibernate.HibernateException;import net.sf.hibernate.Session;public class ComponentDemo {public static void main(String[] args) {try {new ComponentDemo();} catch (HibernateException he) {he.printStackTrace();}}public ComponentDemo() throws HibernateException {Session sess = HibernateUtil.currentSession();Person p = new Person();p.setAddress(new Address("中國", "上海", "普陀", "200055"));p.setUsername("JavaMXJ");sess.save(p);p = new Person();p.setAddress(new Address("中國", "北京", "海澱", "100086"));p.setUsername("張三");sess.save(p);List animals = sess.find("from " + Person.class.getName());for (Iterator it = animals.iterator(); it.hasNext();) {Person person = (Person) it.next();System.out.println(person.getUsername() + ":" + person.getAddress());}HibernateUtil.closeSession();}}
 ·  運行這個類,控制台輸出如下: ·  同時,資料表中產生如下資料: ·  最後的項目結構如下:   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.