我有一个双向的OneToOne示例不起作用:
@Embeddable
public class SolicitudGUCDetalleId implements Serializable {
@Column(name = "IDPEDIDO", insertable = false, updatable = false)
@Size(max = 36)
private String idPedido;
@Column(name = "IDLINEA", precision = 10, scale = 0, insertable = false, updatable = false)
private long idLinea;
...
}
@Entity
@Table(name = "TRIISOLIDGUCDET")
public class SolicitudGUCDetalle implements Serializable {
@EmbeddedId
private SolicitudGUCDetalleId id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "solicitudGUCDetalle")
private SolicitudGUCEnvio solicitudGUCEnvio;
...
}
@Embeddable
public class SolicitudGUCEnvioId implements Serializable {
@Column(name = "IDPEDIDO", insertable = false, updatable = false)
@Size(max = 36)
private String idPedido;
@Column(name = "IDLINEA", insertable = false, updatable = false)
private long idLinea;
...
}
@Entity
@Table(name = "TRIISOLIDGUCENV")
public class SolicitudGUCEnvio implements Serializable {
@EmbeddedId
private SolicitudGUCEnvioId id;
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name = "IDLINEA", referencedColumnName = "IDLINEA"),
@PrimaryKeyJoinColumn(name = "IDPEDIDO", referencedColumnName = "IDPEDIDO")})
private SolicitudGUCDetalle solicitudGUCDetalle;
...
}
我已经阅读了有关该问题的信息,并且有两种可行的实现这种关系的方法。
1-对复合主键使用相同的类:
@Embeddable
public class SolicitudGUCId implements Serializable {
@Column(name = "IDPEDIDO", insertable = false, updatable = false)
@Size(max = 36)
private String idPedido;
@Column(name = "IDLINEA", precision = 10, scale = 0, insertable = false, updatable = false)
private long idLinea;
...
}
@Entity
@Table(name = "TRIISOLIDGUCDET")
public class SolicitudGUCDetalle implements Serializable {
@EmbeddedId
private SolicitudGUCId id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "solicitudGUCDetalle")
private SolicitudGUCEnvio solicitudGUCEnvio;
...
}
@Entity
@Table(name = "TRIISOLIDGUCENV")
public class SolicitudGUCEnvio implements Serializable {
@EmbeddedId
private SolicitudGUCId id;
@OneToOne(fetch = FetchType.LAZY)
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name = "IDLINEA", referencedColumnName = "IDLINEA"),
@PrimaryKeyJoinColumn(name = "IDPEDIDO", referencedColumnName = "IDPEDIDO")})
private SolicitudGUCDetalle solicitudGUCDetalle;
...
}
2-切换到@OneToMany关系并将相关实体存储在Set中:
@Embeddable
public class SolicitudGUCDetalleId implements Serializable {
@Column(name = "IDPEDIDO", insertable = false, updatable = false)
@Size(max = 36)
private String idPedido;
@Column(name = "IDLINEA", precision = 10, scale = 0, insertable = false, updatable = false)
private long idLinea;
...
}
@Entity
@Table(name = "TRIISOLIDGUCDET")
public class SolicitudGUCDetalle implements Serializable {
@EmbeddedId
private SolicitudGUCDetalleId id;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "solicitudGUCDetalle")
private Set<SolicitudGUCEnvio> solicitudes;
...
}
@Embeddable
public class SolicitudGUCEnvioId implements Serializable {
@Column(name = "IDPEDIDO", insertable = false, updatable = false)
@Size(max = 36)
private String idPedido;
@Column(name = "IDLINEA", insertable = false, updatable = false)
private long idLinea;
...
}
@Entity
@Table(name = "TRIISOLIDGUCENV")
public class SolicitudGUCEnvio implements Serializable {
@EmbeddedId
private SolicitudGUCEnvioId id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "IDLINEA", referencedColumnName = "IDLINEA",
nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "IDPEDIDO", referencedColumnName = "IDPEDIDO",
nullable = false, insertable = false, updatable = false)})
private SolicitudGUCDetalle solicitudGUCDetalle;
...
}
我想知道我是否可以按照第一个示例实现这种关系。如果答案是否定的,我想知道为什么。
答案 0 :(得分:1)
这似乎是“派生身份”;因此您可以尝试对选项1进行一些细微改动:
@Embeddable
public class SolicitudGUCId implements Serializable {
@Column(name = "IDPEDIDO", insertable = false, updatable = false)
@Size(max = 36)
private String idPedido;
@Column(name = "IDLINEA", precision = 10, scale = 0, insertable = false, updatable = false)
private long idLinea;
...
}
@Entity
@Table(name = "TRIISOLIDGUCDET")
public class SolicitudGUCDetalle implements Serializable {
@EmbeddedId
private SolicitudGUCId id;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "solicitudGUCDetalle")
private SolicitudGUCEnvio solicitudGUCEnvio;
...
}
@Entity
@Table(name = "TRIISOLIDGUCENV")
public class SolicitudGUCEnvio implements Serializable {
@EmbeddedId
private SolicitudGUCId id;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumns({
@JoinColumn(name = "IDLINEA", referencedColumnName = "IDLINEA"),
@JoinColumn(name = "IDPEDIDO", referencedColumnName = "IDPEDIDO")})
@MapsId
private SolicitudGUCDetalle solicitudGUCDetalle;
...
}
请注意,SolicitudGUCEnvio.solicitudGUCDetalle
用@MapsId
和@JoinColumns
注释(而不是@PrimaryKeyJoinColumns
)。
在JPA 2.2 spec的第2.4.1节中讨论了派生的身份(带有示例)。
答案 1 :(得分:0)
这是我正在寻找的解决方案:
{field1 => true, field2 => false, field3 => true, field4 => false}
@Brian Vosburgh,谢谢您的帮助