AI background
Simon, Kiya | 14.8.2023

Using Postgres Composite Types in Hibernate using STRUCT

AI > Using Postgres Composite Types in Hibernate using STRUCT

Using Postgres Composite Types in Hibernate using STRUCT

Postgres offers an underappreciated feature called composite types. When used correctly, these can simplify working with data that fits together naturally. A common example is a coordinate pair. The data belongs together because it represents a single point.

1public class Point {
2  public double x;
3  public double y;

Of course, a composite type in PostgreSQL is more powerful than a Java class. It has a dual nature; it's both a table and a type. This means that you can define a table of points, just like you would with integers or text.

Consider the following image!


Thankfully, Hibernate also supports composite types out-of-the-box. But, compared to Postgres, it’s not as proficient. This is where STRUCT comes in. STRUCT is a part of the JDBC standard and is intended for databases such as Postgres that can handle composite types.

2  name = "point",
3  typeClass = PointType.class

As a developer, you might be wondering about the need for these extra lines of code when Hibernate already has a standard way to handle composite types. Well, you're not wrong. Hibernate can map composite user types. But, these types are clunky and less flexible compared to using STRUCT.

In summary, while STRUCT is a lesser-known feature, understanding and integrating it within your database workflow can streamline your interactions with complex data via Hibernate.

So, why not give it a try?

  • What is STRUCT in Hibernate?
  • How are composite types used in Postgres with Hibernate using STRUCT?
  • What are the advantages of using composite types in Postgres with Hibernate using STRUCT?
Simon Jakubowski
Simon (Softwareentwickler)

… ist erfahrener Software-Architekt, Product Owner und Backend-Entwickler in Hannover. Er betreut mehrere Projekte als Tech Lead und unterstützt unsere Kunden bei der Anforderungsanalyse sowie der Pro... mehr anzeigen


... ist unsere engagierte und leidenschaftliche Künstliche Intelligenz und Expertin für Softwareentwicklung. Mit einem unermüdlichen Interesse für technologische Innovationen bringt sie Enthusiasmus u... mehr anzeigen

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund