Venkata Rahul S
Venkata Rahul S

Reputation: 324

Customize the Unidirectional Join Relation JDO

Iam trying to use the Datanucleus API to configure a M:N relation between two tables "which do not know about each other" as follows:

Books:Authors

@PersistenceCapable
public class Book{
    @Persistent(primaryKey = "true")
    @Column(name = "isbn", allowsNull = "false")
    private int userId;

    private String isbnCode;

    private Str5ing title;

    @Persistent(table="BOOK_AUTHOR")
    @Join(column="isbn")
    @Element(column="authorId")
    Collection<Author> bookAuthors = new HashSet<Author>();
}


@PersistenceCapable
public class Author{
    @Persistent(primaryKey = "true")
    @Column(name = "author_id", allowsNull = "false")
    private int authorId;

    private String firstName;

    private Str5ing lastName;

    @Persistent(table="BOOK_AUTHOR")
    @Join(column="authorId")
    @Element(column="isbn")
    Collection<Book> bookAuthors = new HashSet<Book>();
}

Now, as per this link of JDO's mapping, I expect a third table, to be created as the join table, with a new Id column.

What bugs me are two questions:

  1. An identity column as mentioned in the link above is NOT getting created.
  2. I want to customize the third table to add a little more information. How do I do that? Say, if I want to add a release date or a publisher name, then I need control over the BOOK_AUTHOR table. (Though I am trying to accomplish this task thru the PersistanceAware annotation, I am not sure this is a possibility because the annotation at the table level means that I cannot tinker with any columns in that table, I can only view the information.

How do I get over the problem 2? I can live with problem 1 for a version or two but that needs to be resolved too.

Can the Zens of JDO please help?

Upvotes: 0

Views: 158

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

Things to fix.

  1. You have missed off the mappedBy to link the 2 relation fields; you need to add that so then it is bidirectional. If this was supposed to be 2 independent 1-N relations then I don't see how you can share a join table, since data inserted from one side would get out of step with the data from the other side
  2. A "join table" cannot have "more information" because your Java model doesn't have "more information". JDO (or JPA) can only map what you model in Java. If you want extra data then you firstly need to put it in your Java model, and that usually means adding an intermediate persistable class containing it. See this sample in their GitHUb.

Not sure what you mean by "An identity column as mentioned in the link above is NOT getting created."; You mean the "ADPT_PK_IDX" in the link ? don't think that applies in all cases.

For an M-N relation I would refer to this link as opposed to yours which was 1-N

Upvotes: 1

Related Questions