2

I have two objects with same fields, but from different tables (foo_something and bar_something in fact not plain tables, there are results of several join operations). I use @Subselect hibernate annotation for fetching data. Can I make class structure like this, but which working fine? This class structure doesn't work because annotations doesn't inherit. If I use @Inheritance annotation, I get error Unknown column foo_somethingca0.DTYPE

class Something {

    @Id @Column Long id;
    @Column String name;
    @Column String description;

    //getters, setters, constructors

}

@Entity
@Immutable
@Subselect("SELECT id, name, description FROM foo_something")
class FooSomething extends Something {

}

@Entity
@Immutable
@Subselect("SELECT id, name, description FROM bar_something")
class BarSomething extends Something {

}

UPD.

Thanks, @HenryMartens, I added annotations @Entity and @Inheritance with TABLE_PER_CLASS strategy, and made class abstract, now it's working fine:

@Entity
@Inheritance(strategy = TABLE_PER_CLASS)
abstract class Something {

    @Id @Column Long id;
    @Column String name;
    @Column String description;

    //getters, setters, constructors

}

@Entity
@Immutable
@Subselect("SELECT id, name, description FROM foo_something")
class FooSomething extends Something {

}

@Entity
@Immutable
@Subselect("SELECT id, name, description FROM bar_something")
class BarSomething extends Something {

}

1 Answer 1

1

The DTYPE in Hibernate is the discriminator column. It is used to tell the difference between some objects that are persisted in the same database table like your example. You can add an discriminator column with the @DiscriminatorColumn annotation.

On the concrete classes you can use @DiscriminatorValue to set the value of the discriminator column for the annotated class.

For example:

@Entity
@Inheritance( strategy = InheritanceType.TABLE_PER_CLASS ) 
@DiscriminatorColumn(name="DISC", discriminatorType=STRING, length=20)
class abstract Something {

    @Id 
    @Column 
    Long id;

    @Column 
    String name;

    @Column 
    String description;

    //getters, setters, constructors 
}

@Entity
@Table("FOO_SOMETHING")
@Immutable
@DiscriminatorValue("FooSomething")
class FooSomething extends Something {

}

@Entity
@Table("BAR_SOMETHING")
@Immutable
@DiscriminatorValue("BarSomething")
class BarSomething extends Something {

}
Sign up to request clarification or add additional context in comments.

2 Comments

My base class Something - it's not a table, it's simple base class with list of fields, getters and setters, which i don't want to duplicate in BarSomething and FooSomething, that's why I cannot use @Table annotation in base class cause SchemaManagementException: Schema-validation: missing table [SOMETHING]. This class an abstract.
@Denis Gavrus Then you have to make your Something class abstract. You can use InheritanceType.TABLE_PER_CLASS to get a table for each FooSomething and BarSomething.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.