openjpa-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Pinaki Poddar <>
Subject Re: Diamond model and foreign keys
Date Thu, 22 Sep 2011 15:09:15 GMT
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;

    @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:
Sent from the OpenJPA Users mailing list archive at

View raw message