简体   繁体   中英

Is it possible to add a parameter to hibernate generated query?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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