ORM是否适用于非crud数据库?

时间:2021-09-03 07:23:48

I'm pretty new to database development in general and I've never used ORM before. I'm interested in the benefits of using one, specifically saving time writing boilerplate SQL queries. I'd like to use ORM for a project that I'm working on right now, but I'm not sure it's applicable.

我对数据库开发很陌生,我以前从未使用过ORM。我感兴趣的是使用一种方法的好处,特别是节省时间编写样板SQL查询。我想用ORM来做我正在做的一个项目,但是我不确定它是否适用。

This project is more akin to change tracking for very small (<= 500 characters) documents. I need to track edits and categorizations made by multiple users. Not really to see the specific changes they make, but more to see if the users agree with each other. I am using a SQL database for this (as opposed to actual document control) for a few reasons:

这个项目更类似于修改很小(<= 500个字符)文档的跟踪。我需要跟踪多个用户的编辑和分类。不是要真正看到他们所做的具体更改,而是要看用户是否同意彼此。我之所以使用SQL数据库(而不是实际的文档控制)是因为以下几个原因:

  1. The documents are really small; and I'm only interested in the strings, not really in files.
  2. 文件非常小;我只对字符串感兴趣,对文件不感兴趣。
  3. I wanted the ability to perform ad-hoc queries against the data for development purposes, and didn't want an unpleasantly surprised halfway through that a particular document control package couldn't do what I wanted.
  4. 我希望能够为开发目的对数据执行临时查询,并且不希望在中途某个特定的文档控制包不能执行我想要的操作时感到意外。

From most of what I've read it seems like you need a direct mapping from columns to data fields in an object to use ORM. What I have now does not even come close to this. To create objects representing documents in different stages of editing I have to cobble together data from columns in different tables, in different combinations.

从我所读到的大部分内容来看,使用ORM似乎需要对象中从列到数据字段的直接映射。我现在所拥有的甚至还没有接近这一点。要创建表示不同编辑阶段的文档的对象,我必须将来自不同表中的列的数据以不同的组合拼凑在一起。

So my question is: Does an ORM like Hibernate apply to this type of project? And if it does can one be added to an existing application/database?

所以我的问题是:像Hibernate这样的ORM是否适用于这种类型的项目?如果它可以添加到现有的应用程序/数据库中?

If it makes a difference: I'm using Java, MySQL, and JDBC. The web app users have access to for edits is made with GWT and hosted via Tomcat6. If I need it, I have complete control of the webserver.

如果有什么不同的话:我正在使用Java、MySQL和JDBC。web应用程序用户可以通过GWT进行编辑,并通过Tomcat6托管。如果需要的话,我可以完全控制webserver。

Thanks.

谢谢。

3 个解决方案

#1


1  

My opinion is that an ORM tool could be useful for you but you really need to delve into it to see for yourself. Remember when you use an ORM tool you are not forced to use only that to connect to your database. ORM tools in general make the most sense for applications that store data in a very object like structure. For instance your user code might be the place to start. Usually you only create 1 user at a time, you edit 1 user at a time, you check if 1 user is logged into. It also makes sense for things where you would return a list of results like Order Lines. Where I have run into issues with ORM tools is when you have complex data that requires multiple joins especially back to the table you started in. For those cases you might want to keep doing what you are doing. Overall, ORM tools are great but they are like a lot of other things in software development. Try them out on a small part of your code and use them where they work and don't where they don't. Ultimately, you are the one that will have to deal with and maintain whatever you make. Just educate yourself on Hibernate and I am sure you will know what to do!

我的观点是,ORM工具可能对您有用,但您确实需要深入研究它以了解您自己。记住,当您使用ORM工具时,您不会*仅使用它来连接数据库。通常,ORM工具对于将数据存储在类似于结构的对象中的应用程序最有意义。例如,您的用户代码可能是开始的地方。通常一次只创建一个用户,一次编辑一个用户,检查是否有一个用户登录。它对于返回一系列结果(如订单行)也是有意义的。我遇到ORM工具问题的地方是当您有需要多个连接的复杂数据时,特别是回到您开始使用的表。对于这些情况,你可能想继续做你正在做的事情。总的来说,ORM工具非常棒,但是它们和软件开发中的许多其他东西一样。在代码的一小部分上尝试它们,并在它们工作的地方使用它们,而不是在它们不工作的地方使用它们。最终,你将不得不处理和维护你所做的一切。只要自学一下冬眠,我相信你会知道该怎么做的!

#2


2  

Does an ORM like Hibernate apply to this type of project?

