If you have worked on hibernate projects, you must be familiar with a lot of properties we need for hibernate mapping and configuration file. Without any proper tool that can guide us in looking for correct properties, it will become very hard to correctly configure our application. This is when Hibernate Tools Eclipse Plugin comes handy and a must have plugin for hibernate projects.
如果您从事过hibernate项目 ,则必须熟悉hibernate映射和配置文件所需的许多属性。 没有任何可以指导我们寻找正确属性的适当工具,正确配置我们的应用程序将变得非常困难。 这是Hibernate Tools Eclipse插件方便使用且必须具有用于hibernate项目的插件的时候。
Today we will look into installing hibernate tools in Eclipse and how to use it for generating Hibernate mapping and configuration xml files.
今天,我们将研究如何在Eclipse中安装hibernate工具,以及如何使用它来生成Hibernate映射和配置xml文件。
Installing Hibernate Tools eclipse plugin requires following steps.
安装Hibernate Tools eclipse插件需要执行以下步骤。
Now that Hibernate Tools Plugin is installed, let’s see how to generate Hibernate Mapping XML file using it.
现在已经安装了Hibernate Tools Plugin ,让我们看看如何使用它来生成Hibernate Mapping XML文件。
For my example, I have created a simple Java Bean in one of my example projects.
对于我的示例,我在一个示例项目中创建了一个简单的Java Bean。
Customer.java
Customer.java
package com.journaldev.hibernate.model;
public class Customer {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now let’s see how we can easily generate Hibernate Mapping file using hibernate tools plugin.
现在,让我们看看如何使用hibernate工具插件轻松生成Hibernate Mapping文件。
Customer.hbm.xml
. Below is the content of the auto generated file.Customer.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated May 18, 2014 12:31:51 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.journaldev.hibernate.model.Customer" table="CUSTOMER">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
</class>
</hibernate-mapping>
当您单击Finish按钮时,一个新文件将被添加到与您的Java Bean相同的包中。 对于我们的Customer类,文件名将为Customer.hbm.xml
。 以下是自动生成的文件的内容。 Customer.hbm.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
注意,Hibernate工具从SourceForge指向旧的DTD位置,您应该更改的是从Hibernate官方网站使用。 Hibernate Configuration XML file contains properties used to configure Database settings, connection pool settings etc. We can use Hibernate Tools plugin to generate the basic hibernate configuration xml file and then use it’s content assist to add additional properties.
Hibernate Configuration XML文件包含用于配置数据库设置,连接池设置等的属性。我们可以使用Hibernate Tools插件生成基本的hibernate配置xml文件,然后使用其内容辅助功能添加其他属性。
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"https://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">pankaj123</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/TestDB</property>
<property name="hibernate.connection.username">pankaj</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
Note that again the DTD is pointing to SourceForge and you should change it to point to Hibernate official DTD location, as specified above.
hibernate.cfg.xml
请注意,DTD再次指向SourceForge,您应该将其更改为指向Hibernate官方DTD位置,如上所述。
Because of content assist, we can easily find out the properties that we are looking for, rather than going through a lot of documentations or online resource. This saves a lot of development time and helps us in configuring our application easily.
由于内容的帮助,我们可以轻松地找到所需的属性,而无需浏览大量文档或在线资源。 这节省了大量开发时间,并帮助我们轻松配置应用程序。
That’s all for using hibernate tools eclipse plugin, I use it for all my hibernate projects and it saves a lot of time every single day.
这就是使用hibernate工具eclipse插件的全部内容,我将其用于我的所有hibernate项目,并且每天节省很多时间。
翻译自: https://www.journaldev.com/2940/hibernate-tools-eclipse-plugin