简体   繁体   中英

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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM