利用Hibernate注解生成表

时间:2021-04-07 12:00:33

 转自:http://blog.csdn.net/madison__/article/details/55677099

 

Hibernate4注释

@Entity(name = "tbl_user")  将一个类声明为一个实体bean(即一个持久化POJO类)
@Id注解则声明了该实体bean的标识属性,必填属性
@GenericGeneratorhibernate在JPA的基础上进行了扩展,可以用一下方式引入hibernate独有的主键生成策略。
@Column   name解决属性名和字段名不对应 length限制字符串长度 unique , nullable , precision 数据长度, scale  无小数

hibernate.cfg.xml

 

[html] view plain copy
  1. <?xml version='1.0' encoding='UTF-8'?>    
  2. <!DOCTYPE hibernate-configuration PUBLIC    
  3.           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"    
  4.           "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">    
  5. <!-- Generated by MyEclipse Hibernate Tools.                   -->    
  6. <hibernate-configuration>    
  7.     
  8. <session-factory>    
  9.     <property name="myeclipse.connection.profile">mysql</property>    
  10.     <property name="connection.url">    
  11.         jdbc:mysql://localhost:3306/forums    
  12.     </property>    
  13.     <property name="connection.username">root</property>    
  14.     <property name="connection.password">root</property>    
  15.     <property name="connection.driver_class">    
  16.         com.mysql.jdbc.Driver    
  17.     </property>    
  18.     <property name="hibernate.current_session_context_class">thread</property>    
  19.     <property name="dialect">    
  20.         org.hibernate.dialect.MySQLDialect    
  21.     </property>    
  22.     <property name="show_sql">true</property>    
  23.     <property name="hibernate.current_session_context_class">thread</property>
  24.     <!--设置自动生成表,如果表不存在就自动生成,如果表存在就更新表结构-->
  25.     <property name="hibernate.hbm2ddl.auto">update</property>    
  26.     
  27.     <!-- 连接池 配置 c3p0-->    
  28.     <property name="hibernate.connection.provider_class">org.hibernate.c3p0.internal.C3P0ConnectionProvider</property>    
  29.     <!-- 在连接池中可用数据连接的最大数目 -->    
  30.     <property name="hibernate.c3p0.max_size">20</property>    
  31.     <!-- 在连接池中可用数据连接的最小数目 -->    
  32.     <property name="hibernate.c3p0.min_size">5</property>    
  33.     <!-- 设定数据库连接的过期时间,以秒为单位,如果连接池中的某个数据库连接处于空闲状态的时间超过了timeout时间,就会从连接池中清除-->    
  34.     <property name="hibernate.c3p0.timeout">120</property>    
  35.     <!-- 每3000秒查询所有连接池的空闲连接以秒为单位 -->    
  36.     <property name="hibernate.c3p0.idle_test_period">3000</property>    
  37.     
  38.     <!--当连接池里面的连接用完的时候,C3P0一下获取的新的连接数     
  39.        <property name="hibernate.c3p0.acquire_increment">2</property>      
  40.      每次都验证连接是否可用     
  41.        <property name="hibernate.c3p0.validate">true</property> -->    
  42.         
  43.         <!-- 映射文件引入 -->    
  44.      <mapping class="csdn.forum.model.TblUser" />    
  45.      <mapping class="csdn.forum.model.TblTopic" />     
  46.      <mapping class="csdn.forum.model.TblBoard" />    
  47.      <mapping class="csdn.forum.model.TblReply" />     
  48. </session-factory>    
  49.     
  50. </hibernate-configuration>    



解析文件hibernate.hbm.xml的类HibernateSessionFactory.java
[java] view plain copy
  1. package csdn.forum.util;    
  2.     
  3. import org.hibernate.Session;    
  4. import org.hibernate.SessionFactory;    
  5. import org.hibernate.boot.registry.StandardServiceRegistryBuilder;    
  6. import org.hibernate.cfg.Configuration;    
  7. import org.hibernate.service.ServiceRegistry;    
  8.     
  9. public class HibernateSessionFactory {    
  10.     static Configuration cfg;    
  11.     static SessionFactory sessionFactory;    
  12.     static ServiceRegistry serviceRegistry;    
  13.     static {    
  14.         cfg = new Configuration().configure();// 默认找文件hibernate.hbm.xml    
  15.         // 创建服务注册对象    
  16.         serviceRegistry = new StandardServiceRegistryBuilder().applySettings(    
  17.                 cfg.getProperties()).build();    
  18.         // 创建Factory工厂    
  19.         sessionFactory = cfg.buildSessionFactory(serviceRegistry);    
  20.     }    
  21.     
  22.     // 获取session对象    
  23.     public static Session getSession() {    
  24.         // 打开session    
  25.         return sessionFactory.getCurrentSession();    
  26.     }    
  27. }    


