Skip to main content

hibernate: one-to-one mapping

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
No replies
matias.364
Offline
Joined: 2013-04-01

Hi all,

Listed below is the annotated java code to store a one-to-one relation in the
database using hibernate. This code is from: http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-exampl.... My question is what's
the reasoning for having both the class Stock contain a StockDetail
member and the class StockDetail contain a Stock member.

thanks much,
M.
--------------------------------------------------------------------------
Stock.java
@Entity
@Table(name = "stock", catalog = "mkyongdb", uniqueConstraints = {
@UniqueConstraint(columnNames = "STOCK_NAME"),
@UniqueConstraint(columnNames = "STOCK_CODE") })
public class Stock implements java.io.Serializable {

</pre> 
<pre> private Integer stockId;

private String stockCode;
private String stockName;
private StockDetail stockDetail;
</pre> 
<pre> public Stock() {

}
</pre> 
<pre> public Stock(String stockCode, String stockName) {

this.stockCode = stockCode;
this.stockName = stockName;
}
</pre> 
<pre> public Stock(String stockCode, String stockName, StockDetail stockDetail) {

this.stockCode = stockCode;
this.stockName = stockName;
this.stockDetail = stockDetail;
}
</pre> 
<pre> @Id

@GeneratedValue(strategy = IDENTITY)
@Column(name = "STOCK_ID", unique = true, nullable = false)
public Integer getStockId() {
return this.stockId;
}
</pre> 
<pre> public void setStockId(Integer stockId) {

this.stockId = stockId;
}
</pre> 
<pre> @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)

public String getStockCode() {
return this.stockCode;
}
</pre> 
<pre> public void setStockCode(String stockCode) {

this.stockCode = stockCode;
}
</pre> 
<pre> @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)

public String getStockName() {
return this.stockName;
}
</pre> 
<pre> public void setStockName(String stockName) {

this.stockName = stockName;
}
</pre> 
<pre> @OneToOne(fetch = FetchType.LAZY, mappedBy = "stock", cascade = CascadeType.ALL)

public StockDetail getStockDetail() {
return this.stockDetail;
}
</pre> 
<pre> public void setStockDetail(StockDetail stockDetail) {

this.stockDetail = stockDetail;
}
</pre> 
<pre>}

</pre>
---------------------------------------------------------------------------
StockDetail.java
<pre>@Entity

@Table(name = "stock_detail", catalog = "mkyongdb")
public class StockDetail implements java.io.Serializable {
</pre> 
<pre> private Integer stockId;

private Stock stock;
private String compName;
private String compDesc;
private String remark;
private Date listedDate;
</pre> 
<pre> public StockDetail() {

}
</pre> 
<pre> public StockDetail(Stock stock, String compName, String compDesc,

String remark, Date listedDate) {
this.stock = stock;
this.compName = compName;
this.compDesc = compDesc;
this.remark = remark;
this.listedDate = listedDate;
}
</pre> 
<pre> @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;
}
</pre> 
<pre> public void setStockId(Integer stockId) {

this.stockId = stockId;
}
</pre> 
<pre> @OneToOne(fetch = FetchType.LAZY)

@PrimaryKeyJoinColumn
public Stock getStock() {
return this.stock;
}
</pre> 
<pre> public void setStock(Stock stock) {

this.stock = stock;
}
</pre> 
<pre> @Column(name = "COMP_NAME", nullable = false, length = 100)

public String getCompName() {
return this.compName;
}
</pre> 
<pre> public void setCompName(String compName) {

this.compName = compName;
}
</pre> 
<pre> @Column(name = "COMP_DESC", nullable = false)

public String getCompDesc() {
return this.compDesc;
}
</pre> 
<pre> public void setCompDesc(String compDesc) {

this.compDesc = compDesc;
}
</pre> 
<pre> @Column(name = "REMARK", nullable = false)

public String getRemark() {
return this.remark;
}
</pre> 
<pre> public void setRemark(String remark) {

this.remark = remark;
}
</pre> 
<pre> @Temporal(TemporalType.DATE)

@Column(name = "LISTED_DATE", nullable = false, length = 10)
public Date getListedDate() {
return this.listedDate;
}
</pre> 
<pre> public void setListedDate(Date listedDate) {

this.listedDate = listedDate;
}
</pre> 
<pre>}