首先:之前通过Eclipse反向生成Hibernate的实体类,很傻瓜式,基本上不用配置。但是Idea通过表结构反向生成hibernate实体类和映射文件,如果单独生成一张表的实体Bean类,基本上不需要配置。但是针对关联的两张表,涉及到one-to-many和many-to-one的这种情况,Idea需要自己手动配置。对于如何配置,自己也绕了点弯路。此处记录下最终成功的一些操作,仅作参考。
1、需要准备的:
- 准备hinernate.cf.xml配置文件,放在工程目录的根目录下。例如:
<?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.bytecode.use_reflection_optimizer">false</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.pool_size">5</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatetest</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.generate_statistics">true</property> <property name="hibernate.jdbc.batch_versioned_data">true</property> <property name="hibernate.max_fetch_depth">5</property> <property name="hibernate.search.autoregister_listeners">false</property> <property name="hibernate.service.allow_crawling">false</property> <property name="hibernate.show_sql">true</property> <mapping></mapping> </session-factory> </hibernate-configuration>
- Idea工程设置支持hinernate。选中工程,右键,选在add framework support,勾选hibernate。
- 引入hibernate源码包,通过maven或者下载导入都可以。
- 数据库:从右边栏的Database中配置,如下:
2、以两个关联表为例:
father表和son表
CREATE TABLE `father` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`id`)
)
COMMENT='父亲'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `son` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NULL DEFAULT NULL,
`father_id` INT(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
INDEX `FK__father` (`father_id`),
CONSTRAINT `FK__father` FOREIGN KEY (`father_id`) REFERENCES `father` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
)
COMMENT='儿子'
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
father和son表是一对one-to-many的关系:一个父亲可以有多个儿子(one-to-many),多个儿子可以有一个父亲(many-to-one)。两表通过son表的外键father_id,产生关系。
所以,如果生成bean文件,我们需要的关系应该如下:
public class FatherPO {
private Set<SonPO> sons;
}
public class SonPO {
private FatherPO father;
}
3、步骤:
- Persistence-->Generate persistence mapping-->By database schema
- 如下勾选,选中需要生成的表。选中son表,右键Add relationship
- 配置关联属性。注意:一定要在最底下的Join column中配置两个Bean之间的联系,实属两张表之间的外键映射关系。Eclipse是自动生成映射联系,但是Idea似乎不能,需要自己手动配置(PS:我就是在这儿琢磨的半天,忘了配置,导致错误)。
针对add relationship页的配置官方说明:http://www.jetbrains.com/help/idea/2016.1/add-relationship.html?search=add%20re
- 然后就OK了。生成的bean和xml文件分别如下。(注:Idea生成的.hbm.xml文件没有lasy,fetch等配置,需要自己按照需求手动添加)
@Entity @Table(name = "father", schema = "hibernatetest", catalog = "") public class FatherPO { private Integer id; private String name; private Set<SonPO> sons; @Id @Column(name = "id", nullable = false) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Basic @Column(name = "name", nullable = false, length = 50) 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; FatherPO fatherPO = (FatherPO) o; if (id != null ? !id.equals(fatherPO.id) : fatherPO.id != null) return false; if (name != null ? !name.equals(fatherPO.name) : fatherPO.name != null) return false; return true; } @Override public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); return result; } @OneToMany(mappedBy = "father") public Set<SonPO> getSons() { return sons; } public void setSons(Set<SonPO> sons) { this.sons = sons; } }
<?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="hibernate.bean.FatherPO" table="father" schema="hibernatetest"> <id name="id"> <column name="id" sql-type="int(11)"/> </id> <property name="name"> <column name="name" sql-type="varchar(50)" length="50"/> </property> <set name="sons" inverse="true"> <key> <column name="father_id"/> </key> <one-to-many not-found="ignore" class="hibernate.bean.SonPO"/> </set> </class> </hibernate-mapping>
@Entity @Table(name = "son", schema = "hibernatetest", catalog = "") public class SonPO { private Integer id; private String name; private FatherPO father; @Id @Column(name = "id", nullable = false) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Basic @Column(name = "name", nullable = true, length = 50) 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; SonPO sonPO = (SonPO) o; if (id != null ? !id.equals(sonPO.id) : sonPO.id != null) return false; if (name != null ? !name.equals(sonPO.name) : sonPO.name != null) return false; return true; } @Override public int hashCode() { int result = id != null ? id.hashCode() : 0; result = 31 * result + (name != null ? name.hashCode() : 0); return result; } @ManyToOne @JoinColumn(name = "father_id", referencedColumnName = "id", nullable = false) public FatherPO getFather() { return father; } public void setFather(FatherPO father) { this.father = father; } }
<?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="hibernate.bean.SonPO" table="son" schema="hibernatetest"> <id name="id"> <column name="id" sql-type="int(11)"/> </id> <property name="name"> <column name="name" sql-type="varchar(50)" length="50" not-null="true"/> </property> <many-to-one name="father" class="hibernate.bean.FatherPO"> <column name="father_id"/> </many-to-one> </class> </hibernate-mapping>