Java 为什么我们在 Hibernate 中使用 @Embeddable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19341838/
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
Why do we use @Embeddable In Hibernate
提问by NaN
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.
有什么用的@Embedded
,并@Embeddable
在Hibernate?因为我在互联网上找到的每个例子都是在单个表中插入数据,并使用两个不同的类来做到这一点。我的观点是,如果我使用的是单个表,那么我可以映射单个类中的所有列,那么我为什么要使用不同的类。如果我们使用两个不同的表,那么就有one-to-one
和one-to-many
休眠的关系。
采纳答案by ankit
There are two types of objects in Hibernate
1. Value Object
2. Entities
Hibernate 中有两种类型的对象
1. 值对象
2. 实体
Value Objectsare 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.
值对象是不能单独存在的对象。以Address
为例。如果你说地址,人们会问这是谁的地址。所以它不能单独存在。
Entity Objectsare those who can stand alone like College
and Student
.
实体对象是那些可以像College
和一样独立的对象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
.
要回答我们为什么要创建两个不同的类:首先,这是一个 OOPS 概念,您应该在类之间具有松散耦合和高内聚性。这意味着您应该仅为特定目的创建类。例如,您的Student
班级应该只有与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
当我们为实体类定义值对象时,我们使用@Embeddable
.
当我们在实体类中使用值类型对象时,我们使用@Embedded
回答by Ramkailash
One entity can be embedded in another entity. The attributes of an entity can be common attributes of more than one entity. In this case there can be one embeddable entity. And this embeddable entity can be embedded in more than one entity.
一个实体可以嵌入到另一个实体中。一个实体的属性可以是多个实体的共同属性。在这种情况下,可以有一个可嵌入的实体。并且这个可嵌入实体可以嵌入多个实体中。
Let's consider an example. We have one Animal
entity, which has name
and location
attributes. Now two different entities Lion
and Elephant
can have Animal
attributes just by embedding the Animal
entity. We can override the attributes. In Animal
entity there is location
attribute and in Elephant
there is place
attribute. So with the help of @AttributeOverrides
we can do like below:
让我们考虑一个例子。我们有一个Animal
实体,它具有name
和location
属性。现在,两个不同的实体Lion
,并Elephant
可以Animal
仅仅通过嵌入属性Animal
的实体。我们可以覆盖这些属性。在Animal
实体中有location
属性,在Elephant
有place
属性。所以在@AttributeOverrides
我们的帮助下,我们可以做如下:
@AttributeOverrides({ @AttributeOverride(name = "location", column = @Column(name = "place")) })
回答by siddartha kamble
suppose we have employee table annotated with @entity and employee has Address so here i dont want to create two tables i.e employee and address, i just want create only one table i.e employee not Address table then we need to declare Address instance in Employee and add @embedable annotation on top of Address class, so finally we get table employee with its record and address records as well in single employee table
假设我们有用@entity 注释的员工表,员工有地址,所以在这里我不想创建两个表,即员工和地址,我只想创建一个表,即员工而不是地址表,那么我们需要在员工中声明地址实例并添加@embedable 注释在 Address 类的顶部,所以最后我们在单个员工表中获得了带有记录和地址记录的员工表