Hi Pinaki, thanks for the advice. Actually, a solution similar to b) (with Object as a part of EmbeddedKey) was implemented in my original project as the error first occured. I'll try the annotation with @ForeignKey first. In every case, it's rather strange that OpenJPA makes everything correct having less information (no link between Price and Room), but writes an error when it has extra information that actually makes the whole thing consistent.
By the way, the duplicate mapping comes from the OpenJPA reverse mapping tool. Wouldn't it be reasonable if the tool would generate the classes without this double mapping? Kind regards, Michael Spiro > -----Ursprüngliche Nachricht----- > Von: Pinaki Poddar [mailto:[email protected]] > Gesendet: Donnerstag, 22. September 2011 17:09 > An: [email protected] > Betreff: Re: Diamond model and foreign keys > > Hi Michael, > The problem could be in domain model/mapping definition. The model > follows > a "duplicate mapping" approach. Let me explain what I mean by > "duplicate > mapping" in the context of your application. > > public class Seasontime { > @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE) > @JoinColumn(name="hotel_idhotel", columnDefinition="INT") > private Hotel hotel; > > @Id > @Column(name="hotel_idhotel", columnDefinition="INT") > private int hotelIdhotel; > > Now in this model, both fields "Hotel hotel" and "int hotelIdhotel" > refer to > the same "logical" thing i.e. a Hotel object, but in two different > ways. A > JPA runtime, however, has little clue that they mean the same thing. > To make JPA understand this "implicit" sameness of these two fields, > there > are two possibilities > a) annotate "int hotelIdhotel" with @ForeignKey(implicit=true) (see > @ForeignKey documentation for details) > b) remove the ""int hotelIdhotel" field altogether. Instead annotate > the > "Hotel hotel" as @Id. JPA 2.0 supports a object reference as a primary > key > either as a simple key or part of a compound key. I prefer this > approach > because it avoids the "duplicate mapping" problem and results into > cleaner > domain model. > Examples of such modeling technique can be found in OpenBooks example > distributed with OpenJPA. > > ----- > Pinaki Poddar > Chair, Apache OpenJPA Project > -- > View this message in context: > http://openjpa.208410.n2.nabble.com/Diamond-model-and-foreign-keys- > tp6819272p6820601.html > Sent from the OpenJPA Users mailing list archive at Nabble.com.
