views:

1368

answers:

3

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?

+7  A: 

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

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

@Entity
@Table(name = "Tb")
public Tb extends AbsT {
....
}
Glen
It does work, Thanks! However, for Toplink, TABLE_PER_CLASS is not supported. I tried @mappedSuperClass method and it works as well.
+1  A: 

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.

Mike
A: 

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 gerericts indicating your parameter entity extends from templateClass and no more code changes are needed. the propper mapings will be in each son you pass.

arbano