oracle 休眠和存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2402039/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 20:09:10  来源:igfitidea点击:

Hibernate and stored procedures

javaoraclehibernatestored-proceduresorm

提问by cc96ai

As my understanding on setting hibernate, I need to create

作为我对设置休眠的理解,我需要创建

  • table meta data file (person.hbm.xml), include all the fields mapping
  • java object (person.java)
  • 表元数据文件 ( person.hbm.xml),包括所有字段映射
  • java 对象 ( person.java)

If we use stored procedures for all transaction, do we still need the above configuration?

如果所有事务都使用存储过程,还需要上面的配置吗?

It seems hibernate and stored procedures will overlap,

似乎休眠和存储过程会重叠,

We set up the stored procedure because we don't want the to developer know all the field in db. If tables change, then we need update above files.

我们设置存储过程是因为我们不希望开发人员知道 db 中的所有字段。如果表发生变化,那么我们需要更新上述文件。

Does it mean if we purely use stored procedure, we should just go for JDBC?

这是否意味着如果我们纯粹使用存储过程,我们应该只使用JDBC?

If hibernate, we should stay in HQL?

如果hibernate,我们应该留在HQL吗?

回答by ewernli

You can use native SQLand map the result to object:

您可以使用本机 SQL并将结果映射到对象:

sess.createSQLQuery("SELECT * FROM CATS").addEntity(Cat.class);

The JDBC syntaxto invoke store procedureis like following:

调用存储过程JDBC 语法如下所示:

CallableStatement proc =
connection.prepareCall("{ call set_death_age(?, ?) }");
proc.setString(1, poetName);
proc.setInt(2, age);

So maybe, you can invoke stored procedure and map them to object:

所以也许,您可以调用存储过程并将它们映射到对象:

  sess.createSQLQuery("{ call my_stored_proc }").addEntity(Cat.class);

Note also that updatesmade through stored procedures will escape hibernate, which means that you will need to evict objects from the 1st level and 2nd level cacheyourself.

另请注意,通过存储过程进行的更新将逃避休眠,这意味着您需要自己从第一级和第二级缓存中逐出对象。

So as you see, hibernate and stored procedure don't really fit naturally together.

因此,如您所见,休眠和存储过程并不能真正自然地结合在一起

we set up the stored procedure because we don't want the to developer know all the field in db. if table change, then we need update above files.

我们设置存储过程是因为我们不希望开发人员知道 db 中的所有字段。如果表发生变化,那么我们需要更新上面的文件。

If you're concerned about security, either use:

如果您担心安全性,请使用:

回答by Pascal Thivent

It is possible to use native-sql and to use stored procedure for querying(with limiations/rules). But, as written in the documentation:

可以使用 native-sql 并使用存储过程进行查询(使用limitations/rules)。但是,正如文档中所写:

Stored procedures currently only return scalars and entities. <return-join>and <load-collection>are not supported.

存储过程目前只返回标量和实体。<return-join>并且<load-collection>不受支持。

So if you want to work with non-managed entities (i.e. not scalars in an Object[]), you'll have to apply a ResultTransformerin the code.

因此,如果您想使用非托管实体(即不是 中的标量Object[]),则必须ResultTransformer在代码中应用 a 。

But at the end, if you want to hide the database to developers, if you don't want to map objects to tables, if you don't want to work with associations, if you don't want to use HQL, if you don't want to use an OO approach, then I really wonder why you want to use Hibernate. You'd better use raw JDBC (with Spring for example) or maybe a data-mapper like iBATIS.

但是最后,如果您想对开发人员隐藏数据库,如果您不想将对象映射到表,如果您不想使用关联,如果您不想使用 HQL,如果您不想使用面向对象的方法,那么我真的很想知道您为什么要使用 Hibernate。您最好使用原始 JDBC(例如使用 Spring)或者像 iBATIS 这样的数据映射器。

回答by Will Marcouiller

Using Hibenate with Stored Procedures is a certain overlap. As you for example need to write astored procedure for INSERT, UPDATE, DELETE and SELECT, Hibernate provides an easiest way to interract with with relational database objects by mappings them into metadata files like you mentioned person.hbm.xml.

将 Hiberate 与存储过程一起使用是有一定重叠的。例如,当您需要为 INSERT、UPDATE、DELETE 和 SELECT 编写存储过程时,Hibernate 提供了一种最简单的方法来与关系数据库对象交互,方法是将它们映射到元数据文件中,如您提到的 person.hbm.xml。

Yes, the use of stored procedure wil require you to write these metadata files anyway. A stored procedure will not replace the Hibernate mappings. Those mapping only tell Hibernate how to persist your object-oriented model to the database. A great thing about Hibernate is that you may even, if needed, generate you database model from your JAVA code through the schema generation tool.

是的,使用存储过程无论如何都需要您编写这些元数据文件。存储过程不会替换 Hibernate 映射。这些映射只告诉 Hibernate 如何将面向对象的模型持久化到数据库中。Hibernate 的一个优点是,如果需要,您甚至可以通过模式生成工具从 JAVA 代码生成数据库模型。

As for the stored procedures, one recommended way is to configure your stored procedures as named queries from within the configuration file. This, however, makes you miss the better potential, in my opinion, of Hibernate.

对于存储过程,推荐的一种方法是将存储过程配置为配置文件中的命名查询。然而,在我看来,这会让您错过 Hibernate 的更好潜力。

Does this answer your question? Do you need further explanations?

这回答了你的问题了吗?你需要进一步的解释吗?

回答by Bozho

You can map the database fields in a result set to an object in hibernate: the documentationexplains how.

您可以将结果集中的数据库字段映射到 hibernate 中的对象:文档说明了如何操作。

The idea of Hibernate is to fill the object-relational gap. With the stored procedures (which I can't guess since you haven't told anything about them) you can't get objects from database. You still get rows.

Hibernate 的想法是填补对象-关系的空白。使用存储过程(我无法猜测,因为你没有告诉他们任何关于它们的信息)你不能从数据库中获取对象。你仍然得到行。

Hiding the database columns from developers sounds like a bad practice to me. Hiding them from the application is perhaps what you want, and you achieve that with the metadata file.

对开发人员隐藏数据库列对我来说是一种不好的做法。将它们从应用程序中隐藏起来可能正是您想要的,而您可以通过元数据文件实现这一点。