I am trying to create tables associated by OneToOne unidirectional mapping. I wrote a small demo program and the tables are getting created but showing null values.There are no exceptions on console.It would be great if someone could help why the enteries do not persist in tables.
Thanks
package com.abc.unidirectional;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "OneToOne_Stock_Unidirectional")
public class Stock implements java.io.Serializable {
private static final long serialVersionId=1L;
@Id
@GeneratedValue
private int stockId;
private String stockCode;
private String stockName;
private StockDailyRecords stockDailyRecords;
public Stock() {
}
public int getStockId() {
return stockId;
}
public void setStockId(int stockId) {
this.stockId = stockId;
}
public String getStockCode() {
return this.stockCode;
}
public void setStockCode(String stockCode) {
this.stockCode = stockCode;
}
public String getStockName() {
return this.stockName;
}
public void setStockName(String stockName) {
this.stockName = stockName;
}
public StockDailyRecords getStockDailyRecords() {
return stockDailyRecords;
}
public void setStockDailyRecords(StockDailyRecords stockDailyRecords) {
this.stockDailyRecords = stockDailyRecords;
}
@Override
public String toString() {
return "Stock [stockId=" + stockId + ", stockCode=" + stockCode + ", stockName=" + stockName
+ ", stockDailyRecords=" + stockDailyRecords + "]";
}
}
This is the StockDailyRecords class
package com.abc.unidirectional;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
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 = "OneToOne_Unidirectional")
public class StockDailyRecords implements java.io.Serializable {
private static final long serialVersionId = 1L;
@Id
@GeneratedValue(generator = "newGenerator") //name of the primary key generator
@GenericGenerator(name = "newGenerator", strategy = "foreign",parameters = { @Parameter(value = "stockDailyRecords", name = "property") })
private int stockId;
private String recordId;
private double priceOpen;
private double priceClose;
private float priceChange;
private Date listedDate;
@OneToOne
@JoinColumn(name = "stockId")
private Stock stock;
public StockDailyRecords() {
}
public int getStockId() {
return stockId;
}
public void setStockId(int stockId) {
this.stockId = stockId;
}
public String getRecordId() {
return recordId;
}
public void setRecordId(String recordId) {
this.recordId = recordId;
}
public Stock getStock() {
return this.stock;
}
public void setStock(Stock stock) {
this.stock = stock;
}
public double getPriceOpen() {
return priceOpen;
}
public void setPriceOpen(double priceOpen) {
this.priceOpen = priceOpen;
}
public double getPriceClose() {
return priceClose;
}
public void setPriceClose(double priceClose) {
this.priceClose = priceClose;
}
public float getPriceChange() {
return priceChange;
}
public void setPriceChange(float priceChange) {
this.priceChange = priceChange;
}
//@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;
}
@Override
public String toString() {
return "StockDailyRecords [stockId=" + stockId + ", recordId=" + recordId + ", priceOpen=" + priceOpen
+ ", priceClose=" + priceClose + ", priceChange=" + priceChange + ", listedDate=" + listedDate
+ ", stock=" + stock + "]";
}
}
This is my main class
package com.abc.unidirectional;
import java.util.Date;
import org.hibernate.Transaction;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import com.cavalier.unidirectional.HibernateUtil;
import org.hibernate.cfg.AnnotationConfiguration;
public class Main {
public static void main(String[] args) {
Main obj = new Main();
obj.saveInTable();
}
public Integer saveInTable(){
Stock stock = new Stock();
stock.setStockCode("705");
stock.setStockName("MICROSOFT");
StockDailyRecords sdrecords1 = new StockDailyRecords();
sdrecords1.setPriceOpen(50.30);
sdrecords1.setPriceClose(80);
sdrecords1.setPriceChange(10.9f);
sdrecords1.setListedDate(new Date());
sdrecords1.setStock(stock);
stock.setStockDailyRecords(sdrecords1);
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session= sessionFactory.openSession();
Transaction transaction = null;
Integer stockId = null;
try {
transaction = session.beginTransaction();
session.save(stock);
session.save(sdrecords1);
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
sessionFactory.close();
}
return stockId; //returns serializable primary key
}
}
This is the hibernate.cfg.xml file:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/emprec</property>
<property name="connection.username">***</property>
<property name="connection.password">***</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<!-- Mention here all the model classes along with their package name -->
<mapping class="com.abc.unidirectional.Stock"/>
<mapping class="com.abc.unidirectional.StockDailyRecords"/>
</session-factory>
</hibernate-configuration>