简体   繁体   English

Spring 3 + Hibernate 4将数据库中的不同表加载到同一实体类中

[英]Spring 3 + Hibernate 4 Load different Tables from database into same entity class

I have around 10 Tables with similar ColumnNames. 我有大约10个具有类似ColumnNames的表。 Can I bind all tables to a single Entity class ? 我可以将所有表绑定到单个Entity类吗? So, in future if more tables are added, no code change needed. 因此,将来如果添加更多表,则无需更改代码。

eg: Example, REFT_Table1, REFT_Table2 ...REFT_Table10 are the tables on Database. 例如:示例,REFT_Table1,REFT_Table2 ... REFT_Table10是数据库上的表。 When user selects any of the tables from a dropdown on jsp page,hibernate queries database loads it into REFTEntity class 当用户从jsp页面上的下拉菜单中选择任何表时,休眠查询数据库会将其加载到REFTEntity类中

It can be done like below. 可以像下面这样完成。

@Entity
@Table(name="table1")
@SecondaryTables({
      @SecondaryTable(name="table2", pkColumnJoins={@PrimaryKeyJoinColumn(name = "id")}),
      @SecondaryTable(name="table3", pkColumnJoins={@PrimaryKeyJoinColumn(name = "id")})
  })
public class TestEntity {
      @Id
      @GeneratedValue
      private int id;

      private String field1;

      @Column(name="column2", table="table2")
      private String field2;

      @Column(name="column3", table="table3")
      private String field3;

      getter and setter...
}

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

相关问题 Spring / Hibernate同一实体不同的数据库结构 - Spring/Hibernate Same Entity different database structure 如何从数据库加载数据并实例化实体(Hibernate和Spring) - How to load data from Database and instance the entity (Hibernate & Spring) Hibernate实体类映射到两个不同的表 - Hibernate Entity Class maps to two different Tables 在 Intellij Idea 中使用 hibernate 从实体类生成数据库表(不使用 Spring) - Generate Database Tables From Entity Classes using hibernate in Intellij Idea (Not using Spring) 如何在 Java/Spring/Hibernate 中从数据库中删除实体并将克隆存储在不同的数据库表中 - How to Delete Entity from Database and Store a Clone in a Different Database Table in Java/Spring/Hibernate 如何使用Spring + Hibernate将实体持久化到两个不同的表 - How to persist entity to two different tables using Spring+Hibernate JPA,如何使用相同的类(实体)来映射不同的表? - JPA, How to use the same class (entity) to map different tables? 从3个表定义休眠实体 - Define hibernate entity from 3 tables Java Hibernate JPA 创建一个实体并加入同一列引用的两个不同的表,具有相同的列名 - Java Hibernate JPA create an entity and Join two different tables referenced by the same column, with same column name 将数据从CSV加载到mySQL数据库Java + hibernate + spring - Load data from CSV to mySQL database Java+hibernate+spring
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM