Idea创建Hibernate bean类和.xml文件
Idea通过表结构反向生成Hibernate实体类和映射文件
首先:之前通过Eclipse反向生成Hibernate的实体类,很傻瓜式,基本上不用配置。但是Idea通过表结构反向生成hibernate实体类和映射文件,如果单独生成一张表的实体Bean类,基本上不需要配置。但是针对关联的两张表,涉及到one-to-many和many-to-one的这种情况,Idea需要自己手动配置。对于如何配置,自己也绕了点弯路。此处记录下最终成功的一些操作,仅作参考。
1.准备hibernate.cfg.xml配置文件,放在工程目录的根目录下。例如:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost::orcl</property>
<property name="connection.username">lex</property>
<property name="connection.password">lex</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>
</session-factory>
</hibernate-configuration
2. Idea工程设置支持hinernate,点击View选择Tool Windows下的Persistence,显示如下效果:
或者点击左下角的图标这样也可以找到我们要的:
3.数据库配置:从右边的DateBase配置,点击DateBase,选择你所要关联的数据库,在这里我关联的是Oracle数据库,我就以这个为例了。
会出现Data Sources and Drivers页面。
出现如下:
4.Persistence-->Generate persistence mapping-->By database schema
5.配置关联属性。注意:一定要在最底下的Join column中配置两个Bean之间的联系,实属两张表之间的外键映射关系。Eclipse是自动生成映射联系,但是Idea似乎不能,需要自己手动配置。需要注意的是:有些数据类型需要自己更改。
针对add relationship页的配置官方说明:http://www.jetbrains.com/help/idea/2016.1/add-relationship.html?search=add%20re
Use this dialog box to set up a relation between two entities of a persistence unit according to the relation between the corresponding tables of the selected database. Item Description
Source / Target Use these sections to define the source and target parts of a relationship. Table
In the Source section, this field is read-only and displays the name of the table selected in the Import Database Schema dialog box.
In the Target section, select the desired table from the list of tables available in the current data source. Attribute name Specify the names of the fields that will be created in the source and target entities respectively. Type Specify the relationship type by selecting appropriate options from the drop-down lists in the Source and Target sections. The list contains Java types, thus enabling you to define the Java code that will be generated for the relationship. Map Key Column Select the desired column name from the drop-down list. Join Table If this check box is selected, the data will be queried from both tables on the basis of the specified relationship. Select the desired join table from the list of tables available in the data source.
For the many-to-many relationships, this check box is selected by default, and not editable. Join Columns Depending on the type of relationship, this table features the following columns:
For one-to-one or one-to-many relationships, the table contains two columns:
Source Column - in this field, specify the foreign key of the source table.
Target Column - in this field, specify the referenced field in the target table.
For multiple relationships, four columns are available:
Source Column
Source Join Column
Target Join Column
Target Column
Use the Add add and Remove delete buttons to manage the list of join columns.
使用此对话框来设置一个持久性单元,根据所选数据库的相应表之间关系的两个实体之间的关系。 项目描述
源 / 目标使用这些部分来定义的源和目标部分的关系。
表
在源部分中,此字段是只读,并显示在导入数据库架构对话框中选定的表的名称。
在目标部分中,从当前的数据源中可用表的列表中选择所需的表。 属性名称指定的字段将在源中创建和分别针对实体名称。 通过选择适当的选项,从下拉列表中的源和目标的部分型关系的类型指定。该列表包含 Java 类型,从而使您能够定义的关系将生成的 Java 代码。 映射键列选择下拉列表中所需的列名称。 联接表如果此复选框处于选中状态,将根据指定的关系两个表中查询的数据。从可用数据源中的表的列表中选择所需的联接表。
对于多对多关系,此复选框是默认情况下,选定,不可编辑。 联接的列取决于关系,此表的类型特点以下各列︰
对于一对一或一对多的关系,此表包含两列︰
源列-在此字段中的,指定源表的外键。
目标列-在这一领域,在目标表中指定引用的字段。
对于多个关系,四列有可用:
源列
源的联接列
目标联接列
目标列
使用添加添加和删除删除按钮来管理的联接列的列表。
选择一张表右键选择Add Relationship:
点击OK就可以了。
package cn.lex.entity; import javax.persistence.*;
import java.util.Set; /**
* Created by accp on 2017/1/13.
*/
@Entity
public class Type {
private long id;
private String name;
private Set<House> house; @Id
@Column(name = "ID", nullable = false, precision = )
public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} @Basic
@Column(name = "NAME", nullable = false, length = )
public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} @Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false; Type type = (Type) o; if (id != type.id) return false;
if (name != null ? !name.equals(type.name) : type.name != null) return false; return true;
} @Override
public int hashCode() {
int result = (int) (id ^ (id >>> ));
result = * result + (name != null ? name.hashCode() : );
return result;
} @OneToMany(mappedBy = "type")
public Set<House> getHouse() {
return house;
} public void setHouse(Set<House> house) {
this.house = house;
}
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="cn.lex.entity.Type" table="TYPE" schema="LEX">
<id name="id">
<column name="ID" sql-type="number(*)" precision=""/>
</id>
<property name="name">
<column name="NAME" sql-type="nvarchar2(50)" length=""/>
</property>
<set name="house" inverse="true">
<key>
<column name="TYPEID" not-null="true"/>
</key>
<one-to-many not-found="ignore" class="cn.lex.entity.House"/>
</set>
</class>
</hibernate-mapping>
Idea创建Hibernate bean类和.xml文件的更多相关文章
- Eclipse从数据库逆向生成Hibernate实体类和映射文件(Eclipse插件系列之HibernateTools)
♣下载安装Eclipse插件(HibernateTools) ♣Eclipse连接数据库(Mysql5.7) ♣新建hibernate.properties和hibernate.cfg.xml文件 ♣ ...
- JAXB—Java类与XML文件之间转换
JAXB-Java类与XML文件之间转换 简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生 ...
- Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件
Fixflow引擎解析(四)(模型) - 通过EMF扩展BPMN2.0元素 Fixflow引擎解析(三)(模型) - 创建EMF模型来读写XML文件 Fixflow引擎解析(二)(模型) - BPMN ...
- 简单实体类和xml文件的相互转换
最近写一个题目,要求将一组员工实体类转换成xml文件,或将xml文件转换成一组实体类.题目不难,但写完感觉可以利用泛型和反射将任意一个实体类和xml文件进行转换.于是今天下午立马动手 试了下,做了个简 ...
- 在C#程序中,创建、写入、读取XML文件的方法
一.在C#程序中,创建.写入.读取XML文件的方法 1.创建和读取XML文件的方法,Values为需要写入的值 private void WriteXML(string Values) { //保存的 ...
- 反向生成hibernate实体类和映射文件
工欲善其事,必先利其器.我们可以使用IDE来根据数据库中的表反向生成实体类和映射文件,虽然这些东西手写也并不是难度很大,但是如果存在大量的简单工作需要我们做,也会显得很麻烦. 写在前面 我们反向生成的 ...
- spring的显示装配bean(1)------通过XML文件装配
1:spring环境的简单搭建 (1)导入spring相关的jar包. 2:准备要进行装配的Java类 这里给出两个举例类 (1) (2) 3:配置XML文件 (1)在配置文件的顶部声明多个XML模式 ...
- intellij配置hibernate自动生成hbm.xml文件
1.首先创建一个Java web项目,这里因为已经在整个项目中配置好tomcat了,所以我是直接创建module的,其实和创建project的配置方法一样,创建的时候选择Web Application ...
- 用 Qt 中的 QDomDocument类 处理 XML 文件
XML,全称为 “可扩展标记语言”(extensible markup language).是一种非常方便的数据交换与数据存储的工具. 我们在取得一个XML格式的文件后,需要作句法分析去提取发布方提供 ...
随机推荐
- iOS核心动画之anchorpoint
anchorpoint是什么 All geometric manipulations to the view occur about the specified point 就是说所有的动画参考点都是 ...
- thinkphp3.2.3----图片上传并生成缩率图
public function uploadify(){ if(!IS_POST){ $this->error('非法!'); } $upload = $this->_upload(); ...
- Leetcode 856. Score of Parentheses 括号得分(栈)
Leetcode 856. Score of Parentheses 括号得分(栈) 题目描述 字符串S包含平衡的括号(即左右必定匹配),使用下面的规则计算得分 () 得1分 AB 得A+B的分,比如 ...
- Saiku2.6 保存查询后,重新打开报 Error Loading Query错误。
发现Saiku2.6的查询保存后重新打开就会报如下错误,同等的Schema文件和数据库环境在3.15环境里面打开是一切正常的. 后面对比了一下2.6和3.15的启动环境,发现有些差异的地方. 2.6启 ...
- HDU 5442 后缀自动机(从环字符串选定一个位置 , 时针或顺时针走一遍,希望得到字典序最大)
http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样 ...
- Q678 有效的括号字符串
给定一个只包含三种字符的字符串:(,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何右括号 ) 必须有相应的左括号 ( ...
- python爬虫之urllib库(一)
python爬虫之urllib库(一) urllib库 urllib库是python提供的一种用于操作URL的模块,python2中是urllib和urllib2两个库文件,python3中整合在了u ...
- DFS-20190206
找出所有方案 排列和组合问题 排列: https://www.lintcode.com/problem/combination-sum/description public class Solutio ...
- python logging模块的使用
logging 专门用于记录日志的模块,相对于print来说,logging 提供了日志信息的分级.格式化.过滤等功能.在程序中定义丰富有条理的log信息,可以方便分析程序的运行状态,在发生问题是可有 ...
- PIE SDK组件式开发综合运用示例
1. 功能概述 关于PIE SDK的功能开发,在我们的博客上已经分门别类的进行了展示,点击PIESat博客就可以访问,为了初学者入门,本章节将对从PIE SDK组件式二次开发如何搭建界面.如何综合开发 ...