persistence@glassfish.java.net

Re: _at_OneToMany without join table with TopLink

From: Wonseok Kim <guruwons_at_gmail.com>
Date: Tue, 18 Dec 2007 22:02:43 +0900

The default mapping rule for *uni-directional* one-to-many is using join
table (spec content).
Please add @ManyToOne to the other side to make it *bi-directional *
relationship.

There is already an enhancement request
issue<https://glassfish.dev.java.net/issues/show_bug.cgi?id=2049>for
using join column for uni-directional one-to-many and it will be added
in JPA 2.0 spec.

HTH
-Wonseok

On Dec 18, 2007 8:51 PM, Aleksei Valikov <aleksei.valikov_at_gmail.com> wrote:

> Hi.
>
> I'm trying to implement a @OneToMany association without the join
> table under TopLink. Consider two entities One and Two and One has the
> following property:
>
> public List<Two> getOneToManyDefault() {...}
>
> If I just annotate it as follows:
>
> @OneToMany(cascade = {
> CascadeType.ALL
> })
> public List<Two> getOneToManyDefault() {...}
>
> I get
>
> CREATE TABLE ONE (HJID NUMERIC(19) NOT NULL, DTYPE VARCHAR(31),
> PRIMARY KEY (HJID))
> CREATE TABLE TWO (HJID NUMERIC(19) NOT NULL, DTYPE VARCHAR(31),
> PRIMARY KEY (HJID))
>
> CREATE TABLE ONE_TWO (One_HJID NUMERIC(19) NOT NULL,
> oneToManyDefault_HJID NUMERIC(19) NOT NULL, PRIMARY KEY (One_HJID,
> oneToManyDefault_HJID))
>
> ALTER TABLE ONE_TWO ADD CONSTRAINT FK_ONE_TWO_oneToManyDefault_HJID
> FOREIGN KEY (oneToManyDefault_HJID) REFERENCES TWO (HJID)
> ALTER TABLE ONE_TWO ADD CONSTRAINT ... FOREIGN KEY (One_HJID)
> REFERENCES ONE (HJID)
>
> So there is an implicit join table generated.
> I would like to avoid the join table and have the following:
>
> create table ONE (HJID bigint generated by default as identity (start
> with 1), primary key (HJID))
> create table TWO (HJID bigint generated by default as identity (start
> with 1), ONETOMANYDEFAULT_ONE_ID bigint, primary key (HJID))
> alter table TWO add constraint FK1462C312740FD foreign key
> (ONETOMANYDEFAULT_ONE_ID) references ONE
>
> This is one table less. Under Hibernate this is produced by the
> following annotation:
>
> @OneToMany(cascade = {
> CascadeType.ALL
> })
> @JoinColumn(name = "ONETOMANYDEFAULT_ONE_ID")
>
> I wonder what the solution for TopLink will be.
>
> Thank you in advance for your time.
>
> Bye.
> /lexi
>