About Question enthuware.oce-jpad.v6.2.431 :

Moderator: admin

Post Reply
lveto15
Posts: 27
Joined: Tue Sep 22, 2015 1:01 pm
Contact:

About Question enthuware.oce-jpad.v6.2.431 :

Post by lveto15 » Wed Jan 06, 2016 6:48 am

Example from the explanation is not complete. According to comment in the first option both columns should be UNIQUE . First is primary key of the join table thus it will be unique by definition. But the second one is defined by partially default @JoinColumn, where "unique" is false. So, table in db will be definied as following:
CREATE TABLE acct_acctdet_jointable
(
act_details_id integer,
acct_id integer NOT NULL,
CONSTRAINT acct_acctdet_jointable_pkey PRIMARY KEY (acct_id),
CONSTRAINT fk949un3kgn8s2vbqb0sff1sto8 FOREIGN KEY (acct_id)
REFERENCES account (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fkn66tbtylqpyf93leaxe4nk2p8 FOREIGN KEY (act_details_id)
REFERENCES account_details (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
Second column will not be unique and the same additional data can be associated with two different Account.

To make it better unique attribute has to be set to true:
@JoinTable(name = "ACCT_ACCTDET_JOINTABLE",
joinColumns = @JoinColumn(name = "ACCT_ID"),
inverseJoinColumns = @JoinColumn(name = "ACT_DETAILS_ID", unique = true))
After that table in db looks:
CREATE TABLE acct_acctdet_jointable
(
act_details_id integer,
acct_id integer NOT NULL,
CONSTRAINT acct_acctdet_jointable_pkey PRIMARY KEY (acct_id),
CONSTRAINT fk949un3kgn8s2vbqb0sff1sto8 FOREIGN KEY (acct_id)
REFERENCES account (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT fkn66tbtylqpyf93leaxe4nk2p8 FOREIGN KEY (act_details_id)
REFERENCES account_details (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT uk_9h1b7pr1yblcjd8jw2tx3wieg UNIQUE (act_details_id)
)

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.431 :

Post by __JJ__ » Mon Sep 17, 2018 5:52 pm

Hi
I don't understand why the join table is needed in the first place.
If everything is one-to-one why is it necessary to even have the join table? Is this just a contrived example?
In "real life", why would anyone have a join table between tables T1 and T2 when the relationship between T1 and T2 is one-to-one? What is the benefit or justification? I cannot see what it brings to the table. It seems to only consist of a FK to T1 and a FK to T2. If T1 and T2 are one-to-one you may as well just make T2's PK a FK to T1.PK.
Thanks in advance.

admin
Site Admin
Posts: 8394
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.431 :

Post by admin » Mon Sep 17, 2018 7:18 pm

The two could be legacy tables.
If you like our products and services, please help us by posting your review here.

__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

Re: About Question enthuware.oce-jpad.v6.2.431 :

Post by __JJ__ » Tue Sep 18, 2018 12:10 pm

Yes I understand that; I was just wondering why anyone would have a join table that only consists of two columns, each column being a FK to one of the tables it is joined to. You may as well dispense with the join table and join the main two tables together. But never mind, as this is a topic for schema design, not JPA.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest