【发布时间】:2021-02-13 20:59:20
【问题描述】:
我有以下非常简单的设计:
一个俱乐部有多个团队,每个团队都有一个团队类型代码(性别 - 年龄组代码)加上一个序数(团队)编号,例如20 岁以上的男性将是“MO20”,序号代表该年龄组的第一、第二等团队。这是Teams 的PK。你明白了。
俱乐部映射:
@Entity
@Table(name = "Clubs")
public class Club implements Serializable
{
private static final long serialVersionUID = 1L;
@Id
@Column
private Integer id;
@Basic(optional = false)
@Column
private String name;
@Basic(optional = false)
@Column
private String code;
@OneToMany(mappedBy = "club")
private List<Team> teams;
...
}
团队映射:
@Entity
@Table(name = "Teams")
public class Team implements Serializable
{
private static final long serialVersionUID = 1L;
@EmbeddedId
private TeamPk embeddedId;
@MapsId("clubId")
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "club_id", insertable = false, updatable = false)
private Club club;
...
}
@Embeddable
public class TeamPk implements Serializable
{
private static final long serialVersionUID = 1L;
@Column(name = "club_id")
private Integer clubId;
@Column(name = "team_type_code")
private String teamTypeCode;
@Column(name = "ordinal_nbr")
private Integer ordinalNbr;
...
}
例外:
Caused by: Exception [EclipseLink-28019] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Deployment of PersistenceUnit [BBStatsPU] failed. Close all factories for this PersistenceUnit.
Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
---------------------------------------------------------
Exception [EclipseLink-46] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There should be one non-read-only mapping defined for the primary key field [Teams.club_id].
Descriptor: RelationalDescriptor(net.bbstatstest.i265.entity.Team --> [DatabaseTable(Teams)])
Runtime Exceptions:
---------------------------------------------------------
at org.eclipse.persistence.exceptions.EntityManagerSetupException.deployFailed(EntityManagerSetupException.java:241)
... 182 more
Caused by: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.IntegrityException
Descriptor Exceptions:
---------------------------------------------------------
Exception [EclipseLink-46] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: There should be one non-read-only mapping defined for the primary key field [Teams.club_id].
Descriptor: RelationalDescriptor(net.bbstatstest.i265.entity.Team --> [DatabaseTable(Teams)])
Runtime Exceptions:
---------------------------------------------------------
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:762)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:698)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.initializeDescriptors(DatabaseSessionImpl.java:629)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.postConnectDatasource(DatabaseSessionImpl.java:868)
at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.loginAndDetectDatasource(DatabaseSessionImpl.java:811)
at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:256)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:772)
... 180 more
EclipseLink 抱怨 Teams.club_id 的列映射:
There should be one non-read-only mapping defined for the primary key field [Teams.club_id].
但是为什么呢?
嵌入的 ID 字段或更确切地说是 ID 类在 clubId 字段上具有 @Column 而没有 insertable = true, updatable = true,因此它默认为可写,至少这是文档所说的:https://javaee.github.io/javaee-spec/javadocs/javax/persistence/Column.html#insertable--
因此,所有其他 club_id 映射必须为 insertable = true, updatable = true 才能为只读,例如 Team.club 关系。
问题:
这里有什么问题?
请注意,当在 Team.club 和 TeamPk.clubId 之间切换 insertable = ..., updatable = ... 时,整个事情都会起作用。
为什么 EclipseLink 在这里阻塞?对我来说似乎是一个错误。
PS:我使用的是 EclipseLink 2.7.7。 (hibernate标签只是为了吸引更多关注)
【问题讨论】:
标签: hibernate jpa eclipselink jpa-2.0