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.