-->
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.  [ 7 posts ] 
Author Message
 Post subject: Querying from subclass class in joined-subclass
PostPosted: Thu Feb 09, 2006 12:07 am 
Newbie

Joined: Wed Feb 08, 2006 11:45 pm
Posts: 4
<class name="header" table="definition">
<composite-key >
<property name="id" >
<column name="id"/>
</property>
<property name="origid">
<column name=oridid">
</composite-key>
<property name...

...
<joined-subclass name="assignedheader" table="assignment">
<key>
<column..
</key>
<component name="assignedkey" class="baseoid">
<property name="id" >
<column name="id"/>
</property>
<property name="origid">
<column name=oridid">
</property>
...
..
I want to save only subclass from the above mapping,i don't want to save super class object.The keys are composite in each and every table, they represent primary keys to their corresponding tables.The above joined sublass table has foriegn composite key from the super class table and also have it's own composite key.

When i am saving to subclass table , it is comparing the keys from the super class to sub class in hibernate generated query.(which are not all related).How can save only subclass if i have super class.How can i do make it work with mapping.I wrote another mapping(wth just subclass mapping to it's table) with different entity name for storing and it did not work because i was reading the database from the above mapping, it 's poiting to the above mappig even though i change the entity name in criteria while storing.IS there any other way to do it?.Retrieval part is fine with above mapping , but storing did not work as i expect to.The error is
constraint voliation.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 12:35 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Quote:
I want to save only subclass from the above mapping,i don't want to save super class object.

As I pointed out in your other thread: this is not possible. If an object is mapped to two tables (as it is when it is a joined-subclass), then saving the object writes to both tables. Redesign your mapping to use a different relationship, possibly a one-to-one or many-to-one unique="true".


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 1:03 am 
Newbie

Joined: Wed Feb 08, 2006 11:45 pm
Posts: 4
[quote="tenwit"][quote] I want to save only subclass from the above mapping,i don't want to save super class object.[/quote]
As I pointed out in your other thread: this is not possible. If an object is mapped to two tables (as it is when it is a joined-subclass), then saving the object writes to both tables. Redesign your mapping to use a different relationship, possibly a one-to-one or many-to-one unique="true".[/quote]


That is ok, but that problem here is that if i have a <many-to-one> relationship in assignedheader class in mapping i have to map a property from this class which is a type of super class.
in java class i need

private Header header;
gettet and setter method for this.
(why do i need this if i am subclassing assignedheader to header)

<class name="assignedheader"

<property...
..</property>
...
<many-to-one name="header">
<column>deinfitiid</column>
<column>origid</column>
</many-to-one>
</class>
Ofcourse this works..(this was existing earlier we are changing so that in object oriented it can make some sense) if i use above relationship the inheritance goes away and assignedheader holds a reference to header.
Again assignedheader is sublass of header.why do i need a reference to super class again with a relationship.Can't i access the super class properties from subclass.Even with <join> i had some problems.Hibernate cannot understand inheritance?.How about having same class different mapping with entity-name . this did not work when i specified in criteria while trying to save. Thanks alot for your answers.What is your suggestion about subclassing in java class hierarchy(not in mapping)?.If the hibernate mapping doesn't help with our design , i have to think about alternative.
I believe that it is our datamodel that appears to be screwed a bit.Class hierarchy seems to be fine.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 4:50 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want to have your AssignedHeader java class be a subclass of Header, but still want to be able to save your AssignedHeader properties without saving your Header properties, then you probably want to use the delegation pattern. Have the java class that implements AssignedHeader have a private Header member; it then implements the Header methods by calling them on the private Header member. Something to this effect:
Code:
/* AssignedHeader interface extends Header interface */
public class AssignedHeaderImpl implements AssignedHeader
{
  private HeaderImpl header;
  private int assignedHeaderProperty = 0;

  public int getHeaderProperty()
  {
    return header.getHeaderProperty();
  }
  public int getAssignedHeaderProperty()
  {
    return assignedHeaderProperty;
  }

  private HeaderImpl getInternalHeader()
  {
    return header;
  }
  private void setInternalHeader(HeaderImpl h)
  {
    header = h;
  }
}


In your hibernate mapping, have a one-to-one (or many-to-one unique="true", as appropriate) mapping from AssignedHeaderImpl to HeaderImpl, and call the mapping "InternalHeader". Only hibernate will ever call setInternalHeader or getInternalHeader. Your normal API will use the methods from the Header interface, delegated by AssignedHeaderImpl to HeaderImpl.

Hope that's of use to you.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 09, 2006 11:48 pm 
Newbie

Joined: Wed Feb 08, 2006 11:45 pm
Posts: 4
[quote="tenwit"]If you want to have your AssignedHeader java class be a subclass of Header, but still want to be able to save your AssignedHeader properties without saving your Header properties, then you probably want to use the delegation pattern. Have the java class that implements AssignedHeader have a private Header member; it then implements the Header methods by calling them on the private Header member. Something to this effect:[code]/* AssignedHeader interface extends Header interface */
public class AssignedHeaderImpl implements AssignedHeader
{
private HeaderImpl header;
private int assignedHeaderProperty = 0;

public int getHeaderProperty()
{
return header.getHeaderProperty();
}
public int getAssignedHeaderProperty()
{
return assignedHeaderProperty;
}

private HeaderImpl getInternalHeader()
{
return header;
}
private void setInternalHeader(HeaderImpl h)
{
header = h;
}
}[/code]

In your hibernate mapping, have a one-to-one (or many-to-one unique="true", as appropriate) mapping from AssignedHeaderImpl to HeaderImpl, and call the mapping "InternalHeader". Only hibernate will ever call setInternalHeader or getInternalHeader. Your normal API will use the methods from the Header interface, delegated by AssignedHeaderImpl to HeaderImpl.

Hope that's of use to you.[/quote]


Hi, i don't know what your name is, Thanksalot.I have some questions on this.so when i access from interface AssignedHeader , i will have access to all properties from AssignedHeader and Header interfaces through AssignedHeaderImpl right?.Do i need to have HeaderImpl(which implements only header interface) implementation
separately from AssignedHeaderImpl ?.

In Mapping i will have like
<class name="AssignHeaderImpl">
....
<many-to-one name="internalheader>
<Column..
</many-to-one>
</class>

<class name="HeaderImpl">
<properite of all headers..
</class>

So no need of mapping to interfaces right? and also if i call like
Header = new HeaderImpl();
i should be able to read only Header table right?.
I just want to confirm this.Iam wondering if this allows me to do following?.

1) Can i able to read header table alone with out going to assigned header?
2) Can i able to readHeader and Assigned Header both at a time ?.
3)No need of mappings to interfaces right?.
4)Do i need to have separate property variables for Header primary keys in AssignedHeader interface,becoz i want to store them in AssignedHeader
table only while saving.
5)Can i just read and write (both tables)just using one object?.
Header = new AssignedHeaderImpl()
(or)
AssignedHeader = new AssignedHeaderImpl();


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:15 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
sleepwithhibernate wrote:
Hi, i don't know what your name is, Thanksalot.

