-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: how to insert child if parent already exists
PostPosted: Wed Oct 22, 2008 11:37 pm 
Newbie

Joined: Wed Oct 22, 2008 11:23 pm
Posts: 2
I have a parent class Person which maps table Person(pid). I have a subclass called User which maps table User(pid, username). Their mapping is 'joined-subclass'. Now, i have one record in Person, say pid=2. I want to insert a new record User(2,'test').

however, if I do like this:

Person p = session.load(Person.class, pid);
User u = new User();
u.setPid(p.getPid());
u.setUsername("test");
session.save(u);

I got this error:
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session:

if i do session.merge(u), i got this error:
org.hibernate.WrongClassException: Object with id: null was not of the specified subclass:


Any idea?

Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

[b]Hibernate version:[/b]

[b]Mapping documents:[/b]

[b]Code between sessionFactory.openSession() and session.close():[/b]

[b]Full stack trace of any exception that occurs:[/b]

[b]Name and version of the database you are using:[/b]

[b]The generated SQL (show_sql=true):[/b]

[b]Debug level Hibernate log excerpt:[/b]


Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 23, 2008 12:24 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
According to your client code, I am not sure polymorphism is the way to go in this case.

Person and User, even though in Java they might be subclasses, are being treated as concrete subclasses in your Java example.

You can either treat them as a class superclass relationship (possibly with Person Being abstract, or, in your case, with a "table-per-class hierarchy" or "table-per-concrete-subclass" mapping with Person being a concrete class)

or

treat them as a Parent-child relationship, with person "having a User" and taking the id from him, or vice versa, mapped with a "generator foreign".

But you cannot mix both strategies, as presented in your post.

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.