Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

Hibernate Help Needed

I'll try the approach that Lance used to solve my Hibernate problem. First, I'll explain the problem. In two days, if I haven't solved it - I'll bash on Hibernate and say it s***s (can't do it yet - right ;-), and then I'll claim it rocks once I get my problem solved. Hmmm, it took Lance 4 days - I have 3 before my deadline (Thursday). Luckily, I know of workarounds already (I just want to do things the recommended way) - therefore, I should never have to bash on Hibernate.

My problem is basically that I have a Parent object and it has a one-to-many relationship to a Child object. This is usually a very simple thing to configure in a Parent.hbm.xml file. To make things complicated, my Child object has a composite-id which is another class. I can retrieve information just fine, but when I try to save the Parent, Hibernate tries to insert a new Child. Here's the message I sent to the hibernate-devel mailing list:

** 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();
        }

        ;
    }
}

As always, any help is greatly appreciated.

Posted in Java at Jan 20 2003, 07:17:41 PM MST 8 Comments
Comments:

Have you tried setting the unsaved-value to use 'null' rather than 'none'? I am just beginning with Hibernate also, but it would seem to make sense that unless the child's id (as composed by the ChildId class) is null, an insert should occur. Otherwise perform the save.

Posted by Matthew E. Porter on January 20, 2003 at 09:34 PM MST #

I've tried setting it to "none", "any" and "null" and none have worked. I'm going to see if I can whip up a quik example for Gavin and see if I can reproduce it. Knowing my luck, it will work in my test case.

Posted by Matt Raible on January 20, 2003 at 10:56 PM MST #

unsaved-value="null" makes no sense for composite-ids or any other kind of "assigned" id. Thats because the identifier property is *never* null when Hibernate gets to it. Now, I'm assuming that what you *really* want is for an update() to be cascaded from Parent to Child. In that case (1) call update() on the Parent (2) Have the collection configured with cascade="all" (3) have the Child mapped with unsaved-value="none" Then it should work just right. HOWEVER: I have just spotted a bug in Hibernate 1.2.x that causes unsaved-value to be ignored for composite-ids. A simple oversight in the map parsing code. So I apologise if you wasted time on this it was a staightforward little bug.

Posted by Gavin on January 21, 2003 at 08:20 AM MST #

Has this been fixed in CVS - or can you tell me where to fix it in 1.2.2? Is it large enough to warrant a 1.2.3 release ;-) > A simple oversight in the map parsing code. > > So I apologise if you wasted time on this it was > a staightforward little bug. I don't consider it a waste of time if I can get a fix today - I'd rather it be a bug than me an idiot :-) I'm glad I found it and was able to reproduce it - hopefully making Hibernate easier to use for someone else. Thanks!

Posted by Matt Raible on January 21, 2003 at 08:42 AM MST #

Man, did that first paragraph feel like a body slam! ;-) Deserved, of course... I wish I could retract my Velocity Sucks post, but I feel that would be an unethical whitewashing/censorship thing. It seems that the JavaBlog community has learnt a lesson of sorts from my vile temper, though.

Posted by Lance on January 21, 2003 at 09:45 AM MST #

Lance - I didn't mean it as an insult. I actually thought it was a good idea - it worked right?! ;-)

Posted by Matt Raible on January 21, 2003 at 10:33 AM MST #

While I attribute Jon, I think I would have come up with it anyway, so I don't think it was anything more than venting. And I don't think it helped my "persona" any. Then again, Andy gets away with vitriol all the time...

Posted by Lance on January 21, 2003 at 09:20 PM MST #

How do you write the apporiate query for a map with a compsite key ... My query seems to be goign to the void.

Posted by Doug Pham on July 27, 2003 at 11:01 AM MDT #

Post a Comment:
  • HTML Syntax: Allowed