Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Tricky OneToOne mapping
Tricky OneToOne mapping [message #646334] Thu, 23 December 2010 11:49 Go to next message
Matti Hansson is currently offline Matti HanssonFriend
Messages: 68
Registered: July 2009
Member
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 14:41 Go to previous messageGo to next message
Tom Eugelink is currently offline Tom EugelinkFriend
Messages: 807
Registered: July 2009
Senior Member
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 18:45 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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.


James : Wiki : Book : Blog : Twitter
Re: Tricky OneToOne mapping [message #647257 is a reply to message #647151] Wed, 05 January 2011 12:42 Go to previous message
Matti Hansson is currently offline Matti HanssonFriend
Messages: 68
Registered: July 2009
Member
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: Sat Dec 20 01:40:48 GMT 2014

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

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