Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Tricky OneToOne mapping
Tricky OneToOne mapping [message #646334] Thu, 23 December 2010 06:49 Go to next message
Eclipse UserFriend
Hi!
I have two classes, A and B, with a one to one relationship. Both classes have a primary key "OBJECT_ID", but they are joined by two other fields that comprises a composite key: "ID" and "ID_NUMBER".

The code thus far:

@Entity
public class A {
  @Id
  @Column(name = "OBJECT_ID")
  private String objectId;
  
  @OneToOne(mappedBy = "a")
  private B b;
  
  ...
}

@Entity
public class B {
  @Id
  @Column(name = "OBJECT_ID")
  private String objectId;
  
  @OneToOne
  @JoinColumns({
      @JoinColumn(name = "ID", referencedColumnName = "ID", insertable = false, updatable = false),
      @JoinColumn(name = "ID_NUMBER", referencedColumnName = "ID_NUMBER", insertable = false, updatable = false)
  })
  private A a;
  
  ...
}


So far so good. Now here's the tricky part: in some cases A and B are supposed to be joined even though their ID_NUMBERs don't match. This happens when A.ID = B.ID and B.ID_NUMBER = "999". At that time ID is unique on its own and A's ID_NUMBER is "1".

I have no idea how to accomplish this. Maybe through some customizer of some kind? Please help!
Re: Tricky OneToOne mapping [message #646371 is a reply to message #646334] Thu, 23 December 2010 09:41 Go to previous messageGo to next message
Eclipse UserFriend
You could try to move this out of the mapping into the business logic; the merge the sets in the DAO or getters methods?

> So far so good. Now here's the tricky part: in some cases A and B are supposed to be joined even though their ID_NUMBERs don't match. This happens when A.ID = B.ID and B.ID_NUMBER = "999". At that time ID is unique on its own and A's ID_NUMBER is "1".
Re: Tricky OneToOne mapping [message #647151 is a reply to message #646334] Tue, 04 January 2011 13:45 Go to previous messageGo to next message
Eclipse UserFriend
You could use an Expression to map the objects,

see,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/MappingSele ctionCriteria

But probably not the best database design.
Re: Tricky OneToOne mapping [message #647257 is a reply to message #647151] Wed, 05 January 2011 07:42 Go to previous message
Eclipse UserFriend
Thank you, James! The example you linked to was exactly what I needed to get it working.
Previous Topic:Named Query error
Next Topic:JPQL statement: wrong generated SQL code?
Goto Forum:
  


Current Time: Fri Jul 04 05:39:03 EDT 2025

Powered by FUDForum. Page generated in 0.05742 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top