https://github.com/marschall/hibernate-array-types
https://github.com/marschall/hibernate-array-types
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/marschall/hibernate-array-types
- Owner: marschall
- Created: 2022-09-14T22:07:09.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-20T12:06:55.000Z (over 1 year ago)
- Last Synced: 2025-01-16T02:44:53.646Z (4 months ago)
- Language: Java
- Size: 113 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Hibernate Array Types [](https://maven-badges.herokuapp.com/maven-central/com.github.marschall/hibernate-array-types) [](https://www.javadoc.io/doc/com.github.marschall/hibernate-array-types)
=====================User types for binding IN lists to arrays in Hibernate 5.
```xml
com.github.marschall
hibernate-array-types
1.0.1```
Versions 2.x are for Hibernate 6.
Advantages
----------* reduces the number of hard parses
* improves the effectives of cursor sharing
* works well together with client side statement caching to reduce the number of soft parses
* supports more than 1000 parameters in IN lists in OracleDisadvantages
-------------* requires native queries with non-portable syntax
* only a small number of databases are supported
* works only with Hibernate, not other JPA providers
* the API for Hibernate 6 is quite differentH2 / PostgreSQL
---------------```java
entityManager.createNativeQuery("""
SELECT u.*
FROM user_table u
WHERE u.id = ANY(:userids)
ORDER BY u.id
""", User.class)
// you can leave out the "INTEGER" parameter and let ArrayType figure out the actual type
.setParameter("userids", ArrayType.newParameter("INTEGER", 1, 3, 5, 7, 9))
.getResultList()
```Additionally PostgreSQL supports arrays of primitive types through the `PgIntArrayType` and `PgLongArrayType` types.
Oracle
------```java
entityManager.createNativeQuery("""
SELECT u.*
FROM user_table u
WHERE u.id = ANY(SELECT column_value FROM TABLE(:userids))
ORDER BY u.id
""", User.class)
.setParameter("userids", OracleIntArrayType.newParameter("USER_ID_TYPE", 1, 3, 5, 7, 9))
.getResultList();
```You'll have to create a type of the appropriate name, for example like this
```sql
CREATE OR REPLACE TYPE USER_ID_TYPE IS TABLE OF NUMBER;
```HSQLDB
------```java
entityManager.createNativeQuery("""
SELECT u.*
FROM user_table u
WHERE u.id IN(UNNEST(:userids))
ORDER BY u.id
""", User.class)
// you can leave out the "INTEGER" parameter and let ArrayType figure out the actual type
.setParameter("userids", ArrayType.newParameter("INTEGER", 1, 3, 5, 7, 9))
.getResultList();
```Tested Databases
----------------* H2
* HSQLDB
* Oracle
* PostgreSQLLimitations
-----------* Works only with native queries.
* HSQLDB and Oracle require custom SQL syntax.
* Supporting Oracle requires different classes and the creation of custom database types.
* Primitive types are only supported on Oracle and PostgresSQL and require different classes.
* Does not work with the following databases as yet do not support SQL arrays:
* Derby
* Firebird
* MariaDB
* MySQL
* SQL Server