-->
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: How to mix inheritance stratige with annotation
PostPosted: Mon Nov 24, 2008 12:17 am 
Newbie

Joined: Thu Nov 13, 2008 3:57 am
Posts: 3
As the title described. How to mix the inheritence strategy "table per class hierarchy" and "table per subclass"?

The reason for why I want to do so is:
1. I have a super class which is also a entity class.
2. The super class has many subclasses and all these classes are complex, so we cann't use the "table per class hierarchy" strategy only.
3. There are so many subclasses that there will be to many tables if I use "table per subclass" strategy only.

I know how to mix these two strategies with xml. But, how to config with annotation?

Currently I use the "table per subclass" strategy only. But it cause a performance problem. When I query by the super class, the hibernate will generate sql to query from both the table for super class and tables for subclass. Because the amount of subclass is so big, the query sql runs really slowly.
In fact, I need only the info stored in table for super class when I query by super class; I don't need the info from the subclasses.
So I think it can greatly improve the query performace if I can query only from the table for super class when I query by super class.
How to query data from table for super class when query by super class?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 24, 2008 4:04 am 
Beginner
Beginner

Joined: Wed Nov 19, 2008 8:25 am
Posts: 46
Location: Saint Petersburg, Russian Federation
Find below an example of mixed inheritance strategy:

Base.java

Code:
package com.hibernate.entity;

import javax.persistence.*;

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(
    name = "TYPE",
    discriminatorType = DiscriminatorType.STRING
)
@DiscriminatorValue("BASE")
public class Base {

    @Id
    @GeneratedValue
    private Long id;

    private String baseProperty;

    public String getBaseProperty() {
        return baseProperty;
    }

    public void setBaseProperty(String baseProperty) {
        this.baseProperty = baseProperty;
    }
}


Sub1.java

Code:
package com.hibernate.entity;

import javax.persistence.*;

@Entity
@DiscriminatorValue("SUB1")
public class Sub1 extends Base {

    private String sub1Property;

    public String getSub1Property() {
        return sub1Property;
    }

    public void setSub1Property(String sub1Property) {
        this.sub1Property = sub1Property;
    }
}


Sub2.java

Code:
package com.hibernate.entity;

import javax.persistence.*;

@Entity
@DiscriminatorValue("SUB2")
@SecondaryTable(name = "SUB2_EXT")
public class Sub2 extends Base {

    private String sub2Property;
    @Column(table = "SUB2_EXT")
    private String joinedProperty;

    public String getSub2Property() {
        return sub2Property;
    }

    public void setSub2Property(String sub2Property) {
        this.sub2Property = sub2Property;
    }

    public String getJoinedProperty() {
        return joinedProperty;
    }

    public void setJoinedProperty(String joinedProperty) {
        this.joinedProperty = joinedProperty;
    }
}


This generates the following schema:

Quote:
DEBUG SchemaUpdate - create table Base (TYPE varchar(31) not null, id bigint generated by default as identity (start with 1), baseProperty varchar(255), sub1Property varchar(255), sub2Property varchar(255), primary key (id))
DEBUG SchemaUpdate - create table SUB2_EXT (joinedProperty varchar(255), id bigint not null, primary key (id))
DEBUG SchemaUpdate - alter table SUB2_EXT add constraint FKB1FFE6549102BE0F foreign key (id) references Base


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.