Java @Entity 和 @embeddable 有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21143707/
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
What is difference between @Entity and @embeddable
提问by Sanjeev Yadav
The difference between @entity and @embeddable annotation when each one is added before class declaration?
@entity 和 @embeddable 注解在类声明前添加时的区别?
- the first create class as an entity, second insert column from another table?
- the first create class as an table, while second is embedded in another class?
- the first sets standard as a class, second define table type
- the first create table for that class, second embed something into different class
- the first define table property, second create union of two tables
- 第一个创建类作为实体,第二个从另一个表插入列?
- 第一个创建类作为表,而第二个嵌入在另一个类中?
- 第一个将标准设置为一个类,第二个定义表类型
- 第一个为该类创建表,第二个将某些内容嵌入到不同的类中
- 第一个定义表属性,第二个创建两个表的联合
回答by Sazzadur Rahaman
@Entity
annotation over a class defines that, it has a distinct separate existence. Thus we can run DB queries, without being dependent on any other class. @Embeddable
annotation over a class defines that, it does not have independent existence. Thus we cannot run DB queries, without depending on other class. Here is an example to understand it better:
@Entity
类上的注释定义了它有一个独特的独立存在。因此,我们可以运行数据库查询,而无需依赖任何其他类。@Embeddable
类上的注释定义了它不具有独立存在性。因此我们不能在不依赖其他类的情况下运行数据库查询。这是一个更好地理解它的例子:
@Entity
User
-- long id
-- String name
-- String email
@Embedded
-- UserDetails userDetail
@Embeddable
UserDetails
-- Date dateOfBirth
-- String sex
-- String address
-- String maritalStatus
Here you can see without having a User
, UserDetails
is useless.
在这里你可以看到没有一个User
,UserDetails
是没有用的。
Generally, in OOP, we first design the classes and then we design database entities. For some classes (like UserDetails class in the above example), we do not want to have separate tables in DB, where their independent existence is meaningless. In those cases, we mark the class as embeddable.
一般来说,在OOP中,我们首先设计类,然后设计数据库实体。对于某些类(如上例中的 UserDetails 类),我们不希望 DB 中有单独的表,它们的独立存在是没有意义的。在这些情况下,我们将类标记为可嵌入的。
Typically, embeddable classes share the same table as the Entity in which they are embedded
通常,可嵌入类与嵌入它们的实体共享同一张表
回答by kostja
Entities have an identity and can be queried for. Embeddables have no identity of their own and can only be queried for using the owning entities.
实体具有标识并可被查询。Embeddables 没有自己的身份,只能在使用拥有实体时进行查询。
If you open an entity class, you will always find the @Id
annotation - it is mandatory. If you open an embeddable class, you will never find an @Id
annotation - it is forbidden.
如果你打开一个实体类,你总会找到@Id
注释——它是强制性的。如果您打开一个可嵌入的类,您将永远找不到@Id
注释——这是被禁止的。
EDIT: It is not entirely correct that embeddables can only be stored as a part of the parent, i.e. in the same table. This is only true for one-to-one relationships. You can have Collections
and Maps
of embeddable objects in the parent entity and they will be mapped to own collection tables.
编辑: embeddables 只能作为父级的一部分存储,即在同一个表中,这是不完全正确的。这仅适用于一对一关系。您可以在父实体中拥有Collections
和Maps
可嵌入对象,它们将被映射到自己的集合表。
回答by Martin
An entity class is an ordinary user defined Java class whose instances can be stored in the database.
实体类是一个普通的用户定义的 Java 类,其实例可以存储在数据库中。
@Entity
@Table(name="dog")
public class Dog{
@Id
@Column(name = "id")
private int id;
@Embedded
private Animal animal;
public Dog(int id,Animal animal){
this.id=id;
this.animal=animal;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
}
Embeddable classes are user defined persistable classes that function as value types. As with other non entity types, instances of an embeddable class can only be stored in the database as embedded objects, i.e. as part of a containing entity object.
可嵌入类是用户定义的可持久化类,用作值类型。与其他非实体类型一样,可嵌入类的实例只能作为嵌入对象存储在数据库中,即作为包含实体对象的一部分。
@Embeddable
public class Animal {
@Column(name = "name")
private String name;
@Column(name = "location")
private String location;
public Animal(){
}
public Animal(String name,String location){
this.name=name;
this.location=location;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
回答by NiVeR
It is an old topic but I would like to add my answer, which is more from theoretical point of view. In DDD (domain driven design) we usually have Entity
and Value Objects
. The first ones are identifiable only by a an identity
that they have. The second ones are not defined by an identity, which means that if all the components that make that particular objects are the same, than the 2 value objects are the same.
这是一个古老的话题,但我想补充一下我的答案,更多的是从理论的角度来看。在 DDD(领域驱动设计)中,我们通常有Entity
和Value Objects
。第一个只能通过identity
他们拥有的 an 来识别。第二个不是由身份定义的,这意味着如果构成特定对象的所有组件都相同,那么这两个值对象是相同的。
The analogy is that in this case, if we were to apply DDD, the Entity
is the class annotated with @Entity
and the Value Object
is the one with @Embeddable
. A demonstration of this is the fact that the embeddable object is added as addditional information to an existing record, which already has its own identity
defined externally to the embedded object.
这个比喻是,在这种情况下,如果我们采用DDD的Entity
是带注释的类@Entity
和Value Object
是一个用@Embeddable
。对此的一个证明是,可嵌入对象作为附加信息添加到现有记录中,该记录已经its own identity
在嵌入对象的外部进行了定义。
回答by raj240
Well @Entity signifies that the entity object has significance all by itself it doesn't require any further association with any other object. Where as @Embeddable object doesn't carry any significance all by itself, it needs association with some other object.
好吧@Entity 表示实体对象本身就很重要,它不需要与任何其他对象进一步关联。由于@Embeddable 对象本身没有任何意义,它需要与其他一些对象关联。
Lets take an example of say i have a Employee Object and it has a collection of Address Object as its member variable. Now when when speak of any address we need to tell whose address it is, which employees address it is. If we just talk about the address it doesn't make any sense. Hope this gives you the difference between the two.
让我们举一个例子,说我有一个员工对象,它有一个地址对象的集合作为它的成员变量。现在,当谈到任何地址时,我们需要告诉它是谁的地址,它是哪个员工的地址。如果我们只谈论地址,那没有任何意义。希望这可以让您了解两者之间的区别。