---
title: "使用hibernate tools插件生成POJO"
author: "maimode"
date: "2012-08-13T16:43:33Z"
tags: []
categories: ["开源软件"]
slug: "hibernate-tools-pojo"
draft: false
lastmod: "2012-08-14T10:04:57Z"
id: 1630670
url: "https://maimode.iteye.com/blog/1630670"
---

很多时候我们已经设计好了数据库，需要使用hibernate来做数据持久化，因此需要根据数据库中的表结构生成相应的POJO.

 

本例使用hibernatetools来自动创建pojo。

 

测试环境：eclipse3.7（indigo）+hibernate4.1+hibernatetools3.3+oracle10g

 

**1.下载hibernatetools插件**

 

hibernatetools可从官网下载，但是需要注意下载和自己使用的eclipse版本对应的版本包，然后安装到eclipse中取，link安装方式参见：[http://maimode.iteye.com/blog/1164524](/blog/1164524)

 

**2.创建hibernate配置文件及console配置文件**

 

新建java项目，建立包test,test.hibernate,test.model。

选中项目，右键-》new-》other-》hibernate-》hibernate configuration file：

![](http://dl.iteye.com/upload/attachment/0072/2402/86648b10-14bd-39bc-93f1-f18fe02a8884.png)

 

然后 next，配置console文件：

![](http://dl.iteye.com/upload/attachment/0072/2406/eb513bb7-e4d9-3471-aacb-7cf7d5a5c4eb.png)

 

 

**3.创建reverse文件**

 

选中项目，右键-》new-》other-》hibernate-》hibernate reverse engineering file

![](http://dl.iteye.com/upload/attachment/0072/2412/353f0496-995d-3d3b-a28f-27326381ca58.png)

 

下一步，配置要生成POJO对应的表：

![](http://dl.iteye.com/upload/attachment/0072/2783/31d94b53-caec-358d-8e9b-f1837b9d1a54.png)

 

**4.反向工程创建pojo**

 

在hibernate code generation configuration菜单中配置

![](http://dl.iteye.com/upload/attachment/0072/2418/a98128bd-4ec9-3a7d-adfa-f5dfc08b7225.png)

 

![](http://dl.iteye.com/upload/attachment/0072/2410/6467b8aa-d9d4-3136-aad8-de1d374f8ec5.png)

 

![](http://dl.iteye.com/upload/attachment/0072/2408/d4e1be82-167e-3570-b9ab-9465d2b6de5b.png)

 

*执行run后便生成了相应的pojo文件：*

 

![](http://dl.iteye.com/upload/attachment/0072/2422/3f98db03-072d-3cb5-bd7e-b12e5ac08229.png)

 

 

一般自动生成的mapping文件（XXX.hbm.xml）都需要修改后才能使用。