与Hibernate和JDBC相关的对象/关系映射(ORM)是什么?

时间:2022-01-02 16:54:14

Can someone explain, in layman terms, what is Object/relational mapping(ORM) in relation to Hibernate and JDBC?

有人能用外行术语解释什么是对象/关系映射(ORM)与Hibernate和JDBC有关吗?

Diagrams would be especially helpful for understanding...

图表将特别有助于理解……

EDIT: I found this via google for Hibernate ORM, can someone confirm that it is accurate and a good representation of how ORM is used.

编辑:我是通过谷歌为Hibernate ORM找到这个的,能不能有人确认它是准确的,并且很好地表示ORM是如何使用的。

与Hibernate和JDBC相关的对象/关系映射(ORM)是什么?

src: http://software-carpentry.org/3_0/summary.html

src:http://software-carpentry.org/3_0/summary.html

3 个解决方案

#1


31  

ORM allows you to use java objects as representation of a relational database. It maps the two concepts (object-oriented and relational)

ORM允许您使用java对象作为关系数据库的表示。它映射了这两个概念(面向对象的和关系的)

Hibernate is an ORM framework - you describe how your objects are represented in your database, and hibernate handles the conversion.

Hibernate是一个ORM框架——您可以描述对象在数据库中的表示方式,Hibernate处理转换。

JDBC is the API for database access, and it works "in a relational way" - you query tables and get rows and columns back. Hibernate uses JDBC under the hood to fetch the data and later convert it to objects.

JDBC是用于数据库访问的API,它“以一种关系方式”工作——您可以查询表并将行和列返回。Hibernate在后台使用JDBC来获取数据,然后将其转换为对象。

A jdbc ResultSet has multiple records, and each record has a set of columns. In hibernate this becomes of List<SomeClass> where SomeClass has a field for every column in the database table, and there is one instance ofSomeClass` per database record.

jdbc ResultSet有多个记录,每个记录都有一组列。在hibernate中,这会变成List ,其中SomeClass对数据库表中的每一列都有一个字段,并且每个数据库记录都有一个SomeClass '的实例。

#2


4  

I was reading on Hibernate and stumbled across this thread. Doing further research, I found this other great explanation which may help someone:

我正在阅读Hibernate,无意中发现了这个线程。做进一步的研究,我发现了另一个很好的解释可以帮助别人:

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.

Hibernate框架简化了java应用程序的开发以与数据库交互。Hibernate是一个开源的、轻量级的ORM(对象关系映射)工具。

An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

ORM工具简化了数据创建、数据操作和数据访问。它是一种将对象映射到数据库中存储的数据的编程技术。

与Hibernate和JDBC相关的对象/关系映射(ORM)是什么?

Advantages of Hibernate:

Hibernate的优点:

1) Opensource and Lightweight: Hibernate framework is opensource under the LGPL license and lightweight.

1)开放源码和轻量级:Hibernate框架是LGPL许可下的开放源码,是轻量级的。

2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled bydefault.

2)快速性能:hibernate框架的性能非常快,因为在hibernate框架中缓存是内部使用的。hibernate框架中的一级缓存和二级缓存有两种类型。第一级缓存是默认启用的。

3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.

3)数据库独立查询:HQL (Hibernate查询语言)是SQL的面向对象版本。它生成独立于数据库的查询。因此,不需要编写特定于数据库的查询。在Hibernate之前,如果项目更改了数据库,我们还需要更改SQL查询,这将导致维护问题。

4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.

4)自动表创建:Hibernate框架提供自动创建数据库表的功能。因此,不需要在数据库中手工创建表。

5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.

5)简化复杂连接:在hibernate框架中,从多个表中获取数据很容易。

6) Provides query statistics and database status: Hibernate supports Query cache and provide statistics about query and database status.

6)提供查询统计信息和数据库状态:Hibernate支持查询缓存并提供查询和数据库状态的统计信息。

Information from javatpoint

信息从javatpoint

#3


1  

Going by your diagram, we have a Person class which has data members : Login, Surname, FirstName and Address. Now Address for each Person object is an object of Address class.

根据您的图表,我们有一个Person类,它有数据成员:登录、姓、名和地址。现在,每个Person对象的地址都是Address类的对象。

Now look at the database tables. We have one Person table corresponding to the Person class and an Address table for Address class. Now there is a relationship between these 2 tables. The primary key of the address table (ID) is mapped to the foreign key (addr) of the Person table. This way a relation is established between the two tables.

现在看一下数据库表。我们有一个对应于Person类的Person表和一个对应于Address类的Address表。这两个表之间有关系。地址表(ID)的主键映射到Person表的外键(addr)。这样,两个表之间就建立了关系。

But for our Person and Address classes we have no such relationship. So what we do is treat the address as a separate object and then integrate with the Person class. So we are actually compromising with the object-oriented methodology to match the relational method of the table. This is actually a very bad way to solve this problem as there's a huge gap between how we are handling the data in the tables and in the classes using object.

但是对于我们的Person和Address类,我们没有这样的关系。所以我们要做的是把地址当作一个单独的对象,然后与Person类集成。所以我们实际上是在牺牲面向对象的方法来匹配表的关系方法。这实际上是解决这个问题的一个非常糟糕的方法,因为我们如何处理表中的数据和使用对象的类之间存在巨大的差距。

This problem is addressed in what is called as Object Relational Mapping (ORM) where we match the two concepts of Object-oriented and relational. ORM allows you to use java objects as representation of a relational database.

这个问题是在所谓的对象关系映射(ORM)中解决的,在这里我们匹配面向对象和关系的两个概念。ORM允许您使用java对象作为关系数据库的表示。

Hibernate is an ORM framework - you describe how your objects are represented in your database, and hibernate handles the conversion.

Hibernate是一个ORM框架——您可以描述对象在数据库中的表示方式,Hibernate处理转换。

#1


31  

ORM allows you to use java objects as representation of a relational database. It maps the two concepts (object-oriented and relational)

ORM允许您使用java对象作为关系数据库的表示。它映射了这两个概念(面向对象的和关系的)

Hibernate is an ORM framework - you describe how your objects are represented in your database, and hibernate handles the conversion.

Hibernate是一个ORM框架——您可以描述对象在数据库中的表示方式,Hibernate处理转换。

JDBC is the API for database access, and it works "in a relational way" - you query tables and get rows and columns back. Hibernate uses JDBC under the hood to fetch the data and later convert it to objects.

JDBC是用于数据库访问的API,它“以一种关系方式”工作——您可以查询表并将行和列返回。Hibernate在后台使用JDBC来获取数据,然后将其转换为对象。

A jdbc ResultSet has multiple records, and each record has a set of columns. In hibernate this becomes of List<SomeClass> where SomeClass has a field for every column in the database table, and there is one instance ofSomeClass` per database record.

