简体   繁体   English

是否可以添加参数以休眠生成的查询?

[英]Is it possible to add a parameter to hibernate generated query?

Suppose I have a table 'student' in DB which is very LARGE. 假设我在数据库中有一个表“ student”,它很大。 There are several columns in student, including 'id' and 'class-id'. 学生中有几列,包括“ id”和“ class-id”。

In hbm file I currently have the defenter code hereinition like this: 在hbm文件中,我目前具有以下防御者代码:

 <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: 但是出于分区的原因,我还想在查询中包括class-id,例如:

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. 我尝试了Composite-ID,但是发现Composite-ID中不允许使用生成器,因为Composite-ID通常是基于分配的,而不是基于生成器的。

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. 不,不幸的是,Hibernate中没有类似的东西。

Actually, it is very difficult to use database partitioning together with Hibernate because of the way Hibernate reads associated entities. 实际上,由于Hibernate读取关联实体的方式,很难将数据库分区与Hibernate一起使用。 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). 假设我们有一个实体A,它具有到实体B的多对一映射。当Hibernate读取实体A时,它将自动读取实体B。这取决于获取计划和策略,如何以及何时从数据库中准确加载B,但是无论如何,它是一个查询,该查询将不包含where子句中的分区列(如果未使用复合主键)。

I hope that future versions of Hibernate will take this into consideration. 我希望将来的Hibernate版本将考虑到这一点。

However, you can take a look at Hibernate filters , maybe they can be useful for your partitioning needs. 但是,您可以看一下Hibernate 过滤器 ,也许它们可以满足您的分区需求。 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. 请记住,虽然在按ID读取实体时未应用过滤器,但再次意味着,多对一关联中的读取实体将不包含分区条件。 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. 同样,根据您使用分区的目的,您可以创建包含分区条件的数据库视图,然后将Hibernate实体映射到这些视图。

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). 或者,正如您提到的,我还要说的是,在其他情况下,这是最直接的方法,您可以使用复合主键并使用自己的id生成器实现(实际上,如果有一个,则不难实现)这是您不考虑复合ID的唯一原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM