Search code examples
hibernatespring-dataspring-data-jpaoracle-objects

How to map Oracle Object type in an entity using Spring Data JPA?


This question is particularly related to mapping Oracle object type in an entity using Spring Data JPA. The other question did not help explain the problem in detail.

So here below are details:

I could not find information related to mapping Oracle object type in entity objects with Spring Data JPA neither in Spring Data JPA Reference nor in any other search results.

Scenario:

Let's say I have the following date_typ Oracle type.

CREATE TYPE date_typ AS OBJECT (
  month         VARCHAR2(2),
  day           VARCHAR2(2),
  year          VARCHAR2(4),
  extension     VARCHAR2(10),
  MEMBER FUNCTION getFullDate RETURN VARCHAR2
);

CREATE TYPE BODY date_typ AS
  MAP MEMBER FUNCTION getFullDate RETURN VARCHAR2 AS
  BEGIN
    return month || day || year;
 END getFullDate;
END;

Now in the oracle database table, say REGISTRATION, one of the columns is of the above date_typ

Column      Type
REG_ID      VARCHAR2(25)  primary key
FIRSTNAME   VARCHAR2(30)  not null
LASTNAME    VARCHAR2(30)  not null
MIDDLEINIT  VARCHAR2(10) 
REQUESTEDDATE DATE_TYP
...
...

Now, I need to map the above columns in the entity

@Entity
@Table(name = "REGISTRATION", schema = "accounts")
public class RegistrationEntity {
    @Id
    @Column(name = "REG_ID")
    private String registrationId;

    @Column(name = "FIRSTNAME", nullable = false)
    private String firstName;

    @Column(name = "LASTNAME", nullable = false)
    private String lastName;

    @Column(name = "MIDDLEINIT")
    private String middleInitial;

    requestedDate ???
}

How do I map the requestedDate to the REQUESTEDDATE column of the DATE_TYP (oracle object type) in Spring JPA?

Notes: am using Hibernate as the persistence provider.


Solution

  • This answer states, that there is no official support in JPA for Oracles Object Types, but support for it in Eclipse Link. I guess that means there is no out of the box support in Hibernate for this.

    But this blog post describes how to implement a UserType to map a Java class to an Oracle Object type.

    Spring Data is not really involved in this because it just uses the JPA implementation to map enities to the database. Conversions and projections offered by Spring Data only convert input parameters/output values before passing them to/ after getting them from the JPA provider.