1

I am using DB2 database and hibernate framework. There are two tables such as Word and Litter and their relationship is one to many.

  1. Schema A:

    Table Word: ID,
    IDLIT -> references to ID of Litter table
    Name
    
  2. 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)
3
  • 1
    Can you please post the relevant part of the stack trace and the mapping of the association? Commented Sep 25, 2012 at 12:56
  • <hibernate-mapping > <class name="pojopackage.Word" table="Word" schema ="A" > </class></hibernate-mapping> <hibernate-mapping > <class name="pojopackage.Litter" table="Litter" schema ="B" ></class> </hibernate-mapping> how can i display <> tags in the question area? Commented Sep 25, 2012 at 13:27
  • 1
    just use the tags in code snippets. Surround them with ` or just select and click code sample. See Markdown help. Commented Sep 25, 2012 at 14:04

1 Answer 1

0

You can define the schema a mapped class belongs to with the schema attribute of the <class> element in the mapping file. Notice this will override the setting in the general <hibernate-mapping>'s schema attribute.

<!-- A is set to be the default schema -->
<hibernate-mapping schema="A">
    <class name="mypackage.Word" table="WORD" >
    ....
    </class>

    <!-- Overriding the schema definition for this class -->
    <class name="mypackage.Litter" table="LITTER" schema="B">
    ....
    </class>
</hibernate-mapping>

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 the select max(ID) from LITTER query), you'll also have to specify the schema in the <generator> so that the issued query transforms into select max(ID) from B.LITTER. You can achieve that with a <param> named schema in the <generator>:

<generator class="increment">
    <param name="schema">B</param>
</generator>

For more details see this almost duplicate question: Hibernate: ID generator using increment and Oracle Schema

Sign up to request clarification or add additional context in comments.

8 Comments

I am new to hibernate, therefore should I create two hibernate.cfg.xml? I have created classes through reverse engineering which I have two file Word.java and Word.hbm.xml and Iam using Netbeans. What else files should I change?
If you've used Hibernate Tools to do the reverse engineering, you can specify which schemas to reverse engineer with the <schema-selection> option in the hibernate.reveng.xml file. Alternatively, just use the schema attribute in the <hibernate-mapping> or <class> tag of the Entity that is not in the default schema.
i am grateful for your effort, I have changed schema for hibernate-mapping tag of Word, but its giving an exception, i have updated the question. Can you have a look?
Have you tried adding the schema attribute in the <class> element instead?
yeap, I have putted 'schema' attribute in the '<class>' element. Here the another exception: SQLGrammarException: could not fetch initial value for increment generator
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.