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: mapping question
PostPosted: Thu May 20, 2004 8:12 am 
Newbie

Joined: Wed May 19, 2004 11:50 pm
Posts: 9
I have to map a class Account


class Account{

String number
double balance
.....
Pointer customer
Pointer jointcustomer
Pointer agent
}


Pointer here represents a class with two attributes (id,name)
On a webpage when the user clicks on the agent Pointer it navigates away to the agent details.
pointer is a object used to hold references to other things. The UI tier knows how to interpret this and provide display and hyperlinks.

select number, balance...... ,
customer.id, customer.name, (Pointer is construted using these)
agent.id, agent.name
from ..... customer, agent ....


In my code currently I have

Account= new Account();
account.setAmount(results.getDouble("amount"));
String id = results.getString("customer.id");
....
account.setCustomer ( new Pointer(id, name););


ManytoOne relationship from Account to customer.
ManytoOne relationship from Account to jointcustomer.
ManytoOne relationship from Account to agent.





I am trying to use Hibernate to refactor existing code which is working fine . My objective is to keep the same object so that other tiers are not affected.


How do I map this ? (specificallyPointer since the tables and columns change accourding to the usage)




<class name="Account" table="Account" >
......
...
<many-to-one name="customer" column="cust_id" not- null="true" />
<many-to-one name="agent" column="agent_id" not- null="true" />
.......
....

<class name="Pointer" table="Customer">
<id name="Id" column="cust_id" >
<generator class="native"/>
</id>
<property name="Name" column="cust_name"/>

</class>


<class name="Pointer" table="Agent">
<id name="Id" column="agent_id" >
<generator class="native"/>
</id>
<property name="Name" column="agent_name"/>

</class>



thanks for the help


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 20, 2004 9:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Unfortunately, in the current releases of Hibernate what you would have to do to acheive this is define subclasses of your Pointer class for each type of entity it can reference and then map those subclasses. That would look something like:

Code:
<class name="Account" table="Account" >
    ...
    <many-to-one name="customer" column="cust_id" class="CustomerPointer" not-null="true" />
    <many-to-one name="agent" column="agent_id" class="AgentPointer" not-null="true" />
</class>

<class name="CustomerPointer" table="Customer">
    <id name="id" column="cust_id">
        <generator class="native"/>
    </id>
    <property name="name" column="cust_name"/>
</class>

<class name="AgentPointer" table="Agent">
    <id name="id" column="agent_id">
        <generator class="native"/>
    </id>
    <property name="name" column="agent_name"/>
</class>

Assuming you would also have independent mappings for Customer and Agent, this is essentially the "light weight" pattern described in the Hibernate patterns section of the site. Check that out for limitations to this approach.

In general, Hibernate works better if you utiize a (for lack of a better term) normalized domain model. So what if you did something like this instead:

Code:
class Account {
    private String number;
    private double balance;
    private Customer customerRef;
    private Customer jointCustomerRef;
    private Agent agent;

    // normal get/set pairs
    ...

    // accessors for UI Pointers...
    public Pointer getCustomer() {
        if ( getCustomerRef() == null ) {
            return null;
        }
        else {
            return new Pointer( getCustomerRef().getId(), getCustomerRef().getName() );
        }
    }
    ...
}


Or even, better have a base class used by all your domain entities which forces subclasses to define a getPointer() method. That way you'd be able to centralize the way a Pointer looks for a given entity. Then the getCustomer() pointer accessor method above could just be:

Code:
    public Pointer getCustomer() {
        return getCustomerRef() == null
                ? null
                : getCustomerRef().getPointer();
        }


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.