How to generate Hibernate mapping files & annotation with Hibernate Tools

转载 2015年09月16日 10:39:26

In this article, we show you how to use Hibernate / JBoss Tools to generate Hibernate mapping files (hbm) and annotation code from database automatically.

Tools in this article

  • Eclipse v3.6 (Helios)
  • JBoss / Hibernate Tools v3.2
  • Oracle 11g
  • JDK 1.6

Note
Before proceed, please Install Hibernate / JBoss Tools in Eclipse IDE.

1. Hibernate Perspective

Open your “Hibernate Perspective“. In Eclipse IDE, select “Windows” >> “Open Perspective” >> “Others…” , choose “Hibernate“.

2. New Hibernate Configuration

In Hibernate Perspective, right click and select “Add Configuration…

In “Edit Configuration” dialog box,
In “Project” box, click on the “Browse..” button to select your project.
In “Database Connection” box, click “New..” button to create your database settings.
In “Configuration File” box, click “Setup” button to create a new or use existing “Hibernate configuration file”, hibernate.cfg.xml.
Eclipse Hibernate Tools
See your list of your tables in “Hibernate Perspective“.

Eclipse Hibernate Tools
Sample of “hibernate.cfg.xml“, connect to Oracle 11g database.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
  <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
  <property name="hibernate.connection.url">jdbc:oracle:thin:@127.0.0.1:1521:MKYONG</property>
  <property name="hibernate.connection.username">mkyong</property>
  <property name="hibernate.connection.password">password</property>
  <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
  <property name="hibernate.default_schema">MKYONG</property>
 </session-factory>
</hibernate-configuration>

3. Hibernate Code Generation

Now, you are ready to generate the Hibernate mapping files and annotation codes.

– In “Hibernate Perspective”, click “Hibernate code generation” icon (see below figure) and select “Hibernate Code Generation Configuration

Hibernate Code Generation
– Create a new configuration, select your “console configuration” (configured in step 2), puts your “Output directory” and checked option “Reverse engineer from JDBC Connection“.

Hibernate Code Generation
– In “Exporter” tab, select what you want to generate, Model , mapping file (hbm) , DAO, annotation code and etc.

Hibernate Code Generation
See result

Hibernate Code Generation

Note
The generated Hibernate mapping file and annotations code are very clean, standard and easy to modify. Try explore more features.

hibernate3.6.10 注解配置添加表字段注释

如果使用的是xml方式配置,则:
  • earbao
  • earbao
  • 2014年08月14日 17:31
  • 16360

学习Hibernate源码——配置文件解析,SessionFactory创建

http://bsr1983.iteye.com/blog/1935617 本节要学习一下Hibernate的配置文件的具体加载、解析的过程,以及涉及到的相关代码,思路是建立一个简单的jav...
  • Baple
  • Baple
  • 2014年09月12日 11:22
  • 8071

spring4整合hibernate5报错解决

2015-09-19 21:50:34,525 WARN org.springframework.web.context.support.XmlWebApplicationContext.refre...
  • dlm_211314
  • dlm_211314
  • 2015年09月19日 21:58
  • 6272

How to generate ns2 trace files with VanetMobiSim

  • 2013年12月04日 16:25
  • 74KB
  • 下载

Mapping Oracle XMLType to Document with Hibernate

在使用C3P0数据库连接池时,以下示例代码可实现oracle.xdb.XMLType与org.w3c.dom.Document之间的转化。经过测试,在weblogic、tomcat服务器均能正常运行。...
  • wangdandong
  • wangdandong
  • 2017年03月13日 15:43
  • 152

How To Install Hibernate / JBoss Tools In Eclipse IDE

Hibernate Tools is a handy tool for Java’s developers to generate tedious hibernate related stuffs l...
  • guojunjian
  • guojunjian
  • 2012年08月23日 11:47
  • 821

(极力推荐)BW:How to load and generate a custom hierarchy in SAP Business Warehouse (SAP BW) without using flat files

这篇文章,真是救了哥,所以非常改写这位作者。你看,人家也是2011年2月3号才写的,量身打造,多好的人,刚好赶上我了。    PS:前段时间设计的文本抽取方式被否,为了去R3重建数据源,我找到...
  • asdfak
  • asdfak
  • 2011年02月18日 15:50
  • 5038

How can I use Microsoft Visual C++ 2010 to create MEX files with MATLAB 7.10 (R2010a)?

标题 How can I use Microsoft Visual C++ 2010 to create MEX files with MATLAB 7.10 (R2010a)?1 ...
  • Scut_Aspen
  • Scut_Aspen
  • 2015年09月18日 19:06
  • 665

How can I use Microsoft Visual C++ 2010 to create MEX files with MATLAB 7.10 (R2010a)?

直接下载下面的压缩包,然后解压出来,把\bin\win32\mexopts里面对应的文件拷贝到matlab2010a安装路径里面对应的文件夹下面即可!!   I have Visual Studi...
  • alec1987
  • alec1987
  • 2012年05月23日 09:10
  • 1274

如何在MMIT上使用WMLScript文件(How To Use WMLScript Files With MMIT)

准备翻译原文:http://www.dotnet247.com/247reference/msgs/20/103177.aspx++++++++++++++++++++++++++++++++++++...
  • lincyang
  • lincyang
  • 2011年04月11日 13:48
  • 1534
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How to generate Hibernate mapping files & annotation with Hibernate Tools
举报原因:
原因补充:

(最多只允许输入30个字)