That would be "tenwit" :) You're welcome, glad to help.

sleepwithhibernate wrote:
I have some questions on this.so when i access from interface AssignedHeader , i will have access to all properties from AssignedHeader and Header interfaces through AssignedHeaderImpl right?.Do i need to have HeaderImpl(which implements only header interface) implementation separately from AssignedHeaderImpl ?.

Yes, you will have access to all properties when using AssignedHeader. Have the AssignedHeader interface extend the Header interface: that will require that your AssignedHeaderImpl class implements all the methods in both interfaces. Yes you have to have two separate implementations, HeaderImpl and AssignedHeaderImpl. Do not have AssignedHeaderImpl extend HeaderImpl, that defeats the purpose of the delegation pattern that you're using. You want to make it look like AssignedHeaderImpl extends HeaderImpl, but it won't, really.

sleepwithhibernate wrote:
In Mapping i will have like
<class name="AssignHeaderImpl">
....
<many-to-one name="internalheader>
<Column..
</many-to-one>
</class>

<class name="HeaderImpl">
<properite of all headers..
</class>

So no need of mapping to interfaces right?

Exactly right.

sleepwithhibernate wrote:
and also if i call like
Header = new HeaderImpl();
i should be able to read only Header table right?.
I just want to confirm this.

Yes, because your mapping of HeaderImpl will make no reference to AssignedHeaderImpl. Your AssignedHeaderImpl will have a reference to HeaderImpl (via a one-to-one or many-to-one unique="true" relationship): when you load an AssignedHeader, the corresponding Header will be loaded (eagerly or lazily, however you define it in your mapping), but if you don't dirty that Header, it won't be written when the AssignedHeader is written.

sleepwithhibernate wrote:
Iam wondering if this allows me to do following?.

1) Can i able to read header table alone with out going to assigned header?

Yes, using something like
Code:
Header header = (Header) session.load(HeaderImpl.class, headerId);


sleepwithhibernate wrote:
2) Can i able to read Header and Assigned Header both at a time ?.

Yes, any time you load an AssignedHeader you'll get the corresponding Header. That Header might be lazily loaded, meaning that it won't get fetched from the DB if it's not needed.

sleepwithhibernate wrote:
3)No need of mappings to interfaces right?.

That's right. For self-documentation purpose, I always mention the interfaces in the mapping file, but it's purely for my own benefit:
Code:
<class name="HeaderImpl" table=...>
  <meta attribute="implements">
    package.Header
  </meta>
  ...
</class>

sleepwithhibernate wrote:
4)Do i need to have separate property variables for Header primary keys in AssignedHeader interface,becoz i want to store them in AssignedHeader
table only while saving.

No, you do not want to have separate variables in AssignedHeaderImpl for the properties in HeaderImpl. You must have all the accessors and mutators (getters and setters) in AssignedHeader/AssignedHeaderImpl: however, the methods that are backed by HeaderImpl just called the HeaderImpl methods.

sleepwithhibernate wrote:
5)Can i just read and write (both tables)just using one object?.
Header = new AssignedHeaderImpl()
(or)
AssignedHeader = new AssignedHeaderImpl();

Yes, that's exactly what the pattern is for. AssignedHeaderImpl gets its non-Header properties from the AssignedHeaderImpl mapping, but it asks its own private HeaderImpl object (and therefore, the HeaderImpl mapping) for the non-AssignedHeader properties.

There is obviously some code duplication when using the delegation pattern: the accessors and mutators from the delegated class are repeated in the delegating class. However, this is better than trying to force a "kind-of" relationship into a situation that is best desribed by a "has-a" relationship.

As a final request, can I ask you to preview your posts and ensure that your quote tags are matched? And also to use code tags when appropriate. Legibility = answerability, in my book.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 10, 2006 12:35 am 
Newbie

Joined: Wed Feb 08, 2006 11:45 pm
Posts: 4
Hi TenWit,
I will propose this to my superiors and see what they think of this.I have been fighting with this mapping thing day and night from 3 days.
We have several other classes need to be attached to these classes.Let me see how it goes..I will let you know tomorrow.I will do whatever you said from my next post.


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