Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

one-to-many relation

 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have 2 tables category and product. they have one-to-many relation in them.

Category : cat_id(PK),cat_name,cat_desc
Product : prod_id(PK),prod_name,prod_desc,cat_id(FK)

As both have one to many relation so i have declared a set in Category Table.



i have also described the relation in Category.hbm.xml file



And in Product i have made



and in Product.hbm.xml file i have set



I have created the object of Category and put the objects of products in it.
When i execute session.Save(catObj) method It throws exception. It execute the insert query of Category but for product it execute the Update query. Why? what i am doing wrong?

the exception is as follows:

Hibernate: insert into category (cat_name, cat_desc, cat_id) values (?, ?, ?)

Hibernate: update testprod set prod_name=?, prod_desc=?, cat_id=? where prod_id=?

15:32:27,996 ERROR SessionImpl:2368 - Could not synchronize database state with session

net.sf.hibernate.HibernateException: SQL insert, update or delete failed (row not found)

why is it calling update for products? I want to insert all the product in database with the category against that category
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think ,

in your code is like this


if your code is like above code, you should change to



if your code not like , please post your code ....
 
Ali Gohar
Ranch Hand
Posts: 572
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi somkiat puisungnoen,

Thanks for your reply i have used the session.save method. Following is the code:


any idea???
 
Karel Vervaeke
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're saying your id has unsaved-value=null,
you should make sure that the java-type of this id is nullable.
int id; // is not nullable.

You should either
1) specify an unsaved-value (e.g. -1) and make sure you set the id to -1 when constructing the persistent object.

/or/
2) use an Integer id; (default=null)

HTH
 
Tomas Zalusky
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had encountered the same error when I was experimenting with Hibernate and MS SQL Server. The problem in my application was caused by JTDS driver and by the fact that the referential integrity was maintained using _triggers_. JTDS driver returns bad value of count of rows affected by insert (it is caused by trigger or bug in driver or both . So in my case the inserting was _successful_, but the zero value returned by executeUpdate caused a HibernateException (see source of NonBatchingBatcher), rollback of Hibernate transaction and rollback of JDBC transaction. After upgrading driver to 0.8.1 the problem disappeared. There is another advice in Hibernate FAQ based on setting the lastUpdateCount parameter, but it didn't help me. Anyway I recommend you to launch SQL insert in a small java-JDBC program without using Hibernate, it helped me a lot to locate the problem.

Hope this helps
Tomas
[ August 11, 2004: Message edited by: Tomas Zalusky ]
 
David Sissoko
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I have exacly the same problem. I use Oracle database (9i) and for the fisrt time i create an object with a list attribute, hibernate generates an update instead of a create !!
i hope that since the 1st post, someone has found the solution
 
Yashnoo lyo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I come across the same problem. Why? Could some one solve this problem?
java code:

Exception:



Help!
 
Arpit Todi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting same problem while using Hibernate from Spring Framework.
I have Position to Employee as one to many relationship.It is giving me same exception when i try to insert positions from Employee objects.I had tried all option like cascade="save-update" and cascade="all".
Can somebody help me!!

Thanks,
Arpit
 
srs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arpit Todi:
I am getting same problem while using Hibernate from Spring Framework.
I have Position to Employee as one to many relationship.It is giving me same exception when i try to insert positions from Employee objects.I had tried all option like cascade="save-update" and cascade="all".
Can somebody help me!!

Thanks,
Arpit
 
srs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I am also facing the same problem. An insert statement is executed for a parent record whereas an update statment for a child record.
If any one has solved it pls reply. Itz very urgent.
Thanx in advance
 
Loren Rosen
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Most common cause of this is using an assigned value for the id. See for example this topic in the Hibernate forums:
http://forum.hibernate.org/viewtopic.php?t=933496
 
srs
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the generator class as an increment.
Here is the tag which i am using.
<generator class="increment" />

Rgds
srs
 
ojas mulay
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I encountered the same problem.But from what i tried i think the following 2 things work:

1.If ur id is primitive use unsaved-valu or else use wrapper class.
2.the generatoe class should be increment.

& it works!!
 
Mr McLaren
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is my first post here so go easy on me!

I found this discussion very helpful as I had exactly the same problem.

My unique identifier is a string, it is assigned and I don't want it to be generated any other way.

I found a solution in the
Hibernate Users FAQ.



saveOrUpdate(), or cascade save, executes UPDATE instead of INSERT!

There are two reasons why this could occur:

  • The <id> element does not specify the correct unsaved-value
  • You are using an assigned identifier or a composite identifier


  • In the first case, you should simply specify the correct unsaved-value.

    In the second case, you have several choices. In Hibernate 2.1 either:

  • specify unsaved-value for the <version> or <timestamp> property
  • implement Interceptor.isUnsaved() and Interceptor.onSave(), as described in chapter 16 of the reference documentation




  • So for the above example a simple solution would be to add a timestamp element after the <id> section of the mapping file.

    E.g.



    Granted, this would add an additional column in the database table but at least it would now work with assigned generators.

    HTH

    Mark
    [ February 16, 2005: Message edited by: Mr McLaren ]
     
    Patrick Bernardina
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm facing the same problem.

    The foreign key in the child table is part of the primary-key (composite-key).

    In this case, how do I solve my problem?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic