-->
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.  [ 3 posts ] 
Author Message
 Post subject: forcing hibernate to use seperate queries for join tables
PostPosted: Mon Sep 04, 2006 9:17 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
Suppose we have:

Code:
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(schema = "scott", name = "tb_animal")
@DiscriminatorColumn(discriminatorType = DiscriminatorType.STRING, name = "brand")
@SequenceGenerator(name = "mysequence", allocationSize = 1, sequenceName = "sq_animal")
public class Animal implements Serializable {
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "mysequence")
    private Long id;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }
}


Code:
@Entity
@DiscriminatorValue("cat")
@SecondaryTable(
        pkJoinColumns = {@PrimaryKeyJoinColumn(name = "pk_animal", referencedColumnName = "id")},
        schema = "scott", name = "tb_cat"
)
public class Cat extends Animal {
    @Column(name = "color", table = "tb_cat")
    private String color;

    @Column(name = "name", table = "tb_cat")
    private String name;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}


and

Code:
@Entity
@DiscriminatorValue("dog")
@SecondaryTable(
        pkJoinColumns = {@PrimaryKeyJoinColumn(name = "pk_animal", referencedColumnName = "id")},
        schema = "scott", name = "tb_dog"
)
public class Dog extends Animal {
    @Column(name = "race", table = "tb_dog")
    private String race;

    @Column(name = "weight", table = "tb_dog")
    private Float weight;

    public String getRace() {
        return race;
    }

    public void setRace(String race) {
        this.race = race;
    }

    public Float getWeight() {
        return weight;
    }

    public void setWeight(Float weight) {
        this.weight = weight;
    }
}



Well the mapping works fine, and "joined subclasses" are simulated and there also exists a "discriminator column".

But a problem remains, when I execute from Animal query, all tables are joined together in resulting SQL query!
Is there any way to hibernate create 2 queries (one only on tb_animal and one on tb_cat or tb_dog) according to discriminator values?

The problem is in our project we have a complex heirarchy which is mapped on more than 20 tables! When I execute something like from Animal ax where ax.id = ? a complex and slow query is exexuted....

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 04, 2006 2:51 pm 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
I've found the solution, it can be done using 9.1.3. Table per subclass, using a discriminator
http://www.hibernate.org/hib_docs/v3/reference/en/html/inheritance.html#inheritance-tablepersubclass-discriminator

But I don't know how could I do the same thing using Annotations?!
Can anyone help me!? this is urgent!

_________________
don't forget to credit!

Amir Pashazadeh
Payeshgaran MT
پايشگران مديريت طرح
http://www.payeshgaran.co
http://www.payeshgaran.org
http://www.payeshgaran.net


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 3:49 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
pasha wrote:
But I don't know how could I do the same thing using Annotations?!
Can anyone help me!? this is urgent!


ahem, you might want to consider using commercial support if you want to use this kind of vocabulary http://www.hibernate.org/148.html

_________________
Emmanuel


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