-->
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.  [ 6 posts ] 
Author Message
 Post subject: Map child, return a property of parent instead of entity?
PostPosted: Mon Dec 03, 2007 5:19 pm 
Newbie

Joined: Tue Nov 13, 2007 8:11 pm
Posts: 7
Hibernate version:
3.2

I have several tables containing only id/name pairings and would like to map the child table to its parent but, instead of returning the parent entity, i'd like to return a property of the parent entity.

Is this possible?

I understand that I could just map the table and the create a method in child like

String getMIMEType()
{
return mime.getTypeName();
}

This is quite trivial to do, but I would like to avoid the mappings if possible, as I have many of these types of tables.


Thanks.


Top
 Profile  
 
 Post subject: Re: Map child, return a property of parent instead of entity
PostPosted: Mon Dec 03, 2007 5:26 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Roachling wrote:
Hibernate version:
3.2

I have several tables containing only id/name pairings and would like to map the child table to its parent but, instead of returning the parent entity, i'd like to return a property of the parent entity.

Is this possible?

I understand that I could just map the table and the create a method in child like

String getMIMEType()
{
return mime.getTypeName();
}

This is quite trivial to do, but I would like to avoid the mappings if possible, as I have many of these types of tables.


Thanks.



You might be able to solve this by joins. Look at section 5.1.18 of the online documentation.


Farzad-


Top
 Profile  
 
 Post subject: Re: Map child, return a property of parent instead of entity
PostPosted: Tue Dec 04, 2007 6:47 pm 
Newbie

Joined: Tue Nov 13, 2007 8:11 pm
Posts: 7
farzad wrote:
You might be able to solve this by joins. Look at section 5.1.18 of the online documentation.
Farzad-


Hi, thanks for the reply.

If I use join in the child table's config, then Hibernate will treat is as the parent. The join would need to be used in the mimeTable's config (which is a table i'd rather not map).

In this example A is the child.


create table A (
id integer not null auto_increment,
type integer not null,
primary key (id)
) type=InnoDB;


create table mimeType (
contentTypeId integer not null,
contentType varchar(255),
primary key (contentTypeId)
) type=InnoDB;


alter table A
add index FKTYPE (type),
add constraint FKTYPE
foreign key (type)
references mimeType(contentTypeId);


<class name="A">

<id name="id" type="integer">
<generator class="native"/>
</id>

<join table="mimeType">
<key column="contentTypeId"/>
<property name="contentType"/>
</join>

</class>

public class A {
//...

public void setContentType(String type) {
this.type = type;
}

public String getContentType() {
return type;
}

}

Generates:

[java] Hibernate:
[java] select
[java] a0_.id as id0_0_,
[java] a0_1_.contentType as contentT2_1_0_
[java] from
[java] A a0_
[java] inner join
[java] mimeType a0_1_
[java] on a0_.id=a0_1_.contentTypeId
[java] where
[java] a0_.id=?


Top
 Profile  
 
 Post subject: Re: Map child, return a property of parent instead of entity
PostPosted: Tue Dec 04, 2007 8:29 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Roachling wrote:
farzad wrote:
You might be able to solve this by joins. Look at section 5.1.18 of the online documentation.
Farzad-


Hi, thanks for the reply.

If I use join in the child table's config, then Hibernate will treat is as the parent. The join would need to be used in the mimeTable's config (which is a table i'd rather not map).


Ok you lost me here. Can you rephrase your problem? In other words, can you tell me what you expect to happen in your example?


Top
 Profile  
 
 Post subject: Re: Map child, return a property of parent instead of entity
PostPosted: Wed Dec 05, 2007 2:31 pm 
Newbie

Joined: Tue Nov 13, 2007 8:11 pm
Posts: 7
farzad wrote:
Ok you lost me here. Can you rephrase your problem? In other words, can you tell me what you expect to happen in your example?



Hi, ok, so what I want is to do is relate A to the mimeType, providing only a getter in A for a property of mimeType, without mapping mimeType.

I have several constant tables containing only key/value pairs, and would like to avoid mapping them, if possible.

Given the mapping:

<class name="A">

<id name="id" type="integer">
<generator class="native"/>
</id>

<join table="mimeType">
<key column="contentTypeId"/>
<property name="contentType"/>
</join>

</class>

I would have wanted this to create the following query:

select A.id, mimeType.contentType from A inner join mimeType on mimeType.contentTypeId = A.type

Subsequently, the A entity would be populated with the contentType column.

Instead the join element
causes the mimeType table to be treated as the child in the relationship, which it is not.

Thanks


Top
 Profile  
 
 Post subject: Re: Map child, return a property of parent instead of entity
PostPosted: Wed Dec 05, 2007 4:22 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
Roachling wrote:
Instead the join element
causes the mimeType table to be treated as the child in the relationship, which it is not.

Thanks


I see your point now. You are right. Joins are not made for this. However, you might still be able to use joins if you are not going to update your object. Nonetheless, it is not very performance efficient if most of those key-value pairs are constant. If so, you might want to define a new entity for each of them and cache them in memory and you will have a better performance. In any case, I can't come to any other idea now. Lets see if anyone else has a solution for this.

My ++1 cents
Farzad-


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