Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to map a field in an Entity without defining any association?

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!

like image 243
mdomenjoud Avatar asked Dec 23 '10 11:12

mdomenjoud


People also ask

Can we define an entity without @ID?

An entity must always have a primary key; you cannot create an entity without a primary key (id).

Can an entity class work without having a definition for all columns of the table?

Of-course if you use it only for querying purpose then it should be fine.

What will happen if @table is not specified while defining pojo?

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.

Can we define entity class without primary key?

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.


2 Answers

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:

  1. This is hibernate-specific code
  2. This is plain SQL, and may thus be database-specific

HTH

[1]: http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#entity-hibspec-property hibernate docs

like image 86
Mopper Avatar answered Jan 26 '23 00:01

Mopper


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

like image 36
jpkrohling Avatar answered Jan 26 '23 02:01

jpkrohling