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

JavaJpaEntity

Java Problem Overview


I have two tables: Ta and Tb. They have exactly the same table structure but different table names.

I try to create one entity class to map the table structures. Some of my common application modules will use this entity class to dynamically query and update either Ta or Tb based on parameters. Can it be done in JPA? How can I write the program to dynamically mapping the entity class to different tables at run time?

Java Solutions


Solution 1 - Java

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 {
...
}

Solution 2 - Java

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.

Solution 3 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohnView Question on Stackoverflow
Solution 1 - JavaGlenView Answer on Stackoverflow
Solution 2 - JavaarbanoView Answer on Stackoverflow
Solution 3 - JavaMikeView Answer on Stackoverflow