|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
1-1 uni-directional mapping; possible with owner not holding foreign keys?Refer to the attached ER diagram
![]() (the actual diagram has several tables hanging off ChartEvent) or the @Entity below. Current to add something to aspectDisplay table do: void putAspectdisplayData(ChartEvent ce, Set<AspectId> data) { Aspectdisplay ad = new Aspectdisplay(); ad.setChevPK(ce.getChevPK()); ad.setChartEvent(ce); ad.setDat(new AspectdisplayData(add03)); em.persist(ad); doCommit(); ce.setAspectdisplay(ad); } I thought there might be a way to redefine the mappings so that the owner of the relationship is ChartEvent and make the relationship uni-directional (Aspectdisplay does not need a reference to ChartEvent). Ideally, it could be specified so that the add looks like void putAspectdisplayData(ChartEvent ce, Set<AspectId> data) { Aspectdisplay ad = new Aspectdisplay(); ad.setDat(new AspectdisplayData(add03)); ce.setAspectdisplay(ad); em.persist(ce); // not really needed assuming ce in persistence context doCommit(); } Note that the primary key is not being set, it seems there's enough info around so the persistence layer could assign it. I tried a variety of things to make ChartEvent the relationship owner, usually involving specifying @JoinColumns and/or @JoinTable; most attempts failed with "Multiple writable mappings exist for the field" exception, except when I used a 'table="ASPECTDISPLAY"', then I got something about the ASPECTDISPLAY not being in the Entity, maybe it thought this was a secondary table. Is it possible to make ChartEvent the owner of a uni-directional 1-1? If the owner *must* be the holder of the foreign key, then out of luck. Is there some mapping that doesn't care about foreign keys that just does the join? Curiously, while keeping Aspectdisplay the owner, putting "ad.setChevPK(ce.getChevPK())" into the second put method and using it updates the database even though ChartEvent is the inverse side. But the spec, 3.2.4, says Bidirectional relationships between managed entities will be persisted based on references held by the owning side of the relationship. so I guess this is non-portable (and a spec violation). Is something like this worth filing a bug report? @Entity @Table(name = "CHART_EVENT", catalog = "", schema = "ERNIE") public class ChartEvent implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected ChevPK chevPK; @OneToOne(mappedBy = "chartEvent") private Aspectdisplay aspectdisplay; @OneToMany(mappedBy = "chartEvent", orphanRemoval=true) private Collection<Aspectgroup> aspectgroupCollection; @Entity @Table(name = "ASPECTDISPLAY", catalog = "", schema = "ERNIE") public class Aspectdisplay implements Serializable { private static final long serialVersionUID = 1L; @EmbeddedId protected ChevPK chevPK; @Basic(optional = false) @Lob @Column(name = "DAT", nullable = false) private AspectdisplayData dat; @JoinColumns({ @JoinColumn(name = "CHART_ID", referencedColumnName = "CHART_ID", nullable = false, insertable = false, updatable = false), @JoinColumn(name = "EVENT_ID", referencedColumnName = "EVENT_ID", nullable = false, insertable = false, updatable = false)}) @OneToOne(optional = false) private ChartEvent chartEvent; |
|
|
Re: 1-1 uni-directional mapping; possible with owner not holding foreign keys?Your mappings seem confused, I'm not sure how you can have a OneToOne and a OneToMany using the same mappedBy.
There is some info on cascaded primary keys which you seem to have which may be of some use, http://en.wikibooks.org/wiki/Java_Persistence/OneToOne#Target_Foreign_Keys.2C_Primary_Key_Join_Columns.2C_Cascade_Primary_Keys Personally, I would recommend avoiding the complexity of composite primary keys and just generated an id for each class/table. This would most likely make things much simpler. James Sutherland EclipseLink, TopLink Wiki: EclipseLink, TopLink Forums: TopLink, EclipseLink Book: Java Persistence |
|
|
Re: 1-1 uni-directional mapping; possible with owner not holding foreign keys?> Your mappings seem confused, I'm not sure how you can have a OneToOne
> and a OneToMany using the same mappedBy. Both Aspectgroup and Aspectdisplay have a field chartEvent, notice in ERD they each refer back to ChartEvent through a primary/foreign key; The mappings seem to work, though possibly sub-optimal and confusing. > There is some info on cascaded primary keys which you seem to have which may be of some use, > > http://en.wikibooks.org/wiki/Java_Persistence/OneToOne#Target_Foreign_Keys.2C_Primary_Key_Join_Columns.2C_Cascade_Primary_Keys Doh! I missed that in the early chapters of the 2.0 spec. Thanks very much for the pointer. > Personally, I would recommend avoiding the complexity of composite primary keys > and just generated an id for each class/table. This would most likely make things much simpler. So are you suggesting that the join table ChartEvent have a unique/auto-gen field and that the other tables refer to that? I considered that and then considered that there are lots of ChartEvent and relatively few rows of the other tables hanging off it (used only by the apps when the defaults don't satisfy). Being a novice, I have little feel for the tradeoffs in design complexity and db size. I'll take another look. |
|
|
Re: 1-1 uni-directional mapping; possible with owner not holding foreign keys?While checking out the link you provided, I ran into the solution. I neglected to mention I am using JPA 2.0 (eclipselink). JPA 2.0 has added stuff to deal with the problems in JPA 1.0; in the 2.0 spec see: 2.4.1 Primary Keys Corresponding to Derived Identities.
Unfortunately, the implementation is incomplete. I went to file a bug, but one was filed for this issue only a couple days ago. Thanks again. |
| Free embeddable forum powered by Nabble | Forum Help |