Hibernate文档:父/子类型关系 [英] Hibernate Documentation: Parent/Child type relationship

查看:319
本文介绍了Hibernate文档:父/子类型关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hibernate文档中,有这样的一个句子:

In the Hibernate documentation, there is such a sentence:


Hibernate中一对多关联的默认语义是
更不近对于父/子关系
的通常语义,而不是复合元素映射的语义。

The default semantics of a one-to-many association in Hibernate are much less close to the usual semantics of a parent/child relationship than those of a composite element mapping.

我发现这句话会很混乱,有人可以解释一下什么意思吗?另外,在 Best Pratices 页面上,你会发现

I found this sentence to be very confusing, can somebody explain what it means? Also, on the Best Pratices page, you will find


编写细粒度的类,并使用< component>

我不知道该怎么做。当使用Hibernate API而不是JPA时,使用< component> 进行映射?如果这是Hibernate具体的,它如何转换为JPA?

I'm not sure what to make of these. Is mapping using <component> when you use Hibernate API instead of JPA? How does it translate to JPA if this is Hibernate specific?

推荐答案

案例1: 一对多关联的默认语义Hibernate远不如复合元素映射的父/子关系通常的语义。 表示: -

假设你有两个类名为 A & B 。 A在B中具有主键 aId ,这是外键。然后在这种情况下,当您使用Hibernate,A可以被称为B& /或B的父母是A的孩子。因为B是依赖于A的主键。如果您尝试在 B 类之前添加 A 之前的条目,那么它会给出错误,如 ConstraintViolationException

Suppose, you have two classes named, A & B. A has primary key aId, which is foreign key in B. Then in this case, when you map them using Hibernate, A can be called as parent of B &/or B is a child of A. Because, B is dependent on A for it's primary key. If you try to add entry in B class before A then it gives error something like ConstraintViolationException.

案例2:编写细粒度的类和使用 < component> 映射他们: -

Case 2 : Write fine-grained classes and map them using <component> :-

单词组件在Hibernate中是指另一个对象中包含的对象不是数据库中的一个单独的实体。 Word 组件描述了Hibernate中的组合。更准确地说,这意味着在Java中用两个独立的对象在数据库表中创建一个实体。

The word component in Hibernate refers to a contained object in another object & not as a separate entity in database. Word component describes composition in Hibernate. More precisely, it means create an entity in database table with two separate objects in Java.

例如:你有一个 Person 包含各种字段的对象,即 id,firstName,lastName,address,age等。然后,您可以创建一个单个数据库中的表作为

For example : You have a Person object containing various fields namely, id, firstName, lastName, address, age, etc. Then you can create a single table in database as

drop table if exists Person
create table Person(
     id int,
     firstName varchar(50),
     lastName varchar(50),
     street varchar(50),
     state varchar(50),
     zipcode int(8),
     age int(3)
);

现在,您需要创建两个pojo类,即& 地址

Now, you need to create two pojo classes namely, Person & Address.

public class Person {
    private int id;
    private String firstName;
    private String lastName;
    private int age;
    private Address address;

    //getter/setter
}

public class Address {
    private String street;
    private String state;
    private int zipCode;

    //getter/setter
}

在这种情况下,您可以使用 < component> 标签,以防 XML配置 @Embedded& ; @Embeddable注解

To introduce composition in this scenario you can use <component> tag in case of XML configuration or @Embedded & @Embeddable annotations.

如果您为Person类创建XML文件,那么可能如下所示:

If you create XML file for the Person class then is likely as follows :

<hibernate-mapping>
   <class name="Person" table="Person">
      <id name="id" type="int" column="id">
         <generator class="native"/>
      </id>
      <component name="address" class="Address">
         <property name="street" column="street" type="String"/>
         <property name="state" column="state" type="String"/>
         <property name="zipcode" column="zipcode" type="int"/>
      </component>
      <property name="firstName" column="firstName" type="String"/>
      <property name="lastName" column="lastName" type="String"/>
      <property name="age" column="age" type="int"/>
   </class>
</hibernate-mapping>

另一方面,使用 @Embedded& POJO类中的@Embeddable注释为: -

On the other hand, using @Embedded & @Embeddable annotations in POJO classes as :-

public class Person {
    @Id
    @GeneratedValue
    private int id;
    private String firstName;
    private String lastName;
    private int age;
    @Embedded
    private Address address;

    //getter/setter
}

@Embeddable
public class Address {
    private String street;
    private String state;
    private int zipCode;

    //getter/setter
}

@Embedded注释用于指定;地址实体应作为组件存储在Person表中。

The @Embedded annotation is used to specify; the Address entity should be stored in the Person table as a component.

@Embeddable注释用于指定; Address类将被用作组件。
Address类不能拥有自己的主键,它使用封闭的类主键。

@Embeddable annotation is used to specify; the Address class will be used as a component. The Address class cannot have a primary key of its own, it uses the enclosing class primary key.

持久化数据库中的人员:
在数据库上执行插入操作。你需要先创建一个Address类&然后Person类。

Persisting Person In DB : While performing insertion operation on database. You need to first create an object of Address class & then Person class.

Address address = new Address("ABC Road", "MH", "440022");
Person person = new Person("A", "Z", 10, address);
session.save(person);

要查看Person表中的数据,您可以点击下面的查询: -

To check data in Person table you can fire below query :-

mysql> select id, firstName, lastName, street, state from Person;
---------------------------------------------------
| id | firstName | lastName |   street  |  state  |
---------------------------------------------------
|  1 | A         | Z        |  ABC Road |   MH    |
|  2 | B         | Y        |  XYZ Road |   MP    |
---------------------------------------------------
2 rows in set (0.00 sec)

mysql>






编辑:案例1与案例2 (协会vs组合):

案例1表示关联,而情况2表示组合。 协会旨在表示两个不同实体之间的关系,例如一对一,一对多,多对一,多对多。另一方面,组合通常被称为两个实体之间的 HAS A 关系,其中一个实体是另一个实体的部分,如果所包含的实体不存在不存在容器实体

Case 1 represents Association whereas, Case 2 represents Composition. Association is meant to represent relationship between two distinct entities, such as one-to-one, one-to-many, many-to-one, many-to-many. On the other hand, composition is commonly known as HAS A relationship between two entities, where one entity is a part of another entity, if the contained entity cannot exist without the existence of container entity.

有关详细说明: 关联,聚合和组合

我希望这可以帮助你。

这篇关于Hibernate文档:父/子类型关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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