-->
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.  [ 4 posts ] 
Author Message
 Post subject: Inheritance using annotations
PostPosted: Tue Feb 03, 2009 12:59 pm 
Newbie

Joined: Thu Jan 29, 2009 8:49 pm
Posts: 5
Hello,

I am trying to do something very simple. I have different types of contact information like emails, addresses, phones etc stored in one table with a discriminator column.

I have a parent class called ContactInfo


Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="CT_CODE",  discriminatorType = DiscriminatorType.STRING)
@Table(name= "MX_ADDRESS")
public class ContactInfo extends Serializable
{
...
}


Each of the contact types have their own classes for eg
Code:
@Entity
@DiscriminatorValue("addr")
public class Address extends ContactInfo
{
...
@ManyToOne
@JoinColumn(name="MAST_ID")
private Master owner;
}

@Entity
@DiscriminatorValue("fax")
public class Fax extends ContactInfo
{
...
@ManyToOne
@JoinColumn(name="MAST_ID")
private Master owner;
}


Now when i try to get only the addresses it returns me back all the addresses

Code:
Session session = getHibernateTemplate().getSessionFactory()
             .getCurrentSession();
Criteria criteria = session.createCriteria(Address.class);
criteria.list();


All the contact information belong to a person.

Code:
@Entity
@Table(name= "MX_MASTER")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="MAST_MEMEX_INDORG", discriminatorType= DiscriminatorType.STRING)
public class Master extends Serializable
{
...
    @OneToMany(mappedBy="owner")
    @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
    protected Set<Address> addresses = new TreeSet<Address>();
}

@Entity
@DiscriminatorValue("I")
public class Individual extends Master
{
...
}


But now when i tried to go through the member it returns me back all the contact info even if i am just looking for addresses

Code:
Master o = (Master ) getHibernateTemplate().get(Master.class, new Long(714));
o.getAddresses().size();



I am not sure why is the discriminator column in Address not being used. Can any one help???

~s.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 03, 2009 1:18 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
The problem is, that you join on the same column. You can fix it if you use
Code:
@OneToMany(mappedBy="owner")
    @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
    @Where(clause="CT_CODE='addr'")
    protected Set<Address> addresses = new TreeSet<Address>();


See also here.

Rating is appreciated.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 1:05 pm 
Newbie

Joined: Thu Jan 29, 2009 8:49 pm
Posts: 5
Thank you that is what i ended up doing.

but should hibernate not add the discriminator column and its value to the where clause automatically???


~s.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 04, 2009 1:07 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Quote:
but should hibernate not add the discriminator column and its value to the where clause automatically???


Yes, I guess this could be improved.

_________________
-----------------
Need advanced help? http://www.viada.eu


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