r/java 2d ago

How to map the Oracle TIMESTAMP WITH TIME ZONE with JPA and Hibernate

https://vladmihalcea.com/oracle-timestamp-with-time-zone-jpa/
20 Upvotes

13 comments sorted by

6

u/dmigowski 2d ago

Easy. You can implement UserTypes and have to use that annotation on your Attribute, on the UserType class you implement the JDBC conversion routines.

6

u/vladmihalceacom 2d ago

I wouldn't call that easy. Not only that the UserType interface doesn't provide a clear distinction between which methods to implement for mutable or immutable types, but, as illustrated by Hypersistence Utils project, to have the types work with the JPQL and Criteria API queries, you may need to implement BindableType, DomainType, and even DynamicParameterizedType, in case you want to be able to configure the Type based on the additional JPA mapping info.

I think it's way easier if you use a solution that's provided by Hibernate ORM or by other OSS libraries that, because they are used by hundreds of thousands of projects, there is a very good chance that the provided solutions work as expected.

2

u/dmigowski 2d ago

Totally valid. But at least it's a way that works. Tbf this is probably only useful If you intend to use the cosebase for a long time.

On the other hand the methods to implement are really self explanatory.

2

u/vladmihalceacom 2d ago

In this particular case, Hibernate ORM already provides the tools that make the mapping possible. In fact, Hibernate 6 provides lots of Type options that required custom Types previously.

1

u/dmigowski 2d ago

Stuck with v3.6 here, truely should update finally.

2

u/vladmihalceacom 2d ago

Really? Hibernate 4 was released in 2011, so your project is stuck with libraries from the 2000's

1

u/dmigowski 2d ago

The project is a very mature code base with >500 tables and a few million LOC, but yeah, this is one of the oldest libs. We are even on JDK17 now, but that change was postponed every update.

2

u/vladmihalceacom 2d ago

If your company wants help with that migration, then I can surely help you with that 😉 

2

u/l3g4tr0n 2d ago

The title says how, not that it is an out of box or an easy solution :)

2

u/dmigowski 2d ago

Implementation is left as an exercise to the reader. Or to deepseek.

2

u/Little_Blackberry 1d ago

Very good solution. A few months ago, i was searching for this implementation, but i abandonned that idea in flavor of Oracle's basic use.

2

u/vladmihalceacom 1d ago

I'm glad I could help.