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:
@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;
}
}
The corresponding database Schema for MySQL:
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) );
What is happening here is the following. I have a Phone
class which embeds a NormalizedNumber
class. The normalizedNumber
is not the primary key and not the @Id
of the class, though. It has a different key
field as @Id
.
A 开发者_运维百科UserPhone
class has a reference to a Phone
class. This is a @ManyToOne
relation since many users can share the same phone. The UsersPhones
table does not use the primary key of the Phones
table as foreign key but rather the NormalizedNumber
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".
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:
"You cannot join on column 'Phones.normalizedNumber'. It is not managed by a mapping that supports joins."
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 EmbedFieldStrategy
does not implement Joinable
. So maybe I am trying the impossible.
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)?
Could I override the embedded NormalizedNumber
to define the normaizedNumber as @Id
?
精彩评论