I am using DB2 database and hibernate framework. There are two tables such as Word and Litter and their relationship is one to many.
Schema A:
Table Word: ID, IDLIT -> references to ID of Litter table Name
Schema B:
Table Litter ID, Short_Name, Long_Name.
Both tables are located in two different schemas but in one database. I have an POJO and XML mapping files to map the tables. Now how can I save the objects into database while handling the many to one relationship(Litter - Word)? Is the any suggestion? If yes please provide me with detailed instructions, thanks in advance!
Updated question
Also I have changed because my mapping xml files different for each POJO class. Here is the code for running:
Session session = HibernateUtility.getSessionFactory().openSession();
session.beginTransaction();
Word word = new Word();
Word word1 =new Word();
Litter litter = new Litter();
litter.setFullname("bla bla");
word.setLitter(litter);
word1.setLitter(litter); //Here I have to handle one to many relationship
Set Words =new HashSet();
Words.add(word);
Words.add(word1);
litter.setWords(words);
session.save(litter);
session.save(word);
session.save(word1);
session.getTransaction().commit();
session.close();
But this code giving an exception: JDBCExceptionReporter logExceptions Is the approach which I am using is correct?
Stack trace------------------ INFO: schema update complete Hibernate: select max(ID) from LITTER сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -204, SQLState: 42704 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=BIMASH.LITTER, DRIVER=3.64.104 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -204, SQLState: 42704 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=BIMASH.LITTER, DRIVER=3.64.104 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions WARNING: SQL Error: -727, SQLState: 56098 сен 25, 2012 7:33:42 PM org.hibernate.util.JDBCExceptionReporter logExceptions SEVERE: DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-204;42704;BIMASH.LITTER, DRIVER=3.64.104 Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not fetch initial value for increment generator at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:107) at org.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:44) at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:99) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187) at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172) at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27) at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70) at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523) at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519) at Main.main(Main.java:73)
code sample
. See Markdown help. – Xavi López