Hibernate实战——Embedded注解

缺乏、安全感 2021-07-24 17:31 546阅读 0赞

一 配置文件

  1. <?xml version="1.0" encoding="GBK"?>
  2. <!-- 指定Hibernate配置文件的DTD信息 -->
  3. <!DOCTYPE hibernate-configuration PUBLIC
  4. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
  5. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
  6. <!-- hibernate-configuration是配置文件的根元素 -->
  7. <hibernate-configuration>
  8. <session-factory>
  9. <!-- 指定连接数据库所用的驱动 -->
  10. <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  11. <!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
  12. <property name="connection.url">jdbc:mysql://localhost/hibernate</property>
  13. <!-- 指定连接数据库的用户名 -->
  14. <property name="connection.username">root</property>
  15. <!-- 指定连接数据库的密码 -->
  16. <property name="connection.password">32147</property>
  17. <!-- 指定连接池里最大连接数 -->
  18. <property name="hibernate.c3p0.max_size">20</property>
  19. <!-- 指定连接池里最小连接数 -->
  20. <property name="hibernate.c3p0.min_size">1</property>
  21. <!-- 指定连接池里连接的超时时长 -->
  22. <property name="hibernate.c3p0.timeout">5000</property>
  23. <!-- 指定连接池里最大缓存多少个Statement对象 -->
  24. <property name="hibernate.c3p0.max_statements">100</property>
  25. <property name="hibernate.c3p0.idle_test_period">3000</property>
  26. <property name="hibernate.c3p0.acquire_increment">2</property>
  27. <property name="hibernate.c3p0.validate">true</property>
  28. <!-- 指定数据库方言 -->
  29. <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  30. <!-- 根据需要自动创建数据库 -->
  31. <property name="hbm2ddl.auto">update</property>
  32. <!-- 显示Hibernate持久化操作所生成的SQL -->
  33. <property name="show_sql">true</property>
  34. <!-- 将SQL脚本进行格式化后再输出 -->
  35. <property name="hibernate.format_sql">true</property>
  36. <!-- 罗列所有持久化类的类名 -->
  37. <mapping class="org.crazyit.app.domain.Person"/>
  38. </session-factory>
  39. </hibernate-configuration>

二 PO

1 Name

  1. package org.crazyit.app.domain;
  2. import org.hibernate.annotations.Parent;
  3. public class Name
  4. {
  5. // 定义first成员变量
  6. private String first;
  7. // 定义last成员变量
  8. private String last;
  9. // 引用拥有该Name的Person对象
  10. @Parent
  11. private Person owner;
  12. // 无参数的构造器
  13. public Name()
  14. {
  15. }
  16. // 初始化全部成员变量的构造器
  17. public Name(String first , String last)
  18. {
  19. this.first = first;
  20. this.last = last;
  21. }
  22. // first的setter和getter方法
  23. public void setFirst(String first)
  24. {
  25. this.first = first;
  26. }
  27. public String getFirst()
  28. {
  29. return this.first;
  30. }
  31. // last的setter和getter方法
  32. public void setLast(String last)
  33. {
  34. this.last = last;
  35. }
  36. public String getLast()
  37. {
  38. return this.last;
  39. }
  40. // owner的setter和getter方法
  41. public void setOwner(Person owner)
  42. {
  43. this.owner = owner;
  44. }
  45. public Person getOwner()
  46. {
  47. return this.owner;
  48. }
  49. }

2 Person

  1. package org.crazyit.app.domain;
  2. import javax.persistence.*;
  3. @Entity
  4. @Table(name="person_inf")
  5. public class Person
  6. {
  7. @Id @Column(name="person_id")
  8. @GeneratedValue(strategy=GenerationType.IDENTITY)
  9. private Integer id;
  10. private int age;
  11. // 组件属性name
  12. @Embedded
  13. @AttributeOverrides({
  14. @AttributeOverride(name="first", column = @Column(name="person_firstname")),
  15. @AttributeOverride(name="last", column = @Column(name="person_lastname"))
  16. })
  17. private Name name;
  18. // id的setter和getter方法
  19. public void setId(Integer id)
  20. {
  21. this.id = id;
  22. }
  23. public Integer getId()
  24. {
  25. return this.id;
  26. }
  27. // age的setter和getter方法
  28. public void setAge(int age)
  29. {
  30. this.age = age;
  31. }
  32. public int getAge()
  33. {
  34. return this.age;
  35. }
  36. // name的setter和getter方法
  37. public void setName(Name name)
  38. {
  39. this.name = name;
  40. }
  41. public Name getName()
  42. {
  43. return this.name;
  44. }
  45. }

三 测试

1 工具类

  1. package lee;
  2. import org.hibernate.*;
  3. import org.hibernate.cfg.*;
  4. import org.hibernate.service.*;
  5. import org.hibernate.boot.registry.*;
  6. public class HibernateUtil
  7. {
  8. public static final SessionFactory sessionFactory;
  9. static
  10. {
  11. try
  12. {
  13. // 使用默认的hibernate.cfg.xml配置文件创建Configuration实例
  14. Configuration cfg = new Configuration()
  15. .configure();
  16. // 以Configuration实例来创建SessionFactory实例
  17. ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
  18. .applySettings(cfg.getProperties()).build();
  19. sessionFactory = cfg.buildSessionFactory(serviceRegistry);
  20. }
  21. catch (Throwable ex)
  22. {
  23. System.err.println("Initial SessionFactory creation failed." + ex);
  24. throw new ExceptionInInitializerError(ex);
  25. }
  26. }
  27. // ThreadLocal可以隔离多个线程的数据共享,因此不再需要对线程同步
  28. public static final ThreadLocal<Session> session
  29. = new ThreadLocal<Session>();
  30. public static Session currentSession()
  31. throws HibernateException
  32. {
  33. Session s = session.get();
  34. // 如果该线程还没有Session,则创建一个新的Session
  35. if (s == null)
  36. {
  37. s = sessionFactory.openSession();
  38. // 将获得的Session变量存储在ThreadLocal变量session里
  39. session.set(s);
  40. }
  41. return s;
  42. }
  43. public static void closeSession()
  44. throws HibernateException
  45. {
  46. Session s = session.get();
  47. if (s != null)
  48. s.close();
  49. session.set(null);
  50. }
  51. }

2 测试类

  1. package lee;
  2. import org.hibernate.Transaction;
  3. import org.hibernate.Session;
  4. import java.util.Date;
  5. import java.util.Set;
  6. import java.util.HashSet;
  7. import org.crazyit.app.domain.*;
  8. public class PersonManager
  9. {
  10. public static void main(String[] args)
  11. {
  12. PersonManager mgr = new PersonManager();
  13. mgr.createAndStorePerson();
  14. HibernateUtil.sessionFactory.close();
  15. }
  16. private void createAndStorePerson()
  17. {
  18. Session session = HibernateUtil.currentSession();
  19. Transaction tx = session.beginTransaction();
  20. // 创建Person对象
  21. Person person = new Person();
  22. // 为Person对象设置属性
  23. person.setAge(29);
  24. person.setName(new Name("crazyit.org" , "疯狂Java联盟"));
  25. session.save(person);
  26. tx.commit();
  27. HibernateUtil.closeSession();
  28. }
  29. }

四 测试结果

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2NoZW5ncWl1bWluZw_size_16_color_FFFFFF_t_70

发表评论

表情:
评论列表 (有 0 条评论,546人围观)

还没有评论,来说两句吧...

相关阅读