hibernate使用SchemaExport生成对应的数据库表
2017-07-14 15:41
435 查看
我们在项目中完成实体类和对应的*.hbm.xml配置文件后,可以使用junit4来生成对应的数据库表,具体如下:
新建一个hibernate项目,具体步骤,可以参见《Hibernate环境搭建和配置》
项目架构如图:
![](http://img.blog.csdn.net/20170714152639093?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTE1OTQxNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
Score实体类代码如下:
package com.robert.pojo;
public class Score {
private ScoreId scoreId ;
private double result ;//成绩
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public ScoreId getScoreId() {
return scoreId;
}
public void setScoreId(ScoreId scoreId) {
this.scoreId = scoreId;
}
}
ScoreId实体类代码如下:
Score.hbm.xml配置如下:
实体类Score.hbm.xml和实体类对应关系如图:
![](http://img.blog.csdn.net/20170714164807003?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTE1OTQxNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
HibernateTest测试类的代码如下:
当我们使用junit4运行testCreateDB代码时,就会在数据库中生成对应的表,运行结果如图:
控制台打印的日志:
![](http://img.blog.csdn.net/20170714153803476?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMTE1OTQxNw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
数据库生成的表结构:
新建一个hibernate项目,具体步骤,可以参见《Hibernate环境搭建和配置》
项目架构如图:
Score实体类代码如下:
package com.robert.pojo;
public class Score {
private ScoreId scoreId ;
private double result ;//成绩
public double getResult() {
return result;
}
public void setResult(double result) {
this.result = result;
}
public ScoreId getScoreId() {
return scoreId;
}
public void setScoreId(ScoreId scoreId) {
this.scoreId = scoreId;
}
}
ScoreId实体类代码如下:
package com.robert.pojo; import java.io.Serializable; public class ScoreId implements Serializable{ private int stuId;// 学生编码 private int subjectId;// 科目编号 public int getStuId() { return stuId; } public void setStuId(int stuId) { this.stuId = stuId; } public int getSubjectId() { return subjectId; } public void setSubjectId(int subjectId) { this.subjectId = subjectId; } }
Score.hbm.xml配置如下:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.robert.pojo"> <class name="Score" table="score"> <!-- 联合主键 composite-id中的配置如下: name:是class是Score中的主键,这里是:scoreId class:是scoreId主键对应的联合主键的类,这里是ScoreId类 key-property:是联合主键所在的类ScoreId中的两个主键 --> <composite-id name="scoreId" class="ScoreId"> <key-property name="stuId"></key-property> <key-property name="subjectId"></key-property> </composite-id> <property name="result"></property> </class> </hibernate-mapping>
实体类Score.hbm.xml和实体类对应关系如图:
HibernateTest测试类的代码如下:
package com.robert.test; import org.hibernate.cfg.Configuration; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; public class HibernateTest { /** * 根据*.hbm.xml文件对应的生成数据库表 */ @Test public void testCreateDB() { Configuration cfg = new Configuration().configure() ; SchemaExport se = new SchemaExport(cfg) ; //第一个参数:是否生成ddl脚本 //第二个参数:是否执行到数据库中 se.create(true, true) ; } }
当我们使用junit4运行testCreateDB代码时,就会在数据库中生成对应的表,运行结果如图:
控制台打印的日志:
数据库生成的表结构:
相关文章推荐
- Hibernate整合Spring后,如何使用SchemaExport生成数据库表
- Hibernate中使用SchemaExport来自动生成数据库中的表时,控制台有sql语句,但是数据库中无新建的表
- 一口一口吃掉Hibernate(一)——使用SchemaExport生成数据表
- Hibernate之——使用SchemaExport类自动创建数据库表
- Hibernate5.x 使用SchemaExport生成数据表
- hibernate笔记--通过SchemaExport生成数据库表
- Hibernate 使用SchemaExport自动生成表
- hibernate中使用schemaExport生成数据表报错解决方法
- 利用ant,hibernate的schemaexport自动生成数据库
- Eclipse使用自带插件JPA Tools实现自动生成数据库对应表的实体类(不使用Hibernate)
- 【Hibernate】——SchemaExport自动生成数据库表
- hibernate中使用schemaExport生成数据表报错解决方法
- Hibernate(一)——使用SchemaExport生成数据表
- 一口一口吃掉Hibernate(一)——使用SchemaExport生成数据表
- Hibernate使用SchemaExport和AnnotationConfiguration生成表结构
- Hibernate5.2.10使用SchemaExport创建数据库
- hibernate使用SchemaExport自动生成数据表
- 使用hibernate的SchemaExport来创建数据库
- 使用SchemaExport生成数据库表
- MySQL数据库使用Innodb数据库类型(工具生成Hibernate+Spring代码)