java Hibernate 映射类实体名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13866626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 14:14:28  来源:igfitidea点击:

Hibernate mapping class entity-name

javahibernatesessionormentity

提问by user1817081

Is it possible to use the entity-nameattribute in classto set an entity and to reference it? I want to do this because I want to map to multiple tables with the same entity class.

是否可以使用entity-name属性 inclass来设置实体并引用它?我想这样做是因为我想映射到具有相同实体类的多个表。

Table 1 and adble 2 have the same schema

表 1 和 adble 2 具有相同的架构

@Entity
public class POJO{
    @Id
    @Column(name="column1")
    private String column1;

    @Column(name="column2")
   private String column2;

   //getters and setters

}



<hibernate mapping>
    <class name="package.POJO" entiy-name="EntityTable1" table="table1">
        <id>.....</id>
            <property>....</property>
            <property>....</property>
     </class>

     <class name="package.POJO" entiy-name="EntityTable2" table="table2">
        <id>.....</id>
            <property>....</property>
            <property>....</property>
     </class>
</hibernate mapping>


Session s = SessionFactory.openSession();
List table1List = s.createQuery("FROM EntityTable1").list();

List table1List = s.createQuery("FROM EntityTable2").list();

I read in the Hibernate Documentationthat this is only in the experimental stage. Has anyone used this method and work?

我在Hibernate 文档中读到这只是在实验阶段。有没有人使用过这种方法并工作?

采纳答案by Stanislav Bashkyrtsev

Yes, you can do this via XML, I didn't experience any problems with that. The example is hereand here. You can find example of usages hereand hererespectively.

是的,您可以通过 XML 执行此操作,我没有遇到任何问题。这个例子是herehere。您可以分别在此处此处找到用法示例。

Note, that you can't do that same with annotations, that's where XML is more flexible.

请注意,您不能对注释做同样的事情,这是 XML 更灵活的地方。