Hi,
I am facing problem when creating one-to-many relationship. I am getting Caused by: java.sql.SQLException: ORA-02291: integrity constraint (C900_CO_LOCAL.FK_STOCK_DAILY_RECORD) violated - parent key not found The issue i am getting when i add the foreign key constraint in child table
Please find the code below.
CREATE TABLE STOCK ( STOCK_ID NUMBER NOT NULL, STOCK_CODE VARCHAR2(10) NOT NULL, STOCK_NAME VARCHAR2(20) NOT NULL, CONSTRAINT PK_STOCK PRIMARY KEY (STOCK_ID ) )
CREATE TABLE STOCK_DAILY_RECORD ( RECORD_ID NUMBER NOT NULL, PRICE_OPEN FLOAT(6), PRICE_CLOSE FLOAT(6), PRICE_CHANGE FLOAT(6), VOLUME LONG, SDATE DATE, STOCK_ID NUMBER(10) )
ALTER TABLE STOCK_DAILY_RECORD ADD ( CONSTRAINT FK_STOCK_DAILY_RECORD FOREIGN KEY (RECORD_ID) REFERENCES STOCK (STOCK_ID))
parent(Stock) -------------- <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.test.stock.Stock" table="stock"> <id name="stockId" type="java.lang.Integer"> <column name="STOCK_ID" /> </id> <property name="stockCode" type="string"> <column name="STOCK_CODE" length="10" not-null="true" unique="true" /> </property> <property name="stockName" type="string"> <column name="STOCK_NAME" length="20" not-null="true" unique="true" /> </property> <set name="stockDailyRecords" table="stock_daily_record" lazy="true" fetch="select"> <key> <column name="STOCK_ID" not-null="true" /> </key> <one-to-many class="com.test.stock.StockDailyRecord" /> </set> </class> </hibernate-mapping>
child(StockDailyRecord) ----------------------- <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.test.stock.StockDailyRecord" table="stock_daily_record"> <id name="recordId" type="java.lang.Integer"> <column name="RECORD_ID" /> </id> <many-to-one name="stock" class="com.test.stock.Stock" fetch="select"> <column name="STOCK_ID" not-null="true" /> </many-to-one> <property name="priceOpen" type="java.lang.Float"> <column name="PRICE_OPEN" precision="6" /> </property> <property name="priceClose" type="java.lang.Float"> <column name="PRICE_CLOSE" precision="6" /> </property> <property name="priceChange" type="java.lang.Float"> <column name="PRICE_CHANGE" precision="6" /> </property> <property name="volume" type="java.lang.Long"> <column name="VOLUME" /> </property> <property name="date" type="date"> <column name="SDATE" length="10" not-null="true" unique="true" /> </property> </class> </hibernate-mapping>
client program -------------- package com.test.stock; import java.util.Date; import java.util.HashSet; import org.hibernate.Session; import java.util.Set; import com.test.stock.StockDailyRecord; public class Client { public static void main(String[] args) { System.out.println("Hibernate one to many (XML Mapping)"); Session session = HibernateUtils.getSessionFactory().openSession(); session.beginTransaction(); Stock stock = new Stock(); stock.setStockId(102); stock.setStockCode("7052"); stock.setStockName("PADINI"); session.save(stock); StockDailyRecord stockDailyRecords = new StockDailyRecord(); stockDailyRecords.setRecordId(202); 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()); //Set s = new HashSet(); //s.add(stockDailyRecords); //stock.setStockDailyRecords(s); System.out.println("1"); stockDailyRecords.setStock(stock); System.out.println("2"); stock.getStockDailyRecords().add(stockDailyRecords); System.out.println("3"); session.save(stockDailyRecords); session.save(stock); System.out.println("4"); System.out.println("5"); session.getTransaction().commit(); System.out.println("Done"); } }
Please do the needful
|