# How to use table partitioning with Hibernate

**URL:** <https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate/482>\
**Category:** Hibernate ORM\
**Created:** [April 3, 2018, 2:36pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate/482 "2018-04-03T14:36:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fanyanwei](https://avatars.discourse-cdn.com/v4/letter/f/74df32/32.png) [@fanyanwei](https://discourse.hibernate.org/u/fanyanwei)\
**Post date:** [April 3, 2018, 2:36pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate/482/1 "2018-04-03T14:36:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [April 3, 2018, 2:47pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate/482/2 "2018-04-03T14:47:54Z")

</div>

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](https://dev.mysql.com/doc/refman/5.7/en/partitioning.html) or [PostgreSQL](https://www.postgresql.org/docs/10/static/ddl-partitioning.html) take care of routing rows internally to different paritions.

---

<div class="post-metadata">

**Author:** ![Robert662](https://avatars.discourse-cdn.com/v4/letter/r/b5e925/32.png) [@Robert662](https://discourse.hibernate.org/u/Robert662)\
**Post date:** [November 2, 2018, 1:19pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate/482/3 "2018-11-02T13:19:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [November 2, 2018, 1:28pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate/482/4 "2018-11-02T13:28:40Z")

</div>

> Hibernate Shards is a horizontal partitioning scheme

There used to be an incubating project called [Hibernate Shards](http://hibernate.org/others/), 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](https://vladmihalcea.com/database-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](https://vladmihalcea.com/the-jpa-entitymanager-createnativequery-is-a-magic-wand/)?

Back to partitioning, most DBs support it anway (e.g.[MySQL](https://dev.mysql.com/doc/refman/8.0/en/partitioning.html), [PostgreSQL](https://www.postgresql.org/docs/10/static/ddl-partitioning.html)).
