-->
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.  [ 5 posts ] 
Author Message
 Post subject: Do I really need Child and childId present in Parent ?
PostPosted: Thu Apr 02, 2009 6:17 am 
Newbie

Joined: Thu Apr 02, 2009 5:40 am
Posts: 6
Hibernate version:3.3.1 GA

Hello,

I have an odd theoretical problem.

I have this situation:
bean Parent and Child.

class Parent {

Child c;

setChild(c) {
this.c = c;
}

Child getChild() {
return c;
}
}

My mappings:

<class name="org.myproject.Parent" entity-name="Parent" table="PARENT_TABLE">
<id name="parentId" column="PARTENTID">
<generator class="native" />
</id>
<many-to-one name="child" entity-name="Child" column="CHILDID" not-null="true" />
{....other attributes}
</class>

<class name="org.myproject.Child" entity-name="Child" table="CHILD_TABLE">
<id name="childId" column="CHILDID">
<generator class="native" />
</id>
{....other attributes}
</class>

There are functionalities in my application that boiles down to having selects where I need only a condition on PARENT_TABLE.CHILDID = ?.
For example:
When I need to put the condition on Parent.childId = -999; where -999 might be a sort of void id, like a flag for a parent
with a fake child.
I this case the select will be something like this:
select * from PARENT_TABLE P inner join CHILD_TABLE c on p.CHILDID = c.CHILDID where c.CHILDID = -999 but actually I need something like this:
select * from PARENT_TABLE p where p.CHILDID = -999.


How can I achive this, as my entity Parent doesn't have a primitive attribute mapped exactly on CHILDID column but only the relation many-to-one
with the Child table through the Child object.


I solved this problem by adding childId to Parent object, and adding the coresponding mapping, but I think this is not the real solution.

class Parent {

Child c;
int childId;

setChild(c) {
this.c = c;
}

Child getChild() {
return c;
}

setChildId(childId) {
this.childId = childId;
}

int getChildId() {
return childId;
}

}

My mappings:

<class name="org.myproject.Parent" entity-name="Parent" table="PARENT_TABLE">
<id name="parentId" column="PARTENTID">
<generator class="native" />
</id>
<property name="childId" column="CHILDID" />
<many-to-one name="child" entity-name="Child" column="CHILDID" not-null="true" />
{....other attributes}
</class>


Thanks


Top
 Profile  
 
 Post subject: Hello again
PostPosted: Fri Apr 03, 2009 8:45 am 
Newbie

Joined: Thu Apr 02, 2009 5:40 am
Posts: 6
Hello again,

It's quit urgent... can someone tell me how can I do what I've asked in my last post ?

So... my problem is that I need sometimes to search a Parent by its childId having in mind that Parent and Child have a one to one or one to many realtion but that condition on childId to be invoked only on Parent's Table.

All that I stated above in the context that Parent doesn't have an attribute childId but only an Object Child.

Can I achive this ?


Many thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 9:05 am 
Newbie

Joined: Thu Apr 02, 2009 1:06 pm
Posts: 6
According to my Idea ,Please Refer this Mappings,Here i used one-to-one it will try to solve this problem....


<class name="org.myproject.Parent" entity-name="Parent" table="PARENT_TABLE">
<id name="parentId" column="PARTENTID">
<generator class="native" />
</id>
<one-to-one name="child" class="org.myproject.Child" property-ref="parentId" cascade="all"/>
{....other attributes}
</class>

<class name="org.myproject.Child" entity-name="Child" table="CHILD_TABLE">
<id name="childId" column="CHILDID">
<generator class="native" />
</id>
{....other attributes}
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 03, 2009 10:09 am 
Newbie

Joined: Thu Apr 02, 2009 5:40 am
Posts: 6
Thanks dayana but this doesn't solve my problem.
Child is realted with parent through another column than PARENTID, and that column is CHILDID, a column from PARENT_TABLE.


Top
 Profile  
 
 Post subject: The problem was of another nature
PostPosted: Fri Apr 03, 2009 12:22 pm 
Newbie

Joined: Thu Apr 02, 2009 5:40 am
Posts: 6
Ok, my problem was another one:

I have a classic relation Parent - Child and I want to retrieve a Child
but without its Parent, but I would like the Child to know it's parent Id.

More exactly if Child would be:


public class Child {

Parent parent;

setParent

getParent

}

the Child returned by Hibernate would have an Parent object not retrieved from the database after an inner join but an object having only it's parentId form the Child Table;


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.