简体   繁体   English

JPA,如何使用相同的类(实体)来映射不同的表?

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

I have two tables: Ta and Tb . 我有两张桌子: TaTb 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. 我的一些常见应用程序模块将使用此实体类根据参数动态查询和更新TaTb Can it be done in JPA? 可以在JPA中完成吗? How can I write the program to dynamically mapping the entity class to different tables at run time? 如何编写程序以在运行时将实体类动态映射到不同的表?

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 AbsT具有所有字段但没有@Table注释

Ta and Tb inherit from AbsT and have an @Table annotation each Ta和Tb继承自AbsT并且每个都有一个@Table注释

Use 使用

@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)

in AbsT. 在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. 使用注释@MappedSuperclass创建一个抽象类(模板类),然后对其进行扩展。 Each class that extends uses @table, @entity annotations and contains nothing but an empty constructor. 扩展的每个类都使用@table,@entity注释,只包含一个空构造函数。 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. 在您的方法上使用泛型,指示您的参数实体从templateClass扩展,不再需要更改代码。 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. 实现此目的的一种方法是创建两个orm.xml文件。 In persistence.xml you'll need something like this : 在persistence.xml中,你需要这样的东西:

<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 : 然后在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 : 在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. 您需要为每个PersistenceUnit创建一个单独的EntityManagerFactory(可能不是您想要的),但是如果您想在不同的数据库(具有不同的表名)上使用相同的类,这将是一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM