I sent the e-mail below to Gavin last night, but I might as well send it
to the group to see if anyone else might be able to offer a tip or two.
---

I whipped up a quick example and was able to reproduce my problem on
MySQL (I was using Oracle today).  It was easy to do in my appfuse
project, so I hope you don't mind the 10MB download.

Go to http://raibledesigns.com/downloads/parent-child.zip

The README-PARENT.txt should tell you everything you need to know.
Basically, if you have $J2EE_HOME set and MySQL installed, you should be
able to get this up and running in a couple ant commands.  I've pasted
the instructions below for to make it easier:

SETUP
-----
To build this application - you must be using Ant 1.5.1 and have your 
$J2EE_HOME set to your J2EE SDK install directory.  You can also copy
the j2ee.jar file to lib/j2eesdkee1.4/lib.  J2EE 1.4 is not required
yet, so 
using a 1.3 lib is fine.  You can also pass in the parameter such as 
ant -Dj2ee.jar=/path/to/jar.

To run this application, you will need to perform the following tasks:

1.  Adjust the database.properties file to fit your system - the
username
    and password for the database must have database/table create
rights.
    It currently creates a mysql "appfuse" database using the user:
root/admin.

2.  Run "ant setup-db".   

3.  Load the database with sample parent/child data using:
    ant db-load -Dfile=parent-child.xml

4.  Test saving a parent/child composite-id using:
    ant test-ejb -Dtestcase=ParentDAO


The relevant files for this demo are all in
src/ejb/org/appfuse/persistence.  The JUnit test is at
test/ejb/org/appfuse/persistence/ParentDAOTest.java.

Thanks,

Matt

P.S.  On MySQL I get the error below when I run "ant test-ejb
-Dtestcase=ParentDAO"

    [junit] SEVERE: Invalid argument value: Duplicate entry '1-2' for
key 1
    [junit] ------------- ---------------- ---------------
    [junit] Testcase:
testSaveParent(org.appfuse.persistence.ParentDAOTest):    Caused an
ERROR
    [junit] java.sql.BatchUpdateException: Invalid argument value:
Duplicate entry '1-2' for key 1
    [junit] org.appfuse.persistence.DAOException:
java.sql.BatchUpdateException: Invalid argument value: Duplicate entry
'1-2' for key 1

> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On 
> Behalf Of [EMAIL PROTECTED]
> Sent: Monday, January 20, 2003 8:28 PM
> To: Raible, Matt
> Cc: '[EMAIL PROTECTED]'
> Subject: RE: [Hibernate] Saving Children with composite-id
> 
> 
> 
> saveOrUpdate() requires that
> 
> (1) the object has an identifier property
> (2) the identifier class implements equals() and hashCode()
> (3) unsaved-value is set correctly
> 
> I recommend that you try enabling logging to see what
> Hibernate is doing.
> 
> If you still can't find a solution, email me a simple main()
> method and I will figure out whats wrong very quickly, I'm sure.
> 
> [EMAIL PROTECTED]
> 
> 
> 
> 
>                                                               
>                                                               
>           
>                     "Raible, Matt"                            
>                                                               
>           
>                     <[EMAIL PROTECTED]>         
> To:     "'[EMAIL PROTECTED]'"             
>             
>                     Sent by:                                 
> <[EMAIL PROTECTED]>                       
>            
>                     [EMAIL PROTECTED]       
> cc:                                                           
>             
>                     eforge.net                              
> Subject:     RE: [Hibernate] Saving Children with 
> composite-id            
>                                                               
>                                                               
>           
>                                                               
>                                                               
>           
>                     21/01/03 01:03 PM                         
>                                                               
>           
>                                                               
>                                                               
>           
>                                                               
>                                                               
>           
> 
> 
> 
> 
> Is it possible that saveOrUpdate will not work for the
> children of this object, and I have to use update(obj, id) 
> instead?  Damn, I thought hibernate was easy and now it's 
> kicking my ass!  It's probably something super simple and I'm 
> just not seeing it.  To re-iterate (I know you guys hate me 
> by now), here's a brief overview of my code.
> 
> ** all classes extend from BaseObject with has equals(),
> hashCode() and
> toString() methods.
> 
> Parent.java
> ---
> private Long id;
> private List children;
> +appropriate getters/setters
> 
> Parent.hbm.xml
> ---
> <bag role="children" table="CHILDREN" cascade="all">
>     <key column="parent_id" length="22"/>
>     <one-to-many class="eg.Child"/>
> </bag>
> 
> Child.java
> ---
> private ChildId id;
> +appropriate getter/setter
> 
> ChildId.java
> ---
> private Long parentId;
> private Long recordNum;
> + appropriate getter/setters
> 
> Child.hbm.xml
> ---
> <composite-id name="id"
>     class="eg.ChildId"
>     unsaved-value="none">
>     <key-property column="parent_id" length="22"
> name="parentId" type="long"/>
>     <key-property column="record_num" length="22" 
> name="recordNum" type="long"/> </composite-id>
> 
> 
> I am able to retrieve children just fine, but when I try to
> save the parent, hibernate tries to do an insert and I get a 
> unique constraint violation.
> 
> I'm calling the following method to save everything:
> 
> public Parent saveParent(Parent p) throws DAOException {
>            storeObject(p);
>            return (Parent) retrieveObject(eg.Parent.class,
> p.getId()); }
> 
> I got the storeObject method from one of the examples
> floating around, so it could definitely be my problem:
> 
> protected void storeObject(Object obj) throws DAOException
> {
>            Session ses = null;
> 
>            try {
>                      ses = HibernateSession.currentSession();
>                      ses.saveOrUpdate(obj);
>                      ses.flush();
>                      ses.connection().commit();
>            } catch (Exception e) {
>                      try {
>                                ses.connection().rollback();
>                      } catch (Exception ex) {
>                                e.printStackTrace();
>                      }
> 
>                      ;
>                      throw new DAOException(e);
>            } finally {
>                      try {
>                                HibernateSession.closeSession();
>                      } catch (Exception ex) {
>                                ex.printStackTrace();
>                      }
> 
>                      ;
>            }
> }
> 
> Thanks,
> 
> Matt
> 
> 
> 
> 
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something
> 2 See! http://www.vasoftware.com 
> _______________________________________________
> hibernate-devel mailing list [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> 
> 
> 
> 
> **********************************************************************
> Any personal or sensitive information contained in this email
> and attachments must be handled in accordance with the 
> Victorian Information Privacy Act 2000, the Health Records 
> Act 2001 or the Privacy Act 1988 (Commonwealth), as applicable.
> 
> This email, including all attachments, is confidential.  If
> you are not the intended recipient, you must not disclose, 
> distribute, copy or use the information contained in this 
> email or attachments.  Any confidentiality or privilege is 
> not waived or lost because this email has been sent to you in 
> error.  If you have received it in error, please let us know 
> by reply email, delete it from your system and destroy any copies.
> **********************************************************************
> 
> 
> 
> 
> 
> -------------------------------------------------------
> This SF.NET email is sponsored by:
> SourceForge Enterprise Edition + IBM + LinuxWorld = Something
> 2 See! http://www.vasoftware.com 
> _______________________________________________
> hibernate-devel mailing list [EMAIL PROTECTED]
> https://lists.sourceforge.net/lists/listinfo/hibernate-devel
> 




-------------------------------------------------------
This SF.NET email is sponsored by:
SourceForge Enterprise Edition + IBM + LinuxWorld = Something 2 See!
http://www.vasoftware.com
_______________________________________________
hibernate-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/hibernate-devel

Reply via email to