Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenJPA: Can the target of a ManyToOne relation be an @Embedded field?
    text
    copied!<p>I am trying to use a ManyToOne relation where the foreign key links to an @Embedded field in the target class. This compiles and enhances fine, but when running this code, OpenJPA will complain with an exception. This is with OpenJPA 2.0.1. In code this looks like so:</p> <pre><code>@Embeddable public class NormalizedNumber { private String normalizedNumber; @Basic public String getNormalizedNumber() { return normalizedNumber; } /* ... */ } @Entity @Table(name = "Phones") public class Phone { private String key; private NormalizedNumber normalizedNumber; @Id @Column(name = "IdKey", nullable = false) protected String getKey() { return key; } @Embedded public NormalizedNumber getNormalizedNumber() { return normalizedNumber; } /* ... */ } @Entity @Table(name = "UsersPhones") public class UserPhone { private String key; private Phone device; @Id @Column(name = "IdKey", nullable = false) protected String getKey() { return key; } @ManyToOne @JoinColumn(name="DeviceId", referencedColumnName="NormalizedNumber") public Phone getDevice() { return this.device; } } </code></pre> <p>The corresponding database Schema for MySQL:</p> <pre><code>create table Phones ( IdKey CHAR(32) BINARY, -- type discriminator (inheritance strategy is single table) UDType CHAR(2) BINARY, NormalizedNumber VARCHAR(100) BINARY NOT NULL, -- more columns here constraint Phones_PK primary key (IdKey) ); create table UsersPhones ( IdKey CHAR(32) BINARY, DeviceId VARCHAR(100) BINARY NOT NULL, UserKey CHAR(32) BINARY, -- more columns here constraint UsersPhones_PK primary key (IdKey) ); </code></pre> <p>What is happening here is the following. I have a <code>Phone</code> class which embeds a <code>NormalizedNumber</code> class. The <code>normalizedNumber</code> is not the primary key and not the <code>@Id</code> of the class, though. It has a different <code>key</code> field as <code>@Id</code>. </p> <p>A <code>UserPhone</code> class has a reference to a <code>Phone</code> class. This is a <code>@ManyToOne</code> relation since many users can share the same phone. The <code>UsersPhones</code>table does not use the primary key of the <code>Phones</code> table as foreign key but rather the <code>NormalizedNumber</code> column. Apparently this might not be valid in JPA, but the OpenJPA manual states that OpenJPA supports joins where the target column is not a primary key "with the same syntax as a primary key join". </p> <p>From the manual I was led to believe that this would also work with embedded fields. But I guess I must have misinterpreted that because it doesn't. This is the exception I get:</p> <p>"You cannot join on column 'Phones.normalizedNumber'. It is not managed by a mapping that supports joins."</p> <p>I have now stepped through OpenJPA with a debugger for two days to find a hint if I do something wrong or if it just isn't legal because I don't find it described explicitly in any documentation. I found a hint in the Javadoc in that OpenJPA's <code>EmbedFieldStrategy</code> does not implement <code>Joinable</code>. So maybe I am trying the impossible.</p> <p>Hence the question for the experts: Is this allowed? Or can the target of a relation not be an embedded field (except @EmbeddedId, I guess)?</p> <p>Could I override the embedded <code>NormalizedNumber</code> to define the normaizedNumber as <code>@Id</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload