Skip to content

Instantly share code, notes, and snippets.

@vikasverma787
Created February 25, 2018 08:20
Show Gist options
  • Save vikasverma787/ae4c0aef0c0331b6b2f3c91d8e0af1ae to your computer and use it in GitHub Desktop.
Save vikasverma787/ae4c0aef0c0331b6b2f3c91d8e0af1ae to your computer and use it in GitHub Desktop.
One to one hibernate relationship
1 -------------> 1
Stock Stock_Daily_Record
@OneToOne(fetch = FetchType.LAZY, mappedBy = "stock", @OneToOne(fetch = FetchType.LAZY)
cascade = CascadeType.ALL) @PrimaryKeyJoinColumn
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;
}
}
-----------------------------------------------------
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;
}
}
-------------------------------------------------------------------
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 class="com.mkyong.stock.Stock" />
<mapping class="com.mkyong.stock.StockDetail" />
</session-factory>
</hibernate-configuration>
-------------------------------------------------------------
import com.mkyong.stock.Stock;
import com.mkyong.stock.StockDailyRecord;
import com.mkyong.util.HibernateUtil;
public class App {
public static void main(String[] args) {
System.out.println("Hibernate one to many (Annotation)");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Stock stock = new Stock();
stock.setStockCode("7052");
stock.setStockName("PADINI");
session.save(stock);
StockDailyRecord stockDailyRecords = new StockDailyRecord();
stockDailyRecords.setPriceOpen(new Float("1.2"));
stockDailyRecords.setPriceClose(new Float("1.1"));
stockDailyRecords.setPriceChange(new Float("10.0"));
stockDailyRecords.setVolume(3000000L);
stockDailyRecords.setDate(new Date());
stockDailyRecords.setStock(stock);
stock.getStockDailyRecords().add(stockDailyRecords);
session.save(stockDailyRecords);
session.getTransaction().commit();
System.out.println("Done");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment