Page 1 of 1

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

Posted: Wed Jan 06, 2016 6:48 am
by lveto15
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)
)

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

Posted: Mon Sep 17, 2018 5:52 pm
by __JJ__
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.

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

Posted: Mon Sep 17, 2018 7:18 pm
by admin
The two could be legacy tables.

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

Posted: Tue Sep 18, 2018 12:10 pm
by __JJ__
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.