A one-to-one relationships occurs when one entity is related to exactly one occurrence in another entity.
“One-to-one” example
This is a one-to-one relationship table design, a STOCK table contains exactly one record in STOCK_DETAIL table. Both tables have the same Stock_Id as primary key. In STOCK_DETAIL table, Stock_Id is the primary key and also a foreign key to STOCK table. This is one of the common ways to define one-to-one relationship. More detail…

Hibernate implementation
In Hibernate, this “one-to-one” relationship can implemented in two ways :
- Hibernate XML Mapping file
- Hibernate Annotation
1. Hibernate XML mapping file
In XML mapping way, you need two Java classes to hold the table data.
- Stock.java -> STOCK table
- StockDetail.java -> STOCK_DETAIL table
and two XML mapping files to describe the table relationship.
- Stock.hbm.xml -> STOCK table
- StockDetail.hbm.xml -> STOCK_DETAIL table
Stock.java
... public class Stock implements java.io.Serializable { private StockDetail stockDetail; ... public StockDetail getStockDetail() { return this.stockDetail; } public void setStockDetail(StockDetail stockDetail) { this.stockDetail = stockDetail; }
StockDetail.java
... public class StockDetail implements java.io.Serializable { private Stock stock; ... public Stock getStock() { return this.stock; } public void setStock(Stock stock) { this.stock = stock; }
Stock.hbm.xml
... <class name="com.mkyong.common.Stock" table="stock" ...> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> <generator class="identity" /> </id> ... <one-to-one name="stockDetail" class="com.mkyong.common.StockDetail" cascade="save-update"></one-to-one> ...
StockDetail.hbm.xml
... <class name="com.mkyong.common.StockDetail" table="stock_detail" ...> <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.common.Stock" constrained="true"></one-to-one> ...
The main difficulty with 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 STOCK table. With constrained=”true”, it ensure the Stock must exists.
2. Hibernate annotation
In annotation way, only two classes are required, all the relationships are declared inside the Java classes.
- Stock.java -> STOCK table
- StockDetail.java -> STOCK_DETAIL table
Stock.java
... @Entity @Table(name = "stock", catalog = "mkyong", uniqueConstraints = { @UniqueConstraint(columnNames = "STOCK_NAME"), @UniqueConstraint(columnNames = "STOCK_CODE") }) public class Stock implements java.io.Serializable { private StockDetail stockDetail; ... @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "STOCK_ID", unique = true, nullable = false) public Integer getStockId() { return this.stockId; } @OneToOne(fetch = FetchType.LAZY, mappedBy = "stock") @Cascade({CascadeType.SAVE_UPDATE}) public StockDetail getStockDetail() { return this.stockDetail; }
StockDetail.java
@Entity @Table(name = "stock_detail", catalog = "mkyong") public class StockDetail implements java.io.Serializable { private Stock stock; ... @GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "stock")) @Id @GeneratedValue(generator = "generator") @Column(name = "STOCK_ID", unique = true, nullable = false) public Integer getStockId() { return this.stockId; } @OneToOne(fetch = FetchType.LAZY) @PrimaryKeyJoinColumn public Stock getStock() { return this.stock; }
The code is self-explanatory, all the @annotation are the XML mapping tag substitution.
Test It
Run the example, both annotation and XML mapping file will generate the same output. Hibernate inserts a row into the STOCK table and a row into the STOCK_DETAIL table.
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.setListedDate(new Date()); stock.setStockDetail(stockDetail); stockDetail.setStock(stock); session.save(stock);
Output
Hibernate:
INSERT INTO mkyong.stock
(STOCK_CODE, STOCK_NAME)
VALUES (?, ?)
Hibernate:
INSERT INTO mkyong.stock_detail
(COMP_NAME, COMP_DESC, REMARK, LISTED_DATE, STOCK_ID)
VALUES (?, ?, ?, ?, ?)
Popularity: 1% [?]
Related posts:
- Different between session.get() and session.load() Often times, you will notice Hibernate developers mix use of...
- Developing Applications with JBoss and Hibernate: Part 2 Adding a web client to your project There are several...
- Developing Applications with JBoss and Hibernate: Part 1 In this article, by Francesco Marchioni, we will introduce Hibernate,...
- Как да присвоите съдаржание от файл с променлива от Java Ще използваме DataInputStream за да преобразуваме , съдържанието в байтове....
- Файл настройки Пример 1 В тозу пример, ще напиша стойност, която да...
Related posts brought to you by Yet Another Related Posts Plugin.
RSS Feed
Twitter

февруари 5th, 2010
admin
Posted in 

Hi, nice comment. I look forward to your next topic. Thank you, Zoe
This is a useful piece of writing, I found your weblog researching aol for a related theme and came to this. I couldnt get to much alternative information and facts on this blog post, so it was good to locate this one. I will certainly end up being back to look at some other posts that you have another time.
Because of the constant trend in rising costs and the financial meltdown, people are beginning to pay more attention to their finances and spending. Everywhere around us, we observe this trend in the changing world.
There are a lot of online pharmaceutical companies that sell Viagra pill with no prescription to boot.