Hibernate – One-to-One example (XML Mapping)
2015-09-16 16:16
701 查看
A one-to-one relationships occurs when one entity is related to exactly one occurrence in another entity.
In this tutorial, we show you how to work with one-to-one table relationship in Hibernate, via XML mapping file (hbm).
Tools and technologies used in this tutorials :
Hibernate 3.6.3.Final
MySQL 5.1.15
Maven 3.0.3
Eclipse 3.6
File :
File :
File :
File :
File :
Note
The main difficulty in this one-to-one relationship is ensuring both are assigned the same primary key. In
File :
File :
Done
In this tutorial, we show you how to work with one-to-one table relationship in Hibernate, via XML mapping file (hbm).
Tools and technologies used in this tutorials :
Hibernate 3.6.3.Final
MySQL 5.1.15
Maven 3.0.3
Eclipse 3.6
Project Structure
See the final project structure of this tutorial.Project Dependency
Gethibernate.jarfrom JBoss repository, Maven will take care all the related dependencies for you.
File : pom.xml
<project ...> <repositories> <repository> <id>JBoss repository</id> <url>http://repository.jboss.org/nexus/content/groups/public/</url> </repository> </repositories> <dependencies> <!-- MySQL database driver --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.15</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.3.Final</version> </dependency> <dependency> <groupId>javassist</groupId> <artifactId>javassist</artifactId> <version>3.12.1.GA</version> </dependency> </dependencies> </project>
1. “One-to-one” table relationship
A one-to-one relationship table design, aSTOCKtable contains exactly one record in
STOCK_DETAILtable. Both tables have the same
Stock_Idas primary key. In
STOCK_DETAILtable,
Stock_Idis the primary key and also a foreign key to
STOCKtable. This is the common way of define “one-to-one” table relationship.
2. Hibernate Model Class
Create two model classes –Stock.javaand
StockDetail.java, to represent the above tables.
File : Stock.java
package com.mkyong.stock; public class Stock implements java.io.Serializable { private Integer stockId; private String stockCode; private String stockName; private StockDetail stockDetail; //constructor & getter and setter methods }
File : StockDetail.java
package com.mkyong.stock; public class StockDetail implements java.io.Serializable { private Integer stockId; private Stock stock; private String compName; private String compDesc; private String remark; private Date listedDate; //constructor & getter and setter methods }
3. Hibernate XML Mapping
Now, create two Hibernate mapping files (hbm) –Stock.hbm.xmland
StockDetail.hbm.xml.
File : Stock.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.mkyong.stock.Stock" table="stock" catalog="mkyongdb"> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="identity" /> </id> <property name="stockCode" type="string"> <column name="STOCK_CODE" length="10" not-null="true" unique="true" /> </property> <property name="stockName" type="string"> <column name="STOCK_NAME" length="20" not-null="true" unique="true" /> </property> <one-to-one name="stockDetail" class="com.mkyong.stock.StockDetail" cascade="save-update"></one-to-one> </class> </hibernate-mapping>
File : StockDetail.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 25 April 2011 7:52:33 PM by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.mkyong.stock.StockDetail" table="stock_detail" catalog="mkyongdb"> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="foreign"> <param name="property">stock</param> </generator> </id> <one-to-one name="stock" class="com.mkyong.stock.Stock" constrained="true"></one-to-one> <property name="compName" type="string"> <column name="COMP_NAME" length="100" not-null="true" /> </property> <property name="compDesc" type="string"> <column name="COMP_DESC" not-null="true" /> </property> <property name="remark" type="string"> <column name="REMARK" not-null="true" /> </property> <property name="listedDate" type="date"> <column name="LISTED_DATE" length="10" not-null="true" /> </property> </class> </hibernate-mapping>
Note
The main difficulty in this one-to-one relationship is ensuring both are assigned the same primary key. In
StockDetail.hbm.xml, a special foreign identifier generator is declared, it will know get the primary key value from
STOCKtable. With
constrained=”true”, it ensure the
Stockmust exists.
4. Hibernate Configuration File
PutsStock.hbm.xmland
StockDetail.hbm.xmlin your Hibernate configuration file, and also MySQL connection details.
File : 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> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mkyongdb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">password</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mapping resource="com/mkyong/stock/Stock.hbm.xml" /> <mapping resource="com/mkyong/stock/StockDetail.hbm.xml" /> </session-factory> </hibernate-configuration>
5. Run It
Run it, Hibernate will insert a row into theSTOCKtable and a row into the
STOCK_DETAILtable.
File : App.java
package com.mkyong; import java.util.Date; import org.hibernate.Session; import com.mkyong.stock.Stock; import com.mkyong.stock.StockDetail; import com.mkyong.util.HibernateUtil; public class App { public static void main(String[] args) { System.out.println("Hibernate one to one (XML mapping)"); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); Stock stock = new Stock(); stock.setStockCode("4715"); stock.setStockName("GENM"); StockDetail stockDetail = new StockDetail(); stockDetail.setCompName("GENTING Malaysia"); stockDetail.setCompDesc("Best resort in the world"); stockDetail.setRemark("Nothing Special"); stockDetail.setListedDate(new Date()); stock.setStockDetail(stockDetail); stockDetail.setStock(stock); session.save(stock); session.getTransaction().commit(); System.out.println("Done"); } }
Output
Hibernate one to one (XML mapping) Hibernate: insert into mkyongdb.stock (STOCK_CODE, STOCK_NAME) values (?, ?) Hibernate: insert into mkyongdb.stock_detail (COMP_NAME, COMP_DESC, REMARK, LISTED_DATE, STOCK_ID) values (?, ?, ?, ?, ?)
Done
相关文章推荐
- XML 与 JSON 优劣对比
- As3.0 xml + Loader应用代码
- 网马生成器 MS Internet Explorer XML Parsing Buffer Overflow Exploit (vista) 0day
- ext读取两种结构的xml的代码
- C#针对xml基本操作及保存配置文件应用实例
- asp下查询xml的实现代码
- sqlserver FOR XML PATH 语句的应用
- 使用sp_xml_preparedocument处理XML文档的方法
- C#中的Linq to Xml详解
- C# Entity Framework中的IQueryable和IQueryProvider详解
- C#操作XML文件实例汇总
- SQL Server中的XML数据进行insert、update、delete
- SQL Server中的XML数据进行insert、update、delete操作实现代码
- 关于SQLServer2005的学习笔记 XML的处理
- C#通过DataSet读写xml文件的方法
- C#实现基于XML配置MenuStrip菜单的方法
- php xml 入门学习资料
- Zend 输出产生XML解析错误
- PHP遍历XML文档所有节点的方法