Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA, How to use the same class (entity) to map different tables?

Tags:

java

jpa

entity

People also ask

How do you map an entity to multiple tables?

Yes, you can map an entity to 2 database tables in 2 simple steps: You need to annotate your entity with JPA's @Table and @SecondaryTable annotations and provide the names of the first and second table as the value of the name parameters.

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.

Can a JPA entity have multiple OneToMany associations?

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


Not sure you can do it exactly as you want but you can use inheritance to produce the same result.

AbsT has all the fields but no @Table annotation

Ta and Tb inherit from AbsT and have an @Table annotation each

Use

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

in AbsT.

Sample code:

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class abstract AbsT {
    @Id Long id;
...
}

@Entity
@Table(name = "Ta")
public class Ta extends AbsT {
...
}

@Entity
@Table(name = "Tb")
public class Tb extends AbsT {
...
}

Create an abstract class (a template class) with annotation @MappedSuperclass then extend it. Each class that extends uses @table, @entity annotations and contains nothing but an empty constructor. All code will be in your parent class. On your methods use generics indicating your parameter entity extends from templateClass and no more code changes are needed. The proper mappings will be in each son you pass.


You can also do this without using subclasses if you use two different persistence units.

Each persistence unit can specify a unique set of mappings (including table name). One way to achieve this is to create two orm.xml files. In persistence.xml you'll need something like this :

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0">

    <persistence-unit name="mapping-1"> 
        . . .
        <mapping-file>orm-1.xml</mapping-file>
        . . .
    </persistence-unit>

    <persistence-unit name="mapping-2"> 
        . . .
        <mapping-file>orm-2.xml</mapping-file>
        . . .
    </persistence-unit>
</persistence>

Then within orm-1.xml :

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
    version="1.0">
    <package>mypackage</package>
    <entity name="myEntity" class="myClass">
        <table name="TABLE1">
            </table>
    </entity>
</entity-mappings>

And within orm-2.xml :

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_1_0.xsd"
    version="1.0">
    <package>mypackage</package>
    <entity name="myEntity" class="myClass">
        <table name="TABLE2">
            </table>
    </entity>
</entity-mappings>

You'll need to create a separate EntityManagerFactory for each PersistenceUnit (probably not what you want), but if you wanted to use the same class on different databases (with different table names) this would be a way to go.