# How to use table partitioning with Hibernate (parts 1 month)

**URL:** <https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate-parts-1-month/6241>\
**Category:** Hibernate ORM\
**Created:** [April 11, 2022, 1:25pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate-parts-1-month/6241 "2022-04-11T13:25:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![al01112011](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/al01112011/32/1857_2.png) [@al01112011](https://discourse.hibernate.org/u/al01112011)\
**Post date:** [April 11, 2022, 1:25pm UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate-parts-1-month/6241/1 "2022-04-11T13:25:52Z")

</div>

Hello, we encountered the need for database partition tables.

We need to divide the table into parts 1 month (datetime timestamp).  
Partitioning Using Inheritance.

CREATE TABLE test (  
id int8 NOT NULL,  
versionobject int8 NOT NULL DEFAULT 1,  
person\_id int8 NULL,  
datetime timestamp NOT NULL,  
CONSTRAINT test\_pkey PRIMARY KEY (id)  
);  
CREATE TABLE test\_2022\_01\_01 (  
CHECK (datetime \>= DATE ‘2022-01-01’ AND datetime \< DATE ‘2022-02-01’)  
) INHERITS (test);

There is a trigger that writes data to the desired part.

Cannot add row

NHibernate.StaleStateException: “Unexpected row count: 0; expected: 1”  
Modules.ORMNHibernate.TestRepository.Add.AnonymousMethod\_\_0() in TestRepository.cs  
Modules.ORMNHibernate.TestRepository.ExecuteAndRollbackIfException(System.Func) in TestRepository.cs  
Modules.ORMNHibernate.TestRepository.Add(System.Collections.Generic.IEnumerable, string, bool) in TestRepository.cs

Thanks

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [April 25, 2022, 7:30am UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate-parts-1-month/6241/2 "2022-04-25T07:30:39Z")

</div>

This is a forum for Hibernate, the Java ORM, not NHibernate. Please choose one of the appropriate groups for NHibernate from [Groups - NHibernate](https://nhibernate.info/groups.html)

Relatedly, if NHibernate has the same concepts, you would have to use `@SQLInsert(sql = "insert into test(id, versionobject, person_id, datetime) values (?, ?, ?, ?)", check = ResultCheckStyle.NONE)` to tell Hibernate that the JDBC statement update count should be ignored since the trigger doesn’t return the proper update count via JDBC.

---

<div class="post-metadata">

**Author:** ![al01112011](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/al01112011/32/1857_2.png) [@al01112011](https://discourse.hibernate.org/u/al01112011)\
**Post date:** [May 4, 2022, 6:25am UTC](https://discourse.hibernate.org/t/how-to-use-table-partitioning-with-hibernate-parts-1-month/6241/3 "2022-05-04T06:25:28Z")

</div>

Thanks! This should help me.
