How to use table partitioning with Hibernate

Hello,
when we were using hibernate, we encountered the need for database partition tables.

For example, we need to store different Student objects in different tables according to different conditions, like the table student_1, student_2.

We find hibernate interceptor can implement partition table. But it seems not very suitable.

Does Hibernate has some other method to solve this problem?

Hibernate does not need to do anything special if you need to use partitioning because the database can take care of that.

From Hibernate perspective you just write to the Student table, and MySQL or PostgreSQL take care of routing rows internally to different paritions.

1 Like

I’m not sure what you mean by “partitions”. Hibernate Shards is a horizontal partitioning scheme, but partitioning is also a term sometimes used to indicate fine-grained control of a single database, and in that context, Hibernate isn’t likely to be able to help, since that sort of feature is generally going to vary with the database vendor.

DBMS-specific partitioning is also not usually something that apps would be doing, since it’s mostly about performance and physical robustness, which are generally best maintained across the enterprise by the DBA, whose job it is to balance resources for the enterprise as a whole.

Hibernate Shards is a horizontal partitioning scheme

There used to be an incubating project called Hibernate Shards, but it was discontinued due to lack of contributions from the community.

but partitioning is also a term sometimes used to indicate fine-grained control of a single database, and in that context, Hibernate isn’t likely to be able to help, since that sort of feature is generally going to vary with the database vendor.

You can also use multitenancy, which Hibernate supports and can be used for isolating users if that’s your use case.

DBMS-specific partitioning is also not usually something that apps would be doing, since it’s mostly about performance and physical robustness, which are generally best maintained across the enterprise by the DBA, whose job it is to balance resources for the enterprise as a whole.

That does not sound like DevOps at all. The DB is no island, just like the application. They form a system, so partitioning is yet another task that needs to be addressed by the team who’s developing and monitoring the system.

Now, just because you are using Hibernate, it does not mean you cannot benefit from DB-specific features. Otherwise, why do you think there is a createNativeQuery method?

Back to partitioning, most DBs support it anway (e.g.MySQL, PostgreSQL).