-->
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: how to map column if there is no relationship??
PostPosted: Fri Jan 20, 2006 1:24 pm 
Beginner
Beginner

Joined: Thu Jan 12, 2006 7:38 pm
Posts: 25
Hi All,

I dont know how to map columns if there is no relations ship.

table1 contains clientid, clientname, status,customerid

table 2 contains customerid, customername

i want to display table1 columns, with customer name but there is no relationship in these 2 tables. so how will u map the customer name ???

i define like these. and geter and setter
private Table2 customer;

in Table1 mapping file in xml i mapped like
<property name="customer" type="com.test.model.Table2" column="CUSTOMER_ID"/>

but i am not getting values it shows could not read column value from result set
how to get customer name??????


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 6:12 pm 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
You need to post you mapping files and the full exception. You should have 2 mapping files and customer_id would be an Id on the customer. By the way, you say customerid on the DB description but customer_id in the file. Please post code and mapping files and you'll get a more specific answer.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 6:29 pm 
Beginner
Beginner

Joined: Thu Jan 12, 2006 7:38 pm
Posts: 25
FIRST MAPPING FILE:

<hibernate-mapping package="com.test.model">

<class name="Customer" table="CUSTOMER" discriminator-value="Customer">
<id name="customerId" column="CUSTOMER_ID" unsaved-value="0" type="string">
<generator class="native"/>
</id>
<property name="customerName" column="CUSTOMER_NAME" not-null="false" type="string"/>
<property name="status" column="STATUS" not-null="false" type="string"/>
<property name="lastUpdateTime" column="LAST_UPDATE_TIME" not-null="false" type="date"/>
</class>
</hibernate-mapping>

SECOND MAPPING FILE:
<hibernate-mapping package="com.test.model">

<class name="ProfileEventLog" table="PROFILE_EVENT" discriminator-value="ProfileEvent">
<id name="triggerId" column="TRIGGER_ID" unsaved-value="0" type="string">
<generator class="native"/>
</id>

<property name="documentId" column="DOCUMENT_ID" not-null="false" type="string"/>
<property name="clientId" column="CLIENT_ID" not-null="false" type="string"/>
<property name="application" column="APPLICATION" not-null="false" type="string"/>
<property name="userId" column="USER_ID" not-null="false" type="string"/>
<property name="eventType" column="EVENT_TYPE" not-null="false" type="string"/>
<property name="messageEventDateTime" column="MESSAGE_EVENT_DATE_TIME" not-null="false" type="date"/>
<property name="customerId" type="com.test.model.Customer" column="CUSTOMER_ID"/>
property name="profileName" column="PROFILE_NAME" not-null="false" type="string"/>
<property name="profileVersion" column="PROFILE_VERSION" not-null="false" type="integer"/>

</class>
</hibernate-mapping>



GETTER AND SETTER:

public Customer getCustomerId() {
return customerId;
}

public void setCustomerId(Customer customerId) {
this.customerId = customerId;
}


but there is no relation between these two tables for customerID.

i tried giving this..but fails........

i dont know how to get the customerName????????//

if there is a relationship then i can do customerId().getCustomerName();

but no rel...

since i am using spring , if i get the model object(PROFILE_EVENT) ,then i can send it to model and view and i can display the value


Top
 Profile  
 
 Post subject: Re: how to map column if there is no relationship??
PostPosted: Sat Jan 21, 2006 12:07 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
gopal wrote:
I dont know how to map columns if there is no relations ship.

table1 contains clientid, clientname, status,customerid

table 2 contains customerid, customername

i want to display table1 columns, with customer name but there is no relationship in these 2 tables. so how will u map the customer name ???


So, why dont u have the relationship? This looks a perfect way to map both the tables. In your case, how are you maintaining the integrity if you dont have a relationship.

Anyway, if you are working with a legacy db or some such where you cant change, one way to do this would be to write a listener for the "post-load" event like
Code:
<event type="post-load">
                   <listener class="<something that implements PostLoadListener>"/>
           </event>

Your post load listener will use the customerid from table 1 and retrieve the customer name from the other table. This is definitely bad (as it leads to one SQL everytime u load) but if you dont have a relation, this might work.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 21, 2006 4:58 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Technically profileeventlog is a one to many mapping to customer, since it has the customerid in it. It may be that it's a one to one mapping in reality but that DB schema allows one to many.

So, in the ProfileEventLog.java you should have this and no reference to any customerIds anywhere:
Code:
Customer customer;

public Customer getCustomer()....
public void setCustomer(Customer customer)...


And in the ProfileEventLog mapping have (Instead of your current customerid mapping):
Code:
<many-to-one name="customer" column="customer_id"
    class="com.test.model.Customer"/>


That way you can get the customer from the ProfileEventLog using
Code:
select p.customer.customerName from ProfileEventLog p


It may work as a mapping like so if it is a one to one mapping with the same java code I described as above:
Code:
<property name="customer" type="com.test.model.Customer" column="CUSTOMER_ID"/>


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.