-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: understanding 1 to 1
PostPosted: Mon Mar 25, 2013 2:36 pm 
Newbie

Joined: Wed Mar 28, 2012 8:31 pm
Posts: 9
Hi all,
The java code below is from mkyong's 1 to 1 example. I see the stock object has a stock detail object and
also the stock detail object has a stock object. So, this enables the 1 to 1 relationship of the database's
stock and stock details tables.

But, what is the reasoning for setting up the java code this way? Isn't this duplication? Does this relationship
have to be bidirectional? If we did not have persistence would we still set up the java code as such?

thanks,
m.
----------------------------------------------------------------------------------------------------------------------------------------------------------
stock.java
Code:
package com.mkyong.stock;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

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

   private Integer stockId;
   private String stockCode;
   private String stockName;
   private StockDetail stockDetail;

   public Stock() {
   }

   public Stock(String stockCode, String stockName) {
      this.stockCode = stockCode;
      this.stockName = stockName;
   }

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

   @Id
   @GeneratedValue(strategy = IDENTITY)
   @Column(name = "STOCK_ID", unique = true, nullable = false)
   public Integer getStockId() {
      return this.stockId;
   }

   public void setStockId(Integer stockId) {
      this.stockId = stockId;
   }

   @Column(name = "STOCK_CODE", unique = true, nullable = false, length = 10)
   public String getStockCode() {
      return this.stockCode;
   }

   public void setStockCode(String stockCode) {
      this.stockCode = stockCode;
   }

   @Column(name = "STOCK_NAME", unique = true, nullable = false, length = 20)
   public String getStockName() {
      return this.stockName;
   }

   public void setStockName(String stockName) {
      this.stockName = stockName;
   }

   @OneToOne(fetch = FetchType.LAZY, mappedBy = "stock", cascade = CascadeType.ALL)
   public StockDetail getStockDetail() {
      return this.stockDetail;
   }

   public void setStockDetail(StockDetail stockDetail) {
      this.stockDetail = stockDetail;
   }

}

---------------------------------------------------------------------------------------------------------------------------------------------------
stockdetail.java
Code:
package com.mkyong.stock;

import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name = "stock_detail", catalog = "mkyongdb")
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;

   public StockDetail() {
   }

   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;
   }

   @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;
   }

   public void setStockId(Integer stockId) {
      this.stockId = stockId;
   }

   @OneToOne(fetch = FetchType.LAZY)
   @PrimaryKeyJoinColumn
   public Stock getStock() {
      return this.stock;
   }

   public void setStock(Stock stock) {
      this.stock = stock;
   }

   @Column(name = "COMP_NAME", nullable = false, length = 100)
   public String getCompName() {
      return this.compName;
   }

   public void setCompName(String compName) {
      this.compName = compName;
   }

   @Column(name = "COMP_DESC", nullable = false)
   public String getCompDesc() {
      return this.compDesc;
   }

   public void setCompDesc(String compDesc) {
      this.compDesc = compDesc;
   }

   @Column(name = "REMARK", nullable = false)
   public String getRemark() {
      return this.remark;
   }

   public void setRemark(String remark) {
      this.remark = remark;
   }

   @Temporal(TemporalType.DATE)
   @Column(name = "LISTED_DATE", nullable = false, length = 10)
   public Date getListedDate() {
      return this.listedDate;
   }

   public void setListedDate(Date listedDate) {
      this.listedDate = listedDate;
   }

}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.