Rustem
Rustem

Reputation: 13

How to map to multiple tables from single column using JPA

I have 5 tables MainReg, Person, PersonInfo, Company, CompanyInfo

CREATE TABLE [MainReg] (
[IdMainReg] [uniqueidentifier] NOT NULL,
[PersonalObjectId] [int] NULL,
[OwnerId] [nvarchar](36) NULL,
...)

CREATE TABLE [Person] (
[IdPerson] [uniqueidentifier] NOT NULL,
[PersonInfoId] [nvarchar](36) NULL,
...)

CREATE TABLE [PersonInfo] (
[IdPersonInfo] [uniqueidentifier] NOT NULL,
...)

CREATE TABLE [Company] (
[IdCompany] [uniqueidentifier] NOT NULL,
[CompanyInfoId] [nvarchar] NULL,)
...)

CREATE TABLE [CompanyInfo] (
[IdCompanyInfo] [uniqueidentifier] NOT NULL,
...)

[MainReg.OwnerId] column references row in table [Company] if [MainReg.PersonalObjectId] = 1 and row in table [Person] if [MainReg.PersonalObjectId] = 2.

My question is, how to map this tables using JPA?

Upvotes: 1

Views: 1062

Answers (2)

James
James

Reputation: 18389

In EclipseLink you can use a @VariableOneToOne relationship to map this.

Otherwise, you may be able to use TABLE_PER_CLASS inheritance for it, or change your data model.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692231

You won't be able to map that in pure JPA. It would need to different foreign keys to be able to map that in JPA: one which would reference Company, and another one which would reference Person.

With Hibernate, you would be able to map that using the Any annotation.

Upvotes: 1

Related Questions