前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Hibernate 逆向工程生成POJO类和映射文件

Hibernate 逆向工程生成POJO类和映射文件

作者头像
Hongten
发布2018-09-18 10:22:31
7340
发布2018-09-18 10:22:31
举报
文章被收录于专栏:HongtenHongten

Guestbook.java

代码:

package com.b510.examplex;

import java.util.Date;

public class Guestbook implements java.io.Serializable {

 private static final long serialVersionUID = -7004492417383895995L;  private Integer id;  private String name;  private String email;  private String phone;  private String title;  private String content;  private Date createdTime;

 // Constructors

 /**   * @return the name   */  public String getName() {   return name;  }

 public Integer getId() {   return this.id;  }

 public void setId(Integer id) {   this.id = id;  }

 /**   * @param name   *            the name to set   */  public void setName(String name) {   this.name = name;  }

 /**   * @return the email   */  public String getEmail() {   return email;  }

 /**   * @param email   *            the email to set   */  public void setEmail(String email) {   this.email = email;  }

 /**   * @return the phone   */  public String getPhone() {   return phone;  }

 /**   * @param phone   *            the phone to set   */  public void setPhone(String phone) {   this.phone = phone;  }

 /**   * @return the title   */  public String getTitle() {   return title;  }

 /**   * @param title   *            the title to set   */  public void setTitle(String title) {   this.title = title;  }

 /**   * @return the content   */  public String getContent() {   return content;  }

 /**   * @param content   *            the content to set   */  public void setContent(String content) {   this.content = content;  }

 /**   * @return the createdTime   */  public Date getCreatedTime() {   return createdTime;  }

 /**   * @param createdTime   *            the createdTime to set   */  public void setCreatedTime(Date createdTime) {   this.createdTime = createdTime;  }

 /** default constructor */  public Guestbook() {  } }

Guestbook.hbm.xml

代码:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping>  <class name="com.b510.examplex.Guestbook" table="guestbook"   catalog="users">   <id name="id" type="java.lang.Integer">    <column name="id" />    <generator class="increment" />   </id>   <property name="name" type="java.lang.String">    <column name="name" length="200" />   </property>   <property name="email" type="java.lang.String">    <column name="email" length="50" />   </property>   <property name="phone" type="java.lang.String">    <column name="phone" length="20" />   </property>   <property name="title" type="java.lang.String">    <column name="title" length="200" />   </property>   <property name="content" type="java.lang.String">    <column name="content" length="1000" />   </property>   <property name="createdTime" type="java.util.Date">    <column name="created_time" length="10" />   </property>  </class> </hibernate-mapping>

测试代码:HibernateTest.java

代码:

/**  *  */ package com.b510.examplex;

import org.hibernate.Session; import org.hibernate.Transaction;

/**  *  * @author XHW  *  * @date 2011-7-8  *  */ public class HibernateTest {

 /**   * @param args   */  public static void main(String[] args) {   HibernateTest test = new HibernateTest();   test.testTransaction();  }

 public void testTransaction() {   Session session = HibernateSessionFactoryUtil.getSessionFactory()     .openSession();   Transaction tx = session.beginTransaction();

  Guestbook guestbook = new Guestbook();   guestbook.setName("Hongten");   guestbook.setPhone("123456");   guestbook.setEmail("hongtenzone@foxmail.com");   guestbook.setTitle("hello world!");   guestbook.setContent("hello world!hello world!");   guestbook.setCreatedTime(new java.util.Date());

  try {    session.save(guestbook);   } catch (Exception e) {    e.printStackTrace();    if (tx.isActive()) {     tx.rollback();    }   }   tx.commit();  }

}

运行效果:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment). log4j:WARN Please initialize the log4j system properly. Hibernate:     select         max(id)     from         guestbook Hibernate:     insert     into         users.guestbook         (name, email, phone, title, content, created_time, id)     values         (?, ?, ?, ?, ?, ?, ?)

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-07-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档