日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/9 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Hibernate 逆向工程生成POJO类和映射文件 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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
??????? (?, ?, ?, ?, ?, ?, ?)

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的Hibernate 逆向工程生成POJO类和映射文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。