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

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

问题描述

在Hibernate中有什么用 @Embedded @Embeddable 因为我在互联网上找到的每个例子都是在单个表格中插入数据,并使用两个不同的类来完成。我的观点是如果我使用单个表格,那么我可以映射单个类中的所有列,那么为什么我应该使用不同的类。如果我们使用两个不同的表,那么一对一一对多 hibernate关系。

解决方案

Hibernate中有两种类型的对象

1. Value对象
2.实体



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

实体对象是那些像 College c>和学生

所以在值对象的情况下,首选的方法是将它们嵌入到实体对象中。



为了回答我们为什么要创建两个不同的类:首先,它是一个OOPS概念,您应该在类之间有松耦合和高内聚性。这意味着您应该只为专门用途创建类。例如,您的 Student 类应只有与 Student 相关的信息。



第二点是通过创建不同的类来提高可重用性。



当我们为实体类定义值对象时, @Embeddable

当我们在实体类中使用值类型对象时,我们使用 @Embedded


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.

解决方案

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

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.

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.

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.

When we define the value object for the entity class we use @Embeddable.
When we use value type object in entity class we use @Embedded

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

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