fetch在Hibernate里用时默认值:FetchType.LAZY,它要求程序运行时延迟加载所有的集合和实体。
fetch设置为FetchType.EAGER,它提示程序在首次访问数据时应马上加载所有的集合和实体mappedBy默认值:如果关系是单向的,则该关联提供程序确定拥有该关系的字段。如果关系是双向的,则将关联相反(非拥有)方上的mappedBy元素设置为拥有此关系的字段或属性的名称

@BatchSize(size=10) 对查询抓取的优化方案,通过指定一个主键或外键列表,Hibernate使用单条SELECT语句获取一批对象实例或集合。

TblBoard.java

[java] view plain copy
  1. package csdn.forum.model;    
  2.     
  3. import java.util.ArrayList;    
  4. import java.util.List;    
  5. import javax.persistence.CascadeType;    
  6. import javax.persistence.Column;    
  7. import javax.persistence.Entity;    
  8. import javax.persistence.FetchType;    
  9. import javax.persistence.GeneratedValue;    
  10. import javax.persistence.Id;    
  11. import javax.persistence.JoinColumn;    
  12. import javax.persistence.ManyToOne;    
  13. import javax.persistence.OneToMany;    
  14.     
  15. import org.hibernate.annotations.GenericGenerator;    
  16. @Entity(name = "tbl_board")    
  17. public class TblBoard {    
  18.     private String id;    
  19.     private String name;    
  20.         
  21.     private TblBoard parentBoard;    
  22.     private List<TblBoard> board = new ArrayList<TblBoard>();    
  23.     private List<TblTopic> topics=new ArrayList<TblTopic>();    
  24.     // @id必填    
  25.     @GenericGenerator(name = "generator", strategy = "uuid.hex")    
  26.     // hibernate独有的16进制算法    
  27.     @Id    
  28.     @GeneratedValue(generator = "generator")    
  29.     @Column(name = "id", unique = true, nullable = false, length = 32)    
  30.     public String getId() {    
  31.         return id;    
  32.     }    
  33.     
  34.     public void setId(String id) {    
  35.         this.id = id;    
  36.     }    
  37.     @Column(name ="name")    
  38.     public String getName() {    
  39.         return name;    
  40.     }    
  41.     
  42.     public void setName(String name) {    
  43.         this.name = name;    
  44.     }    
  45.     @ManyToOne(cascade = {CascadeType.ALL})    
  46.     @JoinColumn(name = "parent_id")    
  47.     public TblBoard getParentBoard() {    
  48.         return parentBoard;    
  49.     }    
  50.     
  51.     public void setParentBoard(TblBoard parentBoard) {    
  52.         this.parentBoard = parentBoard;    
  53.     }    
  54.         
  55.     @OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY,mappedBy = "parentBoard")//在1这方加入@OneToMany(cascade = {CascadeType.ALL},mappedBy = "user") ,只调用session.sa//ve(user);    
  56. //在多这方加入@ManyToOne(cascade = {CascadeType.ALL}) @JoinColumn(name = "user_id"),只调用s//ession.save(topic);    
  57.     public List<TblBoard> getBoard() {    
  58.         return board;    
  59.     }    
  60.     
  61.     public void setBoard(List<TblBoard> board) {    
  62.         this.board = board;    
  63.     }    
  64.     @OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY,mappedBy = "board")    
  65.     public List<TblTopic> getTopics() {    
  66.         return topics;    
  67.     }    
  68.     
  69.     public void setTopics(List<TblTopic> topics) {    
  70.         this.topics = topics;    
  71.     }    
  72.     
  73. }    



利用Hibernate注解生成表

TblReply.Java


 