jdbc ResultSet有多个记录,每个记录都有一组列。在hibernate中,这会变成List ,其中SomeClass对数据库表中的每一列都有一个字段,并且每个数据库记录都有一个SomeClass '的实例。

#2


4  

I was reading on Hibernate and stumbled across this thread. Doing further research, I found this other great explanation which may help someone:

我正在阅读Hibernate,无意中发现了这个线程。做进一步的研究,我发现了另一个很好的解释可以帮助别人:

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.

Hibernate框架简化了java应用程序的开发以与数据库交互。Hibernate是一个开源的、轻量级的ORM(对象关系映射)工具。

An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.

ORM工具简化了数据创建、数据操作和数据访问。它是一种将对象映射到数据库中存储的数据的编程技术。

与Hibernate和JDBC相关的对象/关系映射(ORM)是什么?

Advantages of Hibernate:

Hibernate的优点:

1) Opensource and Lightweight: Hibernate framework is opensource under the LGPL license and lightweight.

1)开放源码和轻量级:Hibernate框架是LGPL许可下的开放源码,是轻量级的。

2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled bydefault.

2)快速性能:hibernate框架的性能非常快,因为在hibernate框架中缓存是内部使用的。hibernate框架中的一级缓存和二级缓存有两种类型。第一级缓存是默认启用的。

3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.

3)数据库独立查询:HQL (Hibernate查询语言)是SQL的面向对象版本。它生成独立于数据库的查询。因此,不需要编写特定于数据库的查询。在Hibernate之前,如果项目更改了数据库,我们还需要更改SQL查询,这将导致维护问题。

4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.

4)自动表创建:Hibernate框架提供自动创建数据库表的功能。因此,不需要在数据库中手工创建表。

5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.

5)简化复杂连接:在hibernate框架中,从多个表中获取数据很容易。

6) Provides query statistics and database status: Hibernate supports Query cache and provide statistics about query and database status.

6)提供查询统计信息和数据库状态:Hibernate支持查询缓存并提供查询和数据库状态的统计信息。

Information from javatpoint

信息从javatpoint

#3


1  

Going by your diagram, we have a Person class which has data members : Login, Surname, FirstName and Address. Now Address for each Person object is an object of Address class.

根据您的图表,我们有一个Person类,它有数据成员:登录、姓、名和地址。现在,每个Person对象的地址都是Address类的对象。

Now look at the database tables. We have one Person table corresponding to the Person class and an Address table for Address class. Now there is a relationship between these 2 tables. The primary key of the address table (ID) is mapped to the foreign key (addr) of the Person table. This way a relation is established between the two tables.

现在看一下数据库表。我们有一个对应于Person类的Person表和一个对应于Address类的Address表。这两个表之间有关系。地址表(ID)的主键映射到Person表的外键(addr)。这样,两个表之间就建立了关系。

But for our Person and Address classes we have no such relationship. So what we do is treat the address as a separate object and then integrate with the Person class. So we are actually compromising with the object-oriented methodology to match the relational method of the table. This is actually a very bad way to solve this problem as there's a huge gap between how we are handling the data in the tables and in the classes using object.

但是对于我们的Person和Address类,我们没有这样的关系。所以我们要做的是把地址当作一个单独的对象,然后与Person类集成。所以我们实际上是在牺牲面向对象的方法来匹配表的关系方法。这实际上是解决这个问题的一个非常糟糕的方法,因为我们如何处理表中的数据和使用对象的类之间存在巨大的差距。

This problem is addressed in what is called as Object Relational Mapping (ORM) where we match the two concepts of Object-oriented and relational. ORM allows you to use java objects as representation of a relational database.

这个问题是在所谓的对象关系映射(ORM)中解决的,在这里我们匹配面向对象和关系的两个概念。ORM允许您使用java对象作为关系数据库的表示。

Hibernate is an ORM framework - you describe how your objects are represented in your database, and hibernate handles the conversion.

Hibernate是一个ORM框架——您可以描述对象在数据库中的表示方式,Hibernate处理转换。