Hibernate Component映射的一個簡單例子
建立一個相關的數據庫表:
create table c_user( id int(11) not null auto_increment, age int(11) default null, firstname varchar(50) default null, lastname varchar(50) default null, address varchar(200) default null, zipcode varchar(10) default null, tel varchar(20) default null, primary key(id) )engine=innodb default charset=gbk;
注解:該表是一個用戶信息表,可以將用戶信息歸納為兩個部分:一個部分是name(姓名),包含firstname(姓)和lastname(名);另一部分是contact(聯系方式),包括了address(地址)、zipcode(郵編)和tel(電話)等信息。在創建實體類時,可以將name和contact分別封裝到2個獨立的類中,這樣就提高了系統的復用性和靈活性。也就是說,需要使用Component映射,將其他的實體類映射在一起。
建立一個Name.java類:
package collect.component; import java.io.Serializable; public class Name implements Serializable { private String firstname; private String lastname; public String getFirstname() { return firstname; } public void setFirstname(String firstname) { this.firstname = firstname; } public String getLastname() { return lastname; } public void setLastname(String lastname) { this.lastname = lastname; } }
建立聯系方式相關的類Contact.java:
package collect.component; import java.io.Serializable; public class Contact implements Serializable{ private String address; private String zipcodes; private String tel; public Contact() {} public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getZipcodes() { return zipcodes; } public void setZipcodes(String zipcodes) { this.zipcodes = zipcodes; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } }
最后建立數據庫表對應的實體類Cuser.java:
package collect.component; public class Cuser { private Integer id; private Integer age; private Name name; private Contact contact; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } public Contact getContact() { return contact; } public void setContact(Contact contact) { this.contact = contact; } }
建立映射文件Cuser.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="collect.component.Cuser" table="c_user" catalog="ssh"> <id name="id" type="integer"> <column name="id" /> <generator class="native" /> </id> <property name="age" type="integer"> <column name="age" /> </property> <component name="name" class="collect.component.Name"> <property name="firstname" type="string"> <column name="firstname" length="50" /> </property> <property name="lastname" type="string"> <column name="lastname" length="50" /> </property> </component> <component name="contact" class="collect.component.Contact"> <property name="address" type="string"> <column name="address" length="200" /> </property> <property name="zipcodes" type="string"> <column name="zipcode" length="10" /> </property> <property name="tel" type="string"> <column name="tel" length="20" /> </property> </component> </class> </hibernate-mapping>
注解:映射文件使用component元素將Name類、Contact類同數據庫表c_user聯系起來。
將該映射文件加入到Hibernate的配置文件中,建立一個測試類Test.java:
package collect.component; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { public static void main(String[] args) { // Configuration管理Hibernate配置 Configuration config = new Configuration().configure(); // 根據Configuration建立 SessionFactory // SessionFactory用來建立Session SessionFactory sessionFactory = config.buildSessionFactory(); // 創建實例 Name name=new Name(); name.setFirstname("閆"); name.setLastname("術卓"); Contact contact = new Contact(); contact.setAddress("北京"); contact.setTel("42689334"); contact.setZipcodes("100085"); Cuser user= new Cuser(); user.setAge(33); user.setName(name); user.setContact(contact); // 定義主鍵變量 Integer pid; // 添加數據 Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); // 創建主鍵變量 pid = (Integer) session.save(user); tx.commit(); } catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; } finally { session.close(); } // 關閉sessionFactory sessionFactory.close(); } }
運行結果:
控制臺:
20:47:57,366 DEBUG SQL:346 - insert intossh.c_user (age, firstname, lastname, address, zipcode, tel) values (?, ?, ?,?, ?, ?)
數據庫:
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!