Reputation: 267
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 {
}
Upvotes: 3
Views: 1574
Reputation: 229
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 {
}
Upvotes: 1