-->
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: forcing hibernate to use seperate queries for join tables
PostPosted: Mon Sep 04, 2006 9:37 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 10:03 am 
Senior
Senior

Joined: Sat Nov 27, 2004 4:13 am
Posts: 137
I know this may result in n+1 queries problem! But When the result size of the query is small (for example just one result) then the performance of 2 simple queries would be much better than a complex query on 20 tables!

_________________
don't forget to credit!

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


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.