Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OneToOne mapping with JoinTable #196

Closed
beikov opened this issue Mar 16, 2017 · 4 comments
Closed

Support OneToOne mapping with JoinTable #196

beikov opened this issue Mar 16, 2017 · 4 comments
Labels

Comments

@beikov
Copy link

beikov commented Mar 16, 2017

Consider this simple entity model

@Entity
public class Document implements Serializable {
    @Id @GeneratedValue
    private Long id;

    @OneToOne
    @JoinTable(
            name = "document_extra",
            joinColumns = @JoinColumn(name = "document_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "document_info_id", referencedColumnName = "id")
    )
    private DocumentInfo documentInfo;
}

@Entity
public class DocumentInfo implements Serializable {
    @Id @GeneratedValue
    private Long id;
    private String someInfo;
}

When creating a EntityManagerFactory with the property "javax.persistence.schema-generation.database.action" set to "create" it fails. The error is a syntax error in the generated DDL statement ALTER TABLE DOCUMENT ADD CONSTRAINT DOCUMENT_U1 UNIQUE ().

There are multiple problems here that I found. Apparently a @OneToOne with a @JoinTable is not really supported yet? Anyway, the problems essentially seem to be that the @JoinTable is partly/fully ignored. This is because a join table with a different name i.e. "document_documentinfo" and different column names is generated instead.
There should be 2 unique constraints generated for the join table. One for the "document_id" and another for the "document_info_id" instead of an empty constraint for the "document" table.
The ignoring of the @JoinTable also suggests that a simple @OneToOne without a @JoinColumn annotation would fail too, although in that case a default join column name should be chosen.

The issues are DBMS independent and produce consistently bad DDL on H2, MySQL and PostgreSQL.

@andyjefferson
Copy link
Member

andyjefferson commented Mar 16, 2017

As the docs ( http://www.datanucleus.org/products/accessplatform_5_0/jpa/mapping.html#one_one_relations ) say clearly enough, 1-1 with join table is not supported and minority interest use-case. [Even the EclipseLink/Wikibooks docs says that is not part of the JPA spec].

PS, this relates solely to the datanucleus-rdbms plugin so not really the place for it.

@andyjefferson andyjefferson changed the title OneToOne mapping with JoinTable results in wrong DDL Support OneToOne mapping with JoinTable Mar 16, 2017
@beikov
Copy link
Author

beikov commented Mar 16, 2017

Must have missed that then. Thanks for the info.

@andyjefferson
Copy link
Member

Marking as wont-fix since the work would have to be on the RDBMS plugin only (nothing in core cares about join tables and the like), and opened datanucleus/datanucleus-rdbms#180, so use that as reference. Not likely to attract my attention in some time ;-)

@chrisco484
Copy link

I've never had a need to have a one to one implemented as a join rather than as a bidirectional set of FKs. I imagine it would be slower than a simple bidirectional set FKs because it needs to involve a third table. A joined one to one is an interesting use case so I'm wondering if there is an advantage to this approach that I've missed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants