Hibernate中的实体域模型和关系模型是通过映射文件关联起来的。而对象之间的关联关系可以通过在实体域模型中体现,然后在映射文件中描述,最后通过工具生成数据库表结构。
生成表结构的过程应该是这样子的:
下面我们每一个例子都会按照这个步骤进行。
定义测试Hibernate配置文件
我们的测试文件是这个样子滴:
<?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> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 数据库连接url --> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <!-- 用户名 --> <property name="connection.username">root</property> <!-- 密码 --> <property name="connection.password">root</property> <!-- JDBC驱动 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- 显示SQL语句 --> <property name="show_sql">true</property> <!-- 引用hbm文件 --> <mapping resource="configure/Game.hbm.xml" /> <mapping resource="configure/Task.hbm.xml" /> </session-factory> </hibernate-configuration>
生成数据库
我们生成数据库的代码如下:
package com.freesoft.db.demo; import org.hibernate.tool.hbm2ddl.SchemaExport; public class CreateTable { public static void main(String[] args) { SchemaExport export = new SchemaExport(new org.hibernate.cfg.Configuration().configure()); // public void create(boolean script, boolean export) // script标识是否生成SQL语句 // export标识是否输出export(到数据库中),也就是是否执行SQL语句(生成数据库) export.create(true, true); } }
一对多/多对一例子
我们的这个例子是设计一个游戏,游戏中有多个关卡。
实体域模型
游戏Game类
package com.freesoft.bean; import java.util.HashSet; import java.util.Set; public class Game { private String id; private String name; private Set<Task> tasks = new HashSet<Task>(); public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set<Task> getTasks() { return tasks; } public void setTasks(Set<Task> tasks) { this.tasks = tasks; } }
package com.freesoft.bean; public class Task { // id private long id; // 名称 private String name; // 难易程度 private long level; // 得分 private long score; // 游戏 private Game game; public String getName() { return name; } public void setName(String name) { this.name = name; } public long getLevel() { return level; } public void setLevel(long level) { this.level = level; } public long getScore() { return score; } public void setScore(long score) { this.score = score; } public Game getGame() { return game; } public void setGame(Game game) { this.game = game; } public long getId() { return id; } public void setId(long id) { this.id = id; } }
编写映射文件
Game映射
<?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="com.freesoft.bean.Game" table="game" > <id name="id" type="string" column="id"> <generator class="uuid" /> </id> <property name="name" type="string" column="name" /> <!-- cascade为all表示同步更新所有操作,包括save/update/delete --> <!-- inverse为true表示game和task的关系由task维护 --> <set name="tasks" cascade="all" inverse="true" > <key column="game_id"></key> <one-to-many class="com.freesoft.bean.Task" /> </set> </class> </hibernate-mapping>
Task映射
<?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="com.freesoft.bean.Task" table="task" > <id name="id" type="long" column="id"> <generator class="increment" /> </id> <property name="name" type="string" column="name" /> <property name="level" type="long" column="level" /> <property name="score" type="long" column="score" /> <!-- 多对一,通过game_id关联到game属性 --> <many-to-one name="game" class="com.freesoft.bean.Game" > <column name="game_id" ></column> </many-to-one> </class> </hibernate-mapping>
测试代码
测试数据库的代码如下:
package com.freesoft.db.demo; import org.hibernate.Session; import org.hibernate.Transaction; import com.freesoft.bean.Game; import com.freesoft.bean.Task; public class DemoGame { public static void main(String[] args) { Session s = com.freesoft.db.utils.HibernateSessionFactory.getSession(); Transaction tx = s.beginTransaction(); try { Game game = new Game(); game.setName("疯狂猜图"); Task task01 = new Task(); task01.setName("第一关"); task01.setLevel(1); task01.setScore(1); task01.setGame(game); Task task02 = new Task(); task02.setName("第一关"); task02.setLevel(1); task02.setScore(1); task02.setGame(game); game.getTasks().add(task01); game.getTasks().add(task02); s.save(game); tx.commit(); } catch (Exception e) { e.printStackTrace(); tx.rollback(); } finally { if (s != null) s.close(); } } }