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)
You can define the schema a mapped class belongs to with the
schemaattribute of the<class>element in the mapping file. Notice this will override the setting in the general<hibernate-mapping>‘sschemaattribute.The following Hibernate reference chapter might be worth a read: Chapter 5. Basic O/R Mapping, specially the sections about the
<hibernate-mapping>and<class>elements.UPDATE
Seeing the stack trace, it seems that a
<generator class="increment">is being used in the class. If it’s the case (it seems so by theselect max(ID) from LITTERquery), you’ll also have to specify the schema in the<generator>so that the issued query transforms intoselect max(ID) from B.LITTER. You can achieve that with a<param>namedschemain the<generator>:For more details see this almost duplicate question: Hibernate: ID generator using increment and Oracle Schema