Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is possible map a single entity with multiple tables using JPA?

Tags:

mapping

jpa

I got a legacy database domain I can't change but it was possible conceive a domain entity to address my problema.

Legacy Tables: TABLE1(ID,VALUE) TABLE2(ID,DATE) TABLE3(ID,DESCRIPTION)

Domain: NewConceptDomain { int value; Date date; String description; }

How can I map new NewConceptDomain using JPA?

like image 990
kauedb Avatar asked Aug 17 '13 00:08

kauedb


People also ask

Can a JPA entity have multiple One-To-Many associations?

You can have multiple one-to-many associations, as long as only one is EAGER.

How do you map entity to a table?

In Spring Data JPA we can map an entity to a specific table by using @Table annotation where we can specify schema and name. But Spring Data JDBC uses a NamingStrategy to map an entity to a table name by converting the entities class name.


1 Answers

Use @SecondaryTable (http://en.wikibooks.org/wiki/Java_Persistence/Tables#Multiple_tables)

@Entity
@Table(name="TABLE1")
@SecondaryTables({
  @SecondaryTable(name="TABLE2",
    pkJoinColumns = @PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID")
  ),
  @SecondaryTable(name="TABLE3",
    pkJoinColumns = @PrimaryKeyJoinColumn(name="ID", referencedColumnName="ID")
  )}
)
like image 108
Luca Basso Ricci Avatar answered Sep 20 '22 21:09

Luca Basso Ricci