像Hibernate这样的ORM是否适用于这种类型的项目?

Yes

是的

And if it does can one be added to an existing application/database?

如果它可以添加到现有的应用程序/数据库中?

Yes

是的

#3


0  

I think that ORM (I would suggest using the JPA standard, probably with Hibernate as the provider) could suit your project.

我认为ORM(我建议使用JPA标准,可能使用Hibernate作为提供者)可以适合您的项目。

It is fairly traditional, as you say, for database columns to map directly onto object fields. If you need to keep your existing database structure (which apparently doesn't map at all well to your objects), then you might find that its more trouble than it's worth to use ORM.

正如您所说,将数据库列直接映射到对象字段是相当传统的。如果您需要保留现有的数据库结构(显然它与对象没有很好的映射关系),那么您可能会发现使用ORM比使用它更麻烦。

While it's certainly possible to use ORM to map to a specific database schema - perhaps because it's used by other systems - my view is that one of the biggest advantages of ORM is that you can almost ignore the schema. Once you design your objects, and tell hibernate about them, hibernate will create whatever tables it needs.

虽然可以使用ORM映射到特定的数据库模式(可能因为其他系统使用ORM),但我认为ORM最大的优点之一是您几乎可以忽略该模式。一旦您设计了对象,并告诉hibernate关于它们的信息,hibernate就会创建它需要的任何表。

#1


1  

My opinion is that an ORM tool could be useful for you but you really need to delve into it to see for yourself. Remember when you use an ORM tool you are not forced to use only that to connect to your database. ORM tools in general make the most sense for applications that store data in a very object like structure. For instance your user code might be the place to start. Usually you only create 1 user at a time, you edit 1 user at a time, you check if 1 user is logged into. It also makes sense for things where you would return a list of results like Order Lines. Where I have run into issues with ORM tools is when you have complex data that requires multiple joins especially back to the table you started in. For those cases you might want to keep doing what you are doing. Overall, ORM tools are great but they are like a lot of other things in software development. Try them out on a small part of your code and use them where they work and don't where they don't. Ultimately, you are the one that will have to deal with and maintain whatever you make. Just educate yourself on Hibernate and I am sure you will know what to do!

我的观点是,ORM工具可能对您有用,但您确实需要深入研究它以了解您自己。记住,当您使用ORM工具时,您不会*仅使用它来连接数据库。通常,ORM工具对于将数据存储在类似于结构的对象中的应用程序最有意义。例如,您的用户代码可能是开始的地方。通常一次只创建一个用户,一次编辑一个用户,检查是否有一个用户登录。它对于返回一系列结果(如订单行)也是有意义的。我遇到ORM工具问题的地方是当您有需要多个连接的复杂数据时,特别是回到您开始使用的表。对于这些情况,你可能想继续做你正在做的事情。总的来说,ORM工具非常棒,但是它们和软件开发中的许多其他东西一样。在代码的一小部分上尝试它们,并在它们工作的地方使用它们,而不是在它们不工作的地方使用它们。最终,你将不得不处理和维护你所做的一切。只要自学一下冬眠,我相信你会知道该怎么做的!

#2


2  

Does an ORM like Hibernate apply to this type of project?

像Hibernate这样的ORM是否适用于这种类型的项目?

Yes

是的

And if it does can one be added to an existing application/database?

如果它可以添加到现有的应用程序/数据库中?

Yes

是的

#3


0  

I think that ORM (I would suggest using the JPA standard, probably with Hibernate as the provider) could suit your project.

我认为ORM(我建议使用JPA标准,可能使用Hibernate作为提供者)可以适合您的项目。

It is fairly traditional, as you say, for database columns to map directly onto object fields. If you need to keep your existing database structure (which apparently doesn't map at all well to your objects), then you might find that its more trouble than it's worth to use ORM.

正如您所说,将数据库列直接映射到对象字段是相当传统的。如果您需要保留现有的数据库结构(显然它与对象没有很好的映射关系),那么您可能会发现使用ORM比使用它更麻烦。

While it's certainly possible to use ORM to map to a specific database schema - perhaps because it's used by other systems - my view is that one of the biggest advantages of ORM is that you can almost ignore the schema. Once you design your objects, and tell hibernate about them, hibernate will create whatever tables it needs.

虽然可以使用ORM映射到特定的数据库模式(可能因为其他系统使用ORM),但我认为ORM最大的优点之一是您几乎可以忽略该模式。一旦您设计了对象,并告诉hibernate关于它们的信息,hibernate就会创建它需要的任何表。