为什么我们在 Hibernate 中使用 @Embeddable [英] Why do we use @Embeddable In Hibernate

查看:34
本文介绍了为什么我们在 Hibernate 中使用 @Embeddable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@Embedded@Embeddable 在 Hibernate 中有什么用?因为我在互联网上找到的每个例子都是在单个表中插入数据,并使用两个不同的类来做到这一点.我的观点是,如果我使用的是单个表,那么我可以映射单个类中的所有列,那么我为什么要使用不同的类.如果我们使用两个不同的表,则存在 one-to-oneone-to-many 休眠关系.

What's the use of @Embedded and @Embeddable In Hibernate ? Because every example i found on internet is inserting data inside of a single table and to do that using two different class. My point is if I am using a single table then I can map all the columns inside of a single class then why should i use different class. and if We use two different table then there is one-to-one and one-to-many hibernate relationship.

推荐答案

Hibernate中有两种类型的对象
1. 值对象
2. 实体

There are two types of objects in Hibernate
1. Value Object
2. Entities

值对象是不能独立存在的对象.以 Address 为例.如果你说地址,人们会问这是谁的地址.所以它不能单独存在.

Value Objects are the objects which can not stand alone. Take Address, for example. If you say address, people will ask whose address is this. So it can not stand alone.

实体对象是那些可以像CollegeStudent一样独立的对象.

Entity Objects are those who can stand alone like College and Student.

因此,对于值对象,首选方法是将它们嵌入到实体对象中.

So in case of value objects preferred way is to Embed them into an entity object.

为了回答我们为什么要创建两个不同的类:首先,这是一个 OOPS 概念,您应该在类之间具有松散耦合和高内聚.这意味着您应该仅为特定目的创建类.例如,您的 Student 类应该只包含与 Student 相关的信息.

To answer why we are creating two different classes: first of all, it's a OOPS concept that you should have loose coupling and high cohesion among classes. That means you should create classes for specialized purpose only. For example, your Student class should only have the info related to Student.

第二点是,通过创建不同的类,您可以提高可重用性.

Second point is that by creating different classes you promote re-usability.

当我们为实体类定义值对象时,我们使用@Embeddable.
当我们在实体类中使用值类型对象时,我们使用 @Embedded

这篇关于为什么我们在 Hibernate 中使用 @Embeddable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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