[java] view plain copy
  1. package csdn.forum.model;    
  2.     
  3. import java.util.Date;    
  4.     
  5. import javax.persistence.CascadeType;    
  6. import javax.persistence.Column;    
  7. import javax.persistence.Entity;    
  8. import javax.persistence.GeneratedValue;    
  9. import javax.persistence.Id;    
  10. import javax.persistence.JoinColumn;    
  11. import javax.persistence.ManyToOne;    
  12. import javax.persistence.Temporal;    
  13. import javax.persistence.TemporalType;    
  14.     
  15. import org.hibernate.annotations.GenericGenerator;    
  16. @Entity(name = "tbl_reply")     
  17. public class TblReply{    
  18.         
  19.     private String id;    
  20.     private String title;    
  21.     private String content;    
  22.     private Date publishTime;    
  23.     private Date modifyTime;    
  24.     private TblUser user;    
  25.     private TblTopic tblTopic;    
  26.         
  27.     @GenericGenerator(name = "generator", strategy = "uuid.hex")    
  28.     @Id    
  29.     @GeneratedValue(generator = "generator")    
  30.     @Column(name = "id", unique = true, nullable = false, length = 32)    
  31.     public String getId() {    
  32.         return id;    
  33.     }    
  34.     public void setId(String id) {    
  35.         this.id = id;    
  36.     }    
  37.     @Column(name = "title", length = 32)    
  38.     public String getTitle() {    
  39.         return title;    
  40.     }    
  41.     public void setTitle(String title) {    
  42.         this.title = title;    
  43.     }    
  44.     @Column(name = "content", length = 2000)    
  45.     public String getContent() {    
  46.         return content;    
  47.     }    
  48.     public void setContent(String content) {    
  49.         this.content = content;    
  50.     }    
  51.     @Temporal(TemporalType.DATE)    
  52.     @Column(name = "publishTime")    
  53.     public Date getPublishTime() {    
  54.         return publishTime;    
  55.     }    
  56.     public void setPublishTime(Date publishTime) {    
  57.         this.publishTime = publishTime;    
  58.     }    
  59.     @Temporal(TemporalType.DATE)    
  60.     @Column(name = "modifyTime")    
  61.     public Date getModifyTime() {    
  62.         return modifyTime;    
  63.     }    
  64.     public void setModifyTime(Date modifyTime) {    
  65.         this.modifyTime = modifyTime;    
  66.     }    
  67.     @ManyToOne(cascade = {CascadeType.ALL})    
  68.     @JoinColumn(name = "user_id")    
  69.     public TblUser getUser() {    
  70.         return user;    
  71.     }    
  72.     public void setUser(TblUser user) {    
  73.         this.user = user;    
  74.     }    
  75.     @ManyToOne(cascade = {CascadeType.ALL})    
  76.     @JoinColumn(name = "topic_id")    
  77.     public TblTopic getTblTopic() {    
  78.         return tblTopic;    
  79.     }    
  80.     public void setTblTopic(TblTopic tblTopic) {    
  81.         this.tblTopic = tblTopic;    
  82.     }    
  83.         
  84. }    


利用Hibernate注解生成表


TblTopic.java

 

[java] view plain copy
  1. package csdn.forum.model;    
  2.     
  3. import java.util.ArrayList;    
  4. import java.util.Date;    
  5. import java.util.List;    
  6.     
  7. import javax.persistence.CascadeType;    
  8. import javax.persistence.Column;    
  9. import javax.persistence.Entity;    
  10. import javax.persistence.FetchType;    
  11. import javax.persistence.GeneratedValue;    
  12. import javax.persistence.Id;    
  13. import javax.persistence.JoinColumn;    
  14. import javax.persistence.ManyToOne;    
  15. import javax.persistence.OneToMany;    
  16. import javax.persistence.Temporal;    
  17. import javax.persistence.TemporalType;    
  18.     
  19. import org.hibernate.annotations.BatchSize;    
  20. import org.hibernate.annotations.GenericGenerator;    
  21.     
  22. @Entity(name = "tbl_topic")     
  23. public class TblTopic  {    
  24.     
  25.     private String id;    
  26.     private String title;    
  27.     private String content;    
  28.     private Date pubishTime;    
  29.     private Date modifyTime;    
  30.     private TblUser user;    
  31.     private TblBoard board;    
  32.     private List<TblReply> replys=new ArrayList<TblReply>();    
  33.         
  34.     @GenericGenerator(name = "generator", strategy = "uuid.hex")    
  35.     @Id    
  36.     @GeneratedValue(generator = "generator")    
  37.     @Column(name = "id", unique = true, nullable = false, length = 32)    
  38.     public String getId() {    
  39.         return id;    
  40.     }    
  41.     public void setId(String id) {    
  42.         this.id = id;    
  43.     }    
  44.     @Column(name = "title", length = 32)    
  45.     public String getTitle() {    
  46.         return title;    
  47.     }    
  48.     public void setTitle(String title) {    
  49.         this.title = title;    
  50.     }    
  51.     @Column(name = "content", length = 2000)    
  52.     public String getContent() {    
  53.         return content;    
  54.     }    
  55.     public void setContent(String content) {    
  56.         this.content = content;    
  57.     }    
  58.     @Temporal(TemporalType.DATE)    
  59.     @Column(name = "publishTime")    
  60.     public Date getPubishTime() {    
  61.         return pubishTime;    
  62.     }    
  63.     public void setPubishTime(Date pubishTime) {    
  64.         this.pubishTime = pubishTime;    
  65.     }    
  66.     @Temporal(TemporalType.DATE)    
  67.     @Column(name = "modifyTime")    
  68.     public Date getModifyTime() {    
  69.         return modifyTime;    
  70.     }    
  71.     public void setModifyTime(Date modifyTime) {    
  72.         this.modifyTime = modifyTime;    
  73.     }    
  74. //  多这方映射user_id    
  75.     @ManyToOne(cascade = {CascadeType.ALL})    
  76.     @JoinColumn(name = "user_id")    
  77.     public TblUser getUser() {    
  78.         return user;    
  79.     }    
  80.     public void setUser(TblUser user) {    
  81.         this.user = user;    
  82.     }    
  83.     @OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY,mappedBy = "tblTopic")    
  84.     public List<TblReply> getReplys() {    
  85.         return replys;    
  86.     }    
  87.     public void setReplys(List<TblReply> replys) {    
  88.         this.replys = replys;    
  89.     }    
  90.     @ManyToOne(cascade = {CascadeType.ALL})    
  91.     @JoinColumn(name = "board_id")    
  92.     public TblBoard getBoard() {    
  93.         return board;    
  94.     }    
  95.     public void setBoard(TblBoard board) {    
  96.         this.board = board;    
  97.     }       
  98. }    




利用Hibernate注解生成表 

TblUser.java

 

[java] view plain copy
  1. package csdn.forum.model;    
  2.     
  3. import java.util.ArrayList;    
  4. import java.util.Date;    
  5. import java.util.List;    
  6.     
  7. import javax.persistence.CascadeType;    
  8. import javax.persistence.Column;    
  9. import javax.persistence.Entity;    
  10. import javax.persistence.FetchType;    
  11. import javax.persistence.GeneratedValue;    
  12. import javax.persistence.Id;    
  13. import javax.persistence.OneToMany;    
  14. import javax.persistence.Temporal;    
  15. import javax.persistence.TemporalType;    
  16.     
  17. import org.hibernate.annotations.GenericGenerator;    
  18.     
  19. @Entity(name="tbl_user")    
  20. public class TblUser {    
  21.     /**   
  22.      *    
  23.      */    
  24.     private static final long serialVersionUID = 1L;    
  25.     private String id;    
  26.     private String uname;    
  27.     private String upass;    
  28.     private String head;    
  29.     private Date regTime;    
  30.     private boolean gender;    
  31.         
  32.         
  33.     private  List<TblTopic>  topic=new ArrayList<TblTopic>();    
  34.     private  List<TblReply>  replies=new ArrayList<TblReply>();    
  35.     
  36.     //@id必填    
  37.     @GenericGenerator(name = "generator", strategy = "uuid.hex")//hibernate独有的16进制算法    
  38.     @Id    
  39.     @GeneratedValue(generator = "generator")    
  40.     @Column(name = "id", unique = true, nullable = false, length = 32)    
  41.     public String getId() {    
  42.         return id;    
  43.     }    
  44.     
  45.     public void setId(String id) {    
  46.         this.id = id;    
  47.     }    
  48.    @Column (name="uname",length=20)    
  49.     public String getUname() {    
  50.         return uname;    
  51.     }    
  52.     
  53.     public void setUname(String uname) {    
  54.         this.uname = uname;    
  55.     }    
  56.       @Column (name="upass",length=20)    
  57.     public String getUpass() {    
  58.         return upass;    
  59.     }    
  60.     
  61.     public void setUpass(String upass) {    
  62.         this.upass = upass;    
  63.     }    
  64.       @Column (name="head")    
  65.     public String getHead() {    
  66.         return head;    
  67.     }    
  68.     
  69.     public void setHead(String head) {    
  70.         this.head = head;    
  71.     }    
  72.         
  73.      @Temporal(TemporalType.DATE)    
  74.      @Column(name = "regTime")    
  75.     public Date getRegTime() {    
  76.         return regTime;    
  77.     }     
  78.     public void setRegTime(Date regTime) {    
  79.         this.regTime = regTime;    
  80.     }    
  81.     
  82.     @Column(name="gender",length=20)    
  83.     public boolean isGender() {    
  84.         return gender;    
  85.     }    
  86.     
  87.     public void setGender(boolean gender) {    
  88.         this.gender = gender;    
  89.     }    
  90.     //现在是一的一方直接通过它映射user,user是多的这一方的属性    
  91.     // @OneToMany(mappedBy="user")    
  92.     @OneToMany(cascade = {CascadeType.ALL},fetch = FetchType.LAZY,mappedBy = "user")    
  93.     public List<TblTopic> getTopic() {    
  94.         return topic;    
  95.     }    
  96.     
  97.     public void setTopic(List<TblTopic> topic) {    
  98.         this.topic = topic;    
  99.     }    
  100.     
  101.     @OneToMany(cascade={CascadeType.ALL},fetch=FetchType.LAZY,mappedBy="user")    
  102.     public List<TblReply> getReplies() {    
  103.         return replies;    
  104.     }    
  105.     
  106.     public void setReplies(List<TblReply> replies) {    
  107.         this.replies = replies;    
  108.     }    
  109.     
  110.         
  111.     
  112. }    



利用Hibernate注解生成表


测试类TestUser.java

 

[java] view plain copy
  1. package csdn.forum.test;    
  2.     
  3. import java.util.Date;    
  4. import java.util.List;    
  5.     
  6. import org.hibernate.Query;    
  7. import org.hibernate.Session;    
  8. import org.junit.Test;    
  9.     
  10. import csdn.forum.model.TblBoard;    
  11. import csdn.forum.model.TblReply;    
  12. import csdn.forum.model.TblTopic;    
  13. import csdn.forum.model.TblUser;    
  14. import csdn.forum.util.HibernateSessionFactory;    
  15.     
  16. public class TestUser {    
  17.     @Test    
  18.     public void save() {    
  19.         Session session = HibernateSessionFactory.getSession();    
  20.         session.getTransaction().begin();    
  21.         /*   
  22.          * //1.保存java板块包含jsp,ajax,struts2,hiberante子版块 TblBoard java =new   
  23.          * TblBoard(); java.setName("java");   
  24.          *    
  25.          * TblBoard jsp =new TblBoard(); jsp.setName("jsp"); TblBoard struts2   
  26.          * =new TblBoard(); struts2.setName("struts2"); TblBoard hiberante =new   
  27.          * TblBoard(); hiberante.setName("hiberante"); //相互持有   
  28.          * java.getBoard().add(jsp); java.getBoard().add(struts2);   
  29.          * java.getBoard().add(hiberante); jsp.setParentBoard(java);   
  30.          * struts2.setParentBoard(java); hiberante.setParentBoard(java);   
  31.          *    
  32.          *    
  33.          * session.save(java); session.getTransaction().commit();   
  34.          */    
  35.     
  36.         // 2.利用csdn用户身份,在struts2板块下发表帖子    
  37.         /*   
  38.          * TblUser user=new TblUser(); user.setUname("csdn"); TblBoard   
  39.          * struts2=(TblBoard   
  40.          * )session.get(TblBoard.class,"2c96c05e44da48600144da4861f60002");   
  41.          * TblTopic topic=new TblTopic(); topic.setTitle("struts  TblBoard");   
  42.          * topic.setUser(user); topic.setBoard(struts2); topic.setPubishTime(new   
  43.          * Date()); user.getTopic().add(topic); session.save(topic);   
  44.          * session.getTransaction().commit();   
  45.          */    
  46.     
  47.         /*   
  48.          * //3.利用admin用户身份,给帖子回帖 TblUser user=new TblUser();   
  49.          * user.setUname("admin"); TblTopic topic1=(TblTopic)   
  50.          * session.get(TblTopic.class,"2c96c05e44da44660144da4469390000");   
  51.          * TblReply reply=new TblReply(); reply.setTitle("reply title");   
  52.          * reply.setUser(user); user.getReplies().add(reply);   
  53.          * reply.setTblTopic(topic1); topic1.getReplys().add(reply);   
  54.          * session.getTransaction().commit();   
  55.          */    
  56.     
  57.         // 4.利用admin用户身份,修改回帖内容    
  58.         /*   
  59.          * Query query=session.createQuery(   
  60.          * "from csdn.forum.model.TblReply  r where r.user.uname='admin'");   
  61.          * TblReply reply=(TblReply) query.list().get(0);   
  62.          * reply.setTitle("update"); session.save(reply);   
  63.          * session.getTransaction().commit();   
  64.          */    
  65.     
  66.         // 5.利用admin用户身份,查看回帖//根据帖子查看回帖,查看下面的所有回帖    
  67.     
  68.         /*   
  69.          * Query query=session.createQuery(   
  70.          * "from csdn.forum.model.TblReply t where t.user.uname='admin'");   
  71.          * List<TblReply> lisst=query.list();   
  72.          *    
  73.          * for(TblReply tabl:lisst){ System.out.println("1111111111111");   
  74.          * System.out.println(tabl.getContent()); }   
  75.          * session.getTransaction().commit();   
  76.          */    
  77.     
  78.         // 6.利用csdn用户身份,删除回帖,条件限制为有回帖存在不允许删除    
  79.         /*   
  80.          * Query query1=session.createQuery(   
  81.          * "from csdn.forum.model.TblTopic  t where t.user.uname='csdn'");   
  82.          * TblTopic topic1=(TblTopic) query1.list().get(0);   
  83.          * if(topic1.getReplys().size()==0){ session.delete(topic1); }else{   
  84.          * System.out.println("提示不能删除"); }   
  85.          *    
  86.          * session.getTransaction().commit();   
  87.          */    
  88.     
  89.         // 7.统计struts2板块下的发帖数    
  90.         // Query    
  91.         // query2=session.createQuery("select count(*) from csdn.forum.model.TblTopic  t where t.board.name='struts2'");    
  92.     
  93.         /*   
  94.          * Query query2 = session.createQuery(   
  95.          * " from csdn.forum.model.TblTopic t where t.board.name='struts2'");   
  96.          *    
  97.          * System.out.println(query2.list().size()+"---"); //   
  98.          * System.out.println(query2.list().get(0)); //   
  99.          * System.out.println(query2.list().get(1)); //   
  100.          * System.out.println(query2.list().get(2));   
  101.          * session.getTransaction().commit();   
  102.          */    
  103.         // 8.查询struts2板块下所有帖子按时间倒序排列    
  104.         /*   
  105.          * Query query=session.createQuery(   
  106.          * "from csdn.forum.model.TblTopic t where t.board.name='struts2' order by t.pubishTime  desc"   
  107.          * ); //System.out.println(query); for(int   
  108.          * i=0;i<query.list().size();i++){ TblTopic topic=(TblTopic)   
  109.          * query.list().get(i); System.out.println(topic.getTitle()); }   
  110.          */    
  111.     
  112.         // 9.查询帖子下所有回帖,按时间倒序排列    
  113.         /*   
  114.          * Query query=session.createQuery(   
  115.          * "from csdn.forum.model.TblReply t where t.tblTopic.title='struts  TblBoard' order by t.publishTime  desc"   
  116.          * ); //System.out.println(query); for(int   
  117.          * i=0;i<query.list().size();i++){ TblReply topic=(TblReply)   
  118.          * query.list().get(i); System.out.println(topic.getTitle()); }   
  119.          */    
  120.             
  121.             
  122.         /*   
  123.          *    
  124.          * 查询父亲java板块   
  125.          * TblBoard board = (TblBoard) session.get(TblBoard.class,   
  126.                 "2c96c05e44da48600144da4861dd0000");   
  127.         System.out.println(board);   
  128.         session.getTransaction().commit();*/    
  129.             
  130.             
  131.             
  132.         // 10.查出最后发表帖子标题    
  133.          Query query = session    
  134.          .createQuery("from csdn.forum.model.TblTopic t  order by t.pubishTime  asc");    
  135.          // System.out.println(query);    
  136.          TblTopic topic=(TblTopic) query.list().get(query.list().size()-1);    
  137.          System.out.println(topic.getTitle());    
  138.     }    
  139. }    



利用Hibernate注解生成表

 转自:http://blog.csdn.net/madison__/article/details/55677099