Hibernate 逆向工程生成POJO类和映射文件
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>
測(cè)試代碼: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();
?}
}
運(yùn)行效果:
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
??????? (?, ?, ?, ?, ?, ?, ?)
總結(jié)
以上是生活随笔為你收集整理的Hibernate 逆向工程生成POJO类和映射文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 王道操作系统考研笔记——2.3.2 进程
- 下一篇: deepin linux grub,de