[HIBERNATE, DB2]
Hi
i've tried best to search and understand all threads with similar error but have not found any insight on what may be causing this. it's time to turn to experts :) am a newbie to hibernate..'ve a sample project here..OFFER_SPECIFICATION has a column OFFER_ID [primary key], which is a FK in OFFER_CHANNEL, having OFFER_ID and CHANNEL_TXT as composite primary key. ..have created corresponding classes and mappings, config and other requisites but am getting following error when trying to persist data using hibernate's save operation
OfferChannelDO.java
import java.io.Serializable;
public class OfferChannelDO implements Serializable { private int offerId; private String channelText; public OfferChannelDO() { super(); } public OfferChannelDO(String text) { super(); this.channelText = text; }
public int getOfferId() { return offerId; } public void setOfferId(int id) { this.offerId = id; } public String getChannelText() { return channelText; } public void setChannelText(String name) { this.channelText = name; } }
OfferChannelDO.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="OfferChannelDO" table="OFFER_CHANNEL"> <composite-id access="field" > <key-property name="offerId" column="OFFER_ID"/> <key-property name="channelText" column="CHANNEL_TXT" /> </composite-id> </class> </hibernate-mapping>
OfferSpecificationDO.java
import java.util.ArrayList; import java.util.List; import java.io.Serializable;
public class OfferSpecificationDO implements Serializable { private int offerId; private String offerSpecTxt; private List offerChannel = new ArrayList(); public int getOfferId() { return offerId; } public void setOfferId(int id) { this.offerId = id; } public String getOfferSpecTxt() { return offerSpecTxt; } public void setOfferSpecTxt(String text) { this.offerSpecTxt = text; } public List getOfferChannel() { return offerChannel; } public void setOfferChannel(List channelList) { this.offerChannel = channelList; } }
OfferSspecificationDO.hbm.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping> <class name="OfferSpecificationDO" table="OFFER_SPECIFICATION"> <id name="offerId" column="OFFER_ID" type="int"> <generator class="sequence"> <param name="sequence">channelgen</param> </generator> </id>
<property name="offerSpecTxt" column="OFFER_SPEC_TXT" type="string"/>
<bag name="offerChannel" inverse="true" cascade="save-update" fetch="join"> <key column="OFFER_ID"/> <one-to-many class="OfferChannelDO"/> </bag>
</class> </hibernate-mapping>
Main.java
import java.util.*;
import java.sql.*; import org.hibernate.*; import org.hibernate.criterion.*;
public class Main { public static void main(String[] args) { prepareData(); }
private static void prepareData(){ Session session = HibernateUtil.currentSession(); OfferSpecificationDO spec = new OfferSpecificationDO(); spec.setOfferSpecTxt("Specification test 1"); OfferChannelDO channel = new OfferChannelDO("channel through"); spec.getOfferChannel().add(channel);
try{ Transaction tr = session.beginTransaction(); session.save(spec); tr.commit(); session.flush(); HibernateUtil.closeSession(); }catch(Exception e){ e.printStackTrace(); } } }
log4j.properties
# Direct log messages to a log file log4j.appender.file=org.apache.log4j.RollingFileAppender log4j.appender.file.File=E:\Study\StudyHibernate\NewApp\loging.log log4j.appender.file.MaxFileSize=1MB log4j.appender.file.MaxBackupIndex=1 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n # Root logger option log4j.rootCategory=DEBUG, file # Hibernate logging options (INFO only shows startup messages) log4j.logger.org.hibernate=DEBUG # Log JDBC bind parameter runtime arguments log4j.logger.org.hibernate.type=DEBUG
log4j.logger.org.hibernate.SQL=DEBUG
HibernateUtil.java
public class HibernateUtil {
public static final SessionFactory sessionFactory;
static { try { sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } }
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; }
public static void closeSession() throws HibernateException { Session s = (Session) session.get(); if (s != null) s.close(); session.set(null); }
hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class">COM.ibm.db2.jdbc.app.DB2Driver</property> <property name="connection.url">jdbc:db2:uri</property> <property name="connection.username">id</property> <property name="connection.password">pwd</property>
<!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property>
<!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Mapping files --> <mapping resource="OfferSpecificationDO.hbm.xml"/> <mapping resource="OfferChannelDO.hbm.xml"/> </session-factory> </hibernate-configuration>
here is a trace of DEBUG and exception
17:22:52,115 DEBUG SessionImpl:220 - opened session at timestamp: 12728011720 17:22:52,115 DEBUG JDBCTransaction:54 - begin 17:22:52,115 DEBUG ConnectionManager:421 - opening JDBC connection 17:22:52,115 DEBUG JDBCTransaction:59 - current autocommit status: false 17:22:52,131 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 17:22:52,131 DEBUG SQL:401 - values nextval for channelgen 17:22:52,146 DEBUG SequenceGenerator:82 - Sequence identifier generated: 1 17:22:52,146 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 17:22:52,146 DEBUG AbstractSaveEventListener:112 - generated identifier: 1, using strategy: org.hibernate.id.SequenceGenerator 17:22:52,162 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 17:22:52,178 DEBUG SQL:401 - select offerchann_.OFFER_ID, offerchann_.CHANNEL_TXT from OFFER_CHANNEL offerchann_ where offerchann_.OFFER_ID=? and offerchann_.CHANNEL_TXT=? 17:22:52,178 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 17:22:52,178 DEBUG AbstractSaveEventListener:112 - generated identifier: component[offerId,channelText]{channelText=channel through, offerId=0}, using strategy: org.hibernate.id.Assigned 17:22:52,178 DEBUG JDBCTransaction:103 - commit 17:22:52,178 DEBUG AbstractFlushingEventListener:111 - processing flush-time cascades 17:22:52,178 DEBUG AbstractFlushingEventListener:154 - dirty checking collections 17:22:52,178 DEBUG Collections:176 - Collection found: [OfferSpecificationDO.offerChannel#1], was: [<unreferenced>] (initialized) 17:22:52,193 DEBUG AbstractFlushingEventListener:85 - Flushed: 2 insertions, 0 updates, 0 deletions to 2 objects 17:22:52,193 DEBUG AbstractFlushingEventListener:91 - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections 17:22:52,193 DEBUG Printer:83 - listing entities: 17:22:52,193 DEBUG Printer:90 - OfferChannelDO{} 17:22:52,193 DEBUG Printer:90 - OfferSpecificationDO{offerSpecTxt=Specification test 1, offerChannel=[OfferChannelDO], offerId=1} 17:22:52,193 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 17:22:52,193 DEBUG SQL:401 - insert into OFFER_SPECIFICATION (OFFER_SPEC_TXT, OFFER_ID) values (?, ?) 17:22:52,209 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 17:22:52,209 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 17:22:52,209 DEBUG SQL:401 - insert into OFFER_CHANNEL (OFFER_ID, CHANNEL_TXT) values (?, ?) 17:22:52,240 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 17:22:52,240 DEBUG JDBCExceptionReporter:69 - could not insert: [OfferChannelDO] [insert into OFFER_CHANNEL (OFFER_ID, CHANNEL_TXT) values (?, ?)] COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0530N The insert or update value of the FOREIGN KEY "OFFER_CHANNEL.CC1272784882502" is not equal to any value of the parent key of the parent table. SQLSTATE=23503
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source) at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source) at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source) at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(Unknown Source) at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(Unknown Source) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2247) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at Main.prepareData(Main.java:35) at Main.main(Main.java:13) 17:22:52,240 WARN JDBCExceptionReporter:77 - SQL Error: -530, SQLState: 23503 17:22:52,240 ERROR JDBCExceptionReporter:78 - [IBM][CLI Driver][DB2/NT] SQL0530N The insert or update value of the FOREIGN KEY "OFFER_CHANNEL.CC1272784882502" is not equal to any value of the parent key of the parent table. SQLSTATE=23503
17:22:52,240 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session org.hibernate.exception.ConstraintViolationException: could not insert: [OfferChannelDO] at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660) at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56) at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000) at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338) at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106) at Main.prepareData(Main.java:35) at Main.main(Main.java:13) Caused by: COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver][DB2/NT] SQL0530N The insert or update value of the FOREIGN KEY "OFFER_CHANNEL.CC1272784882502" is not equal to any value of the parent key of the parent table. SQLSTATE=23503
at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source) at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.throw_SQLException(Unknown Source) at COM.ibm.db2.jdbc.app.SQLExceptionGenerator.check_return_code(Unknown Source) at COM.ibm.db2.jdbc.app.DB2PreparedStatement.execute2(Unknown Source) at COM.ibm.db2.jdbc.app.DB2PreparedStatement.executeUpdate(Unknown Source) at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:23) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2247) ... 12 more
_____________________ please help me understand what am i missing/doing wrong here
insert/update value of the FOREIGN KEY is not equal to any value of parent key of parent table:DB2
|