OneToMany without primary keys

6,469 views
Skip to first unread message

Chris

unread,
Sep 21, 2012, 4:35:05 PM9/21/12
to eb...@googlegroups.com
Hi

I'm trying to define a OneToMany / ManyToOne relationship. 

I have the following JPA:
class Definition {
    @Id
    private long id;

    @OneToMany(cascade = CascadeType.ALL, targetEntity = LexicalForm.class, mappedBy = "strongNumber")
    @JoinColumn(name = "raw_strong_number")
    private List<LexicalForm> lexicalForms;

    @Column(unique = true)
    private String strongNumber;

}

And 
class LexicalForm {
    @ManyToOne
    @JoinColumn(name = "raw_strong_number", referencedColumnName = "strong_number")
    private Definition strongNumber;

    private String rawStrongNumber;
}

When I run a query, it produces the wrong join, using the id from Definition and joining it to strongNumber from Lexical form.

select distinct t0.id c0, t0.accented_unicode c1, t0.step_transliteration c2, t0.step_gloss c3 
from definition t0
join lexical_form u1 on u1.raw_strong_number = t0.id  
where (u1.raw_form like ?  or u1.unaccented_form like ? ) 

If I add the strong_number to my Definition class in the join column:     @JoinColumn(name = "raw_strong_number", referencedColumn = "strongNumber"), I get loads of errors spurned out:

Error with the Join on [com.tyndalehouse.step.core.data.entities.lexicon.Definition.lexicalForms]. Could not find the matching foreign key for [id] in table[lexical_form]? Perhaps using a @JoinColumn with the name/referencedColumnName attributes swapped?

Same thing occurs if I swap round the referencedColumn and name columns.

Any ideas? Is this case supported?
Cheers
Chris


mbell697

unread,
Sep 24, 2012, 1:00:23 PM9/24/12
to eb...@googlegroups.com
One thing I would consider is the semantics of this structure.  Without a primary key on the LexicalForm table you can't possible store a List of them.  Lists have order.  Without a key there is no way to do that.  If you don't need the Collection to be ordered use a Set in place of a List.

As for the error, look at your database structure, do you have a foreign key setup correctly between these tables?

Cheers,

Mark  

mbell697

unread,
Sep 24, 2012, 1:17:38 PM9/24/12
to eb...@googlegroups.com
Additional thoughts: I have no idea if Ebean would allow referencing by non-primary keys or not , but JPA 2 definitely doesn't.  

Also you have something wonky in your mapping:

@OneToMany(cascade = CascadeType.ALL, targetEntity = LexicalForm.class, mappedBy = "strongNumber")

Should be

@OneToMany(cascade = CascadeType.ALL, targetEntity = LexicalForm.class, mappedBy = "rawStrongNumber")

In other words the 'mappedBy' attribute should point to the property in the target class.  That said I don't really know what happens when you define both a 'mappedBy' attribute and a @JoinColumn definition since they are defining the same thing.

It would be vastly easier to just use a primary id key in the LexicalForm entity.

Cheers,

Mark

Chris

unread,
Sep 26, 2012, 11:34:06 AM9/26/12
to eb...@googlegroups.com
Thanks for all the comments - I'll try what you've said. The only reason mappedBy is in there twice, is that it's something I've tried, and didn't work. I've tried without and didn't work either. 

Cheers
Chris

Oliver Kocsis

unread,
Aug 23, 2013, 5:22:33 AM8/23/13
to eb...@googlegroups.com
I know it is an old post, but try to delete the JoinColumn from the OneToMany relationship side.

Reply all
Reply to author
Forward
0 new messages