I've got the following schema in DB (simplified)
MainTable(
ID primary key
SOMEFIELD
CODE_FK1 -- references OtherTable1 CODE (without declared foreign key)
CODE_FK2 -- references OtherTable2 CODE (without declared foreign key)
... Other fields used
)
OtherTable1(
CODE primary key
LABEL
... other fields not used
)
OtherTable2(
CODE primary key
LABEL
... other fields not used
)
I'm asking if there is any way to define my Entity for main table in order to use directly labels from my other tables, i.e without defining entities for these other table.
I cannot change the DB schema, which is really awful (there are labels/code couples everywhere, defined in multiples tables). And If it was possible, this solution would allow to keep my code simple, since I don't really need these other entities.
I guess it would result something like that:
@Entity
public class MainEntity{
@Id
private Integer ID;
@Column(name="SOMEFIELD")
private String SomeField;
@SomeAnnotation to Join CODE_FK_1 with OtherTable1.CODE
@SomeAnnotation like @Column(name="LABEL", table="OtherTable1")
private String Label1;
@SomeAnnotation to Join CODE_FK_1 with OtherTable1.CODE
@SomeAnnotation like @Column(name="LABEL", table="OtherTable1")
private String Label1;
}
Thanks by advance for your help!
An entity must always have a primary key; you cannot create an entity without a primary key (id).
Of-course if you use it only for querying purpose then it should be fine.
If no @Table is defined the default values are used: the unqualified class name of the entity. For example if you have: @Entity public class MyTest{ ... Your table will have the name my_test in your database.
When you define an entity object, it must have a primary key or use a RowID attribute (based on the table's ROWID). To do so, you must create a new attribute while you are defining your entity object in the Entity Object Wizard.
Another possibility would be using the @Formula
annotation to fetch the value from the other table. This will automatically generate a subselect whenever you load your Entity.
I think you'll need something like this:
@Entity
public class MainEntity{
@Id
private Integer ID;
@Column(name="SOMEFIELD")
private String SomeField;
@Formula("(SELECT ot1.LABEL FROM OtherTable1 ot1 WHERE ot1.CODE = CODE_FK_1)")
private String Label1;
}
There is little information about this in the [Hibernate docs][1], so you may need some trial and error to get it right (but you should be able to work it out with hibernate.show_sql=true
.
There are 2 possible downsides to this approach:
HTH
[1]: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-property hibernate docs
You can use the @SecondaryTable annotation. See this example:
https://github.com/hibernate/hibernate-orm/blob/823a5c1ede1869fd97471e3b8ebe7ec4ac8068e4/hibernate-core/src/test/java/org/hibernate/test/annotations/join/Dog.java#L20-L24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With