如何在同一个数据库表上映射两个JPA或Hibernate实体 [英] How to map two JPA or Hibernate entities on the same database table

查看:1089
本文介绍了如何在同一个数据库表上映射两个JPA或Hibernate实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的项目中,我们有一个实体餐厅,有近30个字段(有些与其他实体有关系)。所以,每次我们需要一个餐厅对象,甚至几个字段,所有其他的检索。这影响性能。因此,在HBM文件中,我们写了两个类,指向同一个物理类和同一个数据库表,如下所示。

In our project, we have an entity 'Restaurant' with nearly 30 fields(some have relationships with other entities). So, every time we need a 'Restaurant' object even for a few fields, all the others are retrieved. This effects the performance. So, in the HBM file, we wrote two classes both pointing to the same physical class and same database table, as shown below.

=== restaurant.hbm.xml ===
<!-- Light Weight Version -->
<class name="com.raj.model.Restaurant" table="RESTAURANTS" entity-name="RestaurantLite" 
                dynamic-update="false" dynamic-insert="false">
<cache usage="read-only"/>
     <!-- few basic properties and relationships -->
</class>

<!-- Restaurant -->
<class name="com.raj.model.Restaurant" table="RESTAURANTS" entity-name="Restaurant">
     <!-- all properties and relationships -->
</class>

在其中一个DAO实现中,我们使用Criteria,它接受'RestaurantLite'如下所示。

In one of the DAO implementations, we are using Criteria which takes 'RestaurantLite' and returning list of restaurants as shown below.

Criteria criteria = session.createCriteria("RestaurantLite");

   // criteria related stuff

return new LinkedHashSet<Restaurant>(criteria.list());



现在我们要删除所有hbm文件并使用注释。那么如何使用注释来做同样的事情呢?我们需要创建一个额外的类'RestaurantLite'吗?如果那么,如何上述条件返回'Restaurant'对象

Now we want to remove all hbm files and use annotations. So how the same can be done using annotations for entites? Do we need to create an extra class 'RestaurantLite'? If then, how the above criteria returns 'Restaurant' objects??

推荐答案

本主题以及如何使用它延迟抓取属性,它在很详细的描述在本文中

This topic as well as how you can use it for lazy fetching attributes, it's described in great details in this article.

为了总结它,下面的映射将演示如何将多个实体映射到同一个数据库表:

To summarize it, the following mappings are going to demonstrate how you can map multiple entities to the same database table:

@Entity(name = "Post")
public class Post {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    private String name;

    private String description;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

@Entity(name = "PostSummary")
@Table(name = "Post")
@Immutable
public class PostSummary {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Entity(name = "UpdatablePostSummary")
@Table(name = "Post")
@DynamicUpdate
public class UpdatablePostSummary {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Hibernate会正常工作:

And Hibernate will work just fine:

@Test
public void testOneTableMultipleEntities() {
    doInTransaction(session -> {
        Post post = (Post) session.get(Post.class, 1L);
        PostSummary postSummary = (PostSummary) session.get(PostSummary.class, 1L);
        UpdatablePostSummary updatablePostSummary = (UpdatablePostSummary) session.get(UpdatablePostSummary.class, 1L);
        assertEquals(post.getName(), postSummary.getName());
        assertEquals(post.getName(), updatablePostSummary.getName());
        updatablePostSummary.setName("Hibernate Master Class Tutorial.");
    });
}




  1. PostSummary 只是对原始实体的只读视图,因此我用 @Immutable 注释它

  1. The PostSummary is just a read-only View over your original entity, hence I annotated it with @Immutable.

UpdatablePostSummary 标有 @DynamicUpdate ,因此您可以这个View实体。

The UpdatablePostSummary is marked with @DynamicUpdate and so you can propagate changes from this View entity too.

此测试也可在 GitHub

这篇关于如何在同一个数据库表上映射两个JPA或Hibernate实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