程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> hibernate3學習筆記(十二)|Component

hibernate3學習筆記(十二)|Component

編輯:關於JAVA

在MySQL中新建數據表User:

1.CREATE TABLE user (
2. id INT(11) NOT NULL auto_increment PRIMARY KEY,
3. name VARCHAR(100) NOT NULL default '',
4. age INT,
5. email VARCHAR(100) NOT NULL
6.);

按照最基本的映射策略的話,應該把User類設計成:

1.package onlyfun.caterpillar;
2.
3.public class User {
4. private Integer id;
5. private String name;
6. private int age;
7. private String email;
8. 9. ........
10.}

然而由於業務上的需要,要把email字段升格成MailAddress類,例如:

1.package com.hb3.pack_05.model;
2.
3.public class MailAddress {
4. 5. private String email;
6. 7. public MailAddress() {
8. }
9.
10. public String getEmail() {
11. return email;
12. }
13. 14. public void setEmail(String email) {
15. this.email = email;
16. }
17.
18. public void sendMail() {
19. System.out.println("Send mail to " + email);
20. }
21.}

User.java

1.package com.hb3.pack_05.model;
2.
3.public class User {
4. 5. private Integer id;
6. private String name;
7. private Integer age;
8. private MailAddress mailAddress;
9. 10. public User() {
11. }
12. 13. public Integer getId() {
14. return id;
15. }
16. 17. public void setId(Integer id) {
18. this.id = id;
19. }
20. 21. public String getName() {
22. return name;
23. }
24. 25. public void setName(String name) {
26. this.name = name;
27. }
28.
29. public Integer getAge() {
30. return age;
31. }
32. 33. public void setAge(Integer age) {
34. this.age = age;
35. }
36. 37. public MailAddress getMailAddress() {
38. return mailAddress;
39. }
40. 41. public void setMailAddress(MailAddress mailAddress) {
42. this.mailAddress = mailAddress;
43. }
44.}

User.hbm.xml文件:

1.<?xml version="1.0" encoding="UTF-8"?>
2. 3.<!DOCTYPE hibernate-mapping PUBLIC
4. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
5. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 6. 7.<hibernate-mapping> 8. 9. <class name="com.hb3.pack_05.model.User" table="user"> 10.
11. <id name="id" column="id" type="java.lang.Integer">
12. <generator class="native"/>
13. </id> 14.
15. <property name="name" column="name" type="java.lang.String"/> 16.
17. <property name="age" column="age" type="java.lang.Integer"/> 18.
19. <component name="mailAddress" class="com.hb3.pack_05.model.MailAddress">
20. <property name="email"
21. column="email"
22. type="java.lang.String"
23. not-null="true"/>
24. </component>
25. </class>
26.</hibernate-mapping>

修改hibernate.cfg.xml文件:

......
<mapping resource="com/hb3/pack_05/model/User.hbm.xml" />
......

測試代碼如下:

1.package com.hb3.pack_05;
2.
3.import org.hibernate.Session;
4.import org.hibernate.SessionFactory;
5.import org.hibernate.Transaction;
6.import org.hibernate.cfg.Configuration;
7. 8.import com.hb3.pack_05.model.MailAddress;
9.import com.hb3.pack_05.model.User;
10. 11.public class BusinessService {
12. 13. public static void main(String[] args) {
14.
15. Configuration config = new Configuration().configure();
16. SessionFactory sessionFactory = config.buildSessionFactory();
17. Session session = sessionFactory.openSession();
18. 19. MailAddress mailAddress = new MailAddress();
20. mailAddress.setEmail("[email protected]");
21.
22. User user = new User();
23. user.setName("shenbin");
24. user.setAge(new Integer(28));
25. user.setMailAddress(mailAddress);
26. 27. Transaction tx = session.beginTransaction();
28. session.save(user);
29. session.flush();
30. tx.commit();
31. 32. session.close();
33.
34. session = sessionFactory.openSession();
35.
36. user = (User) session.load(User.class, new Integer(1));
37. System.out.println(user.getAge() + "\t" +
38. user.getName() + "\t" +
39. user.getMailAddress().getEmail());
40. user.getMailAddress().sendMail();
41.
42.
43. session.close();
44. sessionFactory.close();
45. }
46.}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved