简体   繁体   English

使用 Jpa 在不同的表(相同的模式)中保存相同的实体

[英]Saving same entity in different tables(same schema) using Jpa

I've using spring data JPA repositories to save the data into my tables.我使用 spring 数据 JPA 存储库将数据保存到我的表中。

In one of the situation, I've to store data in two different tables.在一种情况下,我必须将数据存储在两个不同的表中。 These two tables have the exact same schema.这两个表具有完全相同的架构。 But I can only store in one table as JpaRepository maps to just one table.但是我只能存储在一张表中,因为 JpaRepository 只映射到一张表。

Example:例子:

@Entity
@Table(name="users")
class User {
  private Long userId;
  private String firstName;
  private String lastName;
}

interface MyRepo1 extends JpaRepositories<User,Long> {
}

interface MyRepo2 extends JpaRepositories<User,Long> {
}

Is there a way to map a single entity into multiple JpaRepositories?有没有办法将单个实体映射到多个 JpaRepositories? So, when I call MyRepo1.save(user) it saves in Users table and when I call MyRepo2.save(user), it saves in BackupUsers(exact same schema as Users) table.因此,当我调用 MyRepo1.save(user) 时,它保存在 Users 表中,当我调用 MyRepo2.save(user) 时,它保存在 BackupUsers(与用户完全相同的架构)表中。

Thanks谢谢

You should have two different entities, for instance, User entity AppUser entity, each one of them pointing to the different tables like this, assuming that the two table names are users and app_users :您应该有两个不同的实体,例如 User 实体 AppUser 实体,每个实体都指向不同的表,如下所示,假设两个表名是usersapp_users

@Entity
@Table(name="users")
public class User extends SuperUser {...}

and

@Entity
@Table(name="app_users")
public class AppUser extends SuperClass {...}

Of course, to avoid code duplication you can put your table fields in a superclass named SuperClass , for example:当然,为了避免代码重复,您可以将表字段放在名为SuperClass的超类中,例如:

    @Entity
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public class SuperUser {
      @Id
      private Long userId;
      private String firstName;
      private String lastName;
      // getters and setters, constructors
    }

After, you need to create two repositories for each entity class:之后,您需要为每个实体类创建两个存储库:

public interface UserRepository extends JpaRepositories<User, Long> {
}

public interface AppUserRepository extends JpaRepositories<AppUser,Long> {
}

NOTE: To finish, it's not recommended at all to have two tables that hold the same fields.注意:最后,完全不建议拥有两个包含相同字段的表。

I don't think you can save in different tables with two different repositories, because you can't specify the table name in repository, but in the entity, I would suggest to use :我不认为你可以用两个不同的存储库保存在不同的表中,因为你不能在存储库中指定表名,但在实体中,我建议使用:

// class which hold common fields
class User {
  private Long userId;
  private String firstName;
  private String lastName;
}

// entity for table 1
@Entity
@Table(name="table1")
class User1 extends User {}

// entity for table 1
@Entity
@Table(name="table2")
class User2 extends User {}

// repo for entity 1
interface MyRepo1 extends JpaRepositories<User1,Long> {}

// repo for entity 2
interface MyRepo2 extends JpaRepositories<User2 ,Long> {}

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

相关问题 JPA,如何使用相同的类(实体)来映射不同的表? - JPA, How to use the same class (entity) to map different tables? 使用注释将单个实体映射到休眠中相同模式的多个表 - Mapping a single entity to multiple tables of same schema in hibernate using annotations Java Hibernate JPA 创建一个实体并加入同一列引用的两个不同的表,具有相同的列名 - Java Hibernate JPA create an entity and Join two different tables referenced by the same column, with same column name 是否可以为同一个 jpa 实体创建多个表? - Is it possible to create multiple tables for the same jpa entity? 如何在JPA中的不同关系中使用同一实体? - How to use same entity in different relations in JPA? JPA 2 个不同的本地查询,具有相同的 class 作为实体 - JPA 2 different native queries with same class as entity 是否可以在同一个实体类中维护不同的模式 - is it possible to maintain the different schema in same entity class 将记录插入相同MySQL模式的不同表中 - Inserting records into different tables of the same MySQL Schema 来自实体的JPA实体管理器createQuery,不同的包但名称相同 - JPA entity manager createQuery from entity, different package but same name 与同一实体的JPA关系 - JPA Relation with same entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM