File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes one-to-many relation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one-to-many relation" Watch "one-to-many relation" New topic
Author

one-to-many relation

Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
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

Joined: Jul 04, 2003
Posts: 1312
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 ....


SCJA,SCJP,SCWCD,SCBCD,SCEA I
Java Developer, Thailand
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
Hi somkiat puisungnoen,

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


any idea???
Karel Vervaeke
Greenhorn

Joined: Jul 20, 2004
Posts: 4
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

Joined: Aug 11, 2004
Posts: 1
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

Joined: Oct 22, 2004
Posts: 1
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

Joined: Feb 17, 2003
Posts: 152
I come across the same problem. Why? Could some one solve this problem?
java code:

Exception:



Help!


When I face C# and Java I choose Java.
Arpit Todi
Greenhorn

Joined: Nov 03, 2004
Posts: 2
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

Joined: Nov 23, 2004
Posts: 3
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

Joined: Nov 23, 2004
Posts: 3
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

Joined: Feb 12, 2003
Posts: 156
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

Joined: Nov 23, 2004
Posts: 3
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

Joined: Dec 08, 2004
Posts: 1
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

Joined: Feb 16, 2005
Posts: 1
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

    Joined: Jul 18, 2005
    Posts: 7
    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?
     
    jQuery in Action, 2nd edition
     
    subject: one-to-many relation