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格式的文件后,需要作句法分析去提取发布方提供 ...
随机推荐
- PHP curl 上传文件版本兼容问题
[摘要:做微疑开辟挪用微疑接心上传文件时,总是返回 {"errcode":41005,"errmsg":"media data missing hin ...
- Linx 的组管理和权限管理
Linux组基本介绍 在linux中的每个用户必须属于一个组,不能独立于组外.在linux中每个文件 有所有者.所在组.其它组的概念. 1) 所有者 2) 所在组 3) 其它组 4) 改变用户所在的组 ...
- 海思的一个 Makefile 解析
Makefile 原文 include ../Makefile.param #ifeq ($(SAMPLE_PARAM_FILE), ) # SAMPLE_PARAM_FILE:=../Makefil ...
- 项目Alpha冲刺 6
作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 介绍第6天冲刺的项目进展.问题困难和心得体会 1.团队信息 队名:火鸡堂 队 ...
- 【算法笔记】B1049 数列的片段和
1049 数列的片段和 (20 分) 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, ...
- 剑指offer——面试题16:数值的整数次方
// 面试题16:数值的整数次方 // 题目:实现函数double Power(double base, int exponent),求base的exponent // 次方.不得使用库函数,同时不需 ...
- java程序没有运行选项
1.检查module是否正确 确保src为资源文件 2.检查是否有main函数
- java HelloWorld时报错:"找不到或无法加载主类"问题的解决办法
学习java的第一天: 当我在做Java入门的时候,根据教程写的第一个Java程序是: public class Hello{ public static void main(String args[ ...
- Postman学习(压力测试)
Postman下载安装后 下面是在网上随便抓了一个请求地址来做演示,把请求地址填入地址栏,此请求为GET请求.点击Send发送请求,请求结果将会在下方显示出来.每次的请求历史数据,会被记录下来,但是经 ...
- excel表计算和计算器计算结果不一致
excel表计算和计算器计算结果不一致 : 建议安装完excel进行精度设置: