是否可以在hibernate中添加一个参数生成查询? [英] Is it possible to add a parameter to hibernate generated query?

查看:107
本文介绍了是否可以在hibernate中添加一个参数生成查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在DB中有一个表student,这是非常大的。
在学生中有几个列,包括'id'和'class-id'。



在hbm文件中,



 < id name =idcolumn =IDtype =long> < generator class =native> < param name =sequence> student_ID_SEQ< / param> < param name =max_lo> 999< / param> < / generator>< / id>< property name =class-idcolumn =class-idnot-null =trueinsert =trueupdate =true/>  



在这种情况下,如果我更新学生持久化类, like:

  update .... set ....其中ID = {id} 

但是对于分区原因,我想在查询中包含类的id,如:

  update .... set ....其中ID = {id}和class-id = {class-id} 

我试过复合id,但是注意到生成器不允许在复合id,因为复合id通常是基于分配,



所以,我只是想知道,是否可以添加参数到hibernate生成的查询?

解决方案

不,不幸的是,在Hibernate中没有类似的东西。



实际上,使用数据库分区和Hibernate因为Hibernate读取关联实体的方式。让我们假设我们有实体A与实体B的多对一映射。当Hibernate读取实体A时,它将自动读取实体B.它取决于获取计划和策略如何以及何时从数据库正确加载,但是在任何情况下,它是一个查询,将不包含分区列在where子句(如果复合主键不使用)。



我希望未来的Hibernate版本



但是,您可以查看Hibernate 过滤器,也许它们可以用于您的分区需求。请记住,虽然在按id读取实体时不应用过滤器,这意味着在多对一关联中读取实体不会包括分区条件。这可以通过使用全局索引,如果你的数据库支持他们(但他们有自己的陷阱)克服。



此外,根据你使用的分区,你可以创建数据库视图,其中包括分区条件,然后将Hibernate实体映射到视图。



或者,如你所说,我也会说,这是最直接的方式,情况下,你可以使用复合主键,并使用自己的实现id生成器(实际上,应该不难实现一个,如果这是你唯一的原因不考虑复合id)。


Suppose I have a table 'student' in DB which is very LARGE. There are several columns in student, including 'id' and 'class-id'.

In hbm file I currently have the defenter code hereinition like this:

<id name="id" column="ID" type="long">
            <generator class="native">
                <param name="sequence">student_ID_SEQ</param>
                <param name="max_lo">999</param>
            </generator>
</id>
<property name="class-id" column="class-id" not-null="true" insert="true" update="true"/>

In this case, if I update the student persist class, the query will be like:

update .... set .... where ID={id}

But for partitioning reason, I want to also include the class-id in the query, like:

update .... set .... where ID={id} and class-id={class-id}

I tried composite-id, but noticed generator is not allowed in composite-id, because composite-ids are usually assign-based, not generator-based.

So, I was just wondering, is it possible to add parameters to hibernate generated queries?

解决方案

No, unfortunately there is not anything like that in Hibernate.

Actually, it is very difficult to use database partitioning together with Hibernate because of the way Hibernate reads associated entities. Let's say that we have entity A with a many-to-one mapping to entity B. When Hibernate reads entity A, it will automatically read entity B. It depends on fetch plan and strategy how and when B is exactly loaded from database, but in any case it is a query that will not contain partition column in the where clause (if composite primary keys are not used).

I hope that future versions of Hibernate will take this into consideration.

However, you can take a look at Hibernate filters, maybe they can be useful for your partitioning needs. Keep in mind though that filters are not applied when reading entities by id, meaning again that reading entities in many-to-one associations will not include partition condition. This could be overcome by using global indexes if your database supports them (but they have their own pitfalls).

Also, depending on what you use partitioning for, you could create database views which include partition condition and then map Hibernate entities to the views.

Or, as you mentioned, and I would also say that this is the most straightforward way given other circumstances, you could go with composite primary keys and use your own implementation of id generator (actually, it should not be difficult to implement one, if this is your only reason not to consider composite ids).

这篇关于是否可以在hibernate中添加一个参数生成查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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