在我正在工作的项目中,我们有一个带有多个实体的持久性单元的ejb但最近我们必须更新一些具有复杂id的实体(形成该id的2个字段)并且通过此更改查询不会以我们期望的方式行事。 我们有一个这样的实体,它对应于一个中间表,它与多个实体中的一个实体发生了多对一的关系。
publicic class BapeConvCateZonas implements Serializable {
private static final long serialVersionUID = 1L;
@Size(max = 1)
@Column(name = "B_INTERINIDAD")
private String bInterinidad;
@Size(max = 1)
@Column(name = "B_SUSTIT_LARGA")
private String bSustitLarga;
@Size(max = 1)
@Column(name = "B_SUSTIT_CORTA")
private String bSustitCorta;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "C_CODIGO")
private Long cCodigo;
@JoinColumns({
@JoinColumn(name = "C_COD_GSA", referencedColumnName = "C_COD_GSA", updatable=false,insertable=false),
@JoinColumn(name = "C_COD_CENTRO", referencedColumnName = "C_CODIGO", updatable=false,insertable=false)})
@ManyToOne
private BapeCentrossanitarios bapeCentrossanitarios;
@JoinColumns({
@JoinColumn(name = "C_COD_CONV", referencedColumnName = "C_COD_CONV"),
@JoinColumn(name = "C_COD_CATE", referencedColumnName = "C_COD_CATE")})
@ManyToOne(optional = false)
private BapeConvCateTopes bapeConvCateTopes;
@JoinColumns({
@JoinColumn(name = "C_COD_GSA", referencedColumnName = "C_COD_GSA", updatable=false,insertable=false),
@JoinColumn(name = "C_COD_CENTRO", referencedColumnName = "C_COD_CENTRO", updatable=false,insertable=false),
@JoinColumn(name = "C_COD_DEPTO", referencedColumnName = "C_CODIGO", updatable=false,insertable=false)})
@ManyToOne
private BapeDeptSanitarios bapeDeptSanitarios;
@JoinColumn(name = "C_COD_GSA", referencedColumnName = "C_CODIGO", updatable=false,insertable=false)
@ManyToOne
private BapeGerenciasaludarea cCodGsa;
实体改变了:
public class BapeCentrossanitarios implements Serializable {
@OneToMany(mappedBy = "bapeCentrossanitarios")
private List<BapeConvCateZonas> bapeConvCateZonasList;
private static final long serialVersionUID = 1L;
@EmbeddedId
protected BapeCentrossanitariosPK bapeCentrossanitariosPK;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 150)
@Column(name = "A_DESCRIPCION")
private String aDescripcion;
@JoinColumn(name = "C_COD_GSA", referencedColumnName = "C_CODIGO", insertable = false, updatable = false)
@ManyToOne(optional = false)
private BapeGerenciasaludarea bapeGerenciasaludarea;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "bapeCentrossanitarios")
private List<BapeDeptSanitarios> bapeDeptSanitariosList;
此实体中的ID已更改为:
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "C_CODIGO")
private String cCodigo;
致:
@EmbeddedId
protected BapeCentrossanitariosPK bapeCentrossanitariosPK;
并且:
@Embeddable
公共类BapeCentrossanitariosPK实现了Serializable {
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "C_CODIGO")
private String cCodigo;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 4)
@Column(name = "C_COD_GSA")
private String cCodGsa;
当我执行此查询时,不会返回任何内容:
SQL: select u from BapeConvCateZonas u where u.bapeConvCateTopes.bapeConvCateTopesPK.cCodConv=:convocatoria and u.bapeConvCateTopes.bapeConvCateTopesPK.cCodCate=:categoria and u.bapeCentrossanitarios is null and u.bInterinidad='S' order by u.cCodGsa.cCodigo ASC;
BapeConvCateTopes实体: 公共类BapeConvCateTopes实现Serializable {
@Column(name = "N_ANIO_FINALIZACION")
private Short nAnioFinalizacion;
@JoinTable(name = "BAPE_CONV_CATE_MERI_SUB", joinColumns = {
@JoinColumn(name = "C_COD_CONV", referencedColumnName = "C_COD_CONV"),
@JoinColumn(name = "C_COD_CATE", referencedColumnName = "C_COD_CATE")}, inverseJoinColumns = {
@JoinColumn(name = "C_COD_AREA", referencedColumnName = "C_COD_AREA"),
@JoinColumn(name = "C_COD_TIPO", referencedColumnName = "C_COD_TIPO"),
@JoinColumn(name = "C_COD_SUBTIPO", referencedColumnName = "C_CODIGO")})
@ManyToMany
@OrderBy("bapeMeritosSubtiposPK.cCodArea ASC")
private List<BapeMeritosSubtipos> bapeMeritosSubtiposList;
@JoinTable(name = "BAPE_CONV_CATE_TITU_EXPE", joinColumns = {
@JoinColumn(name = "C_COD_CONV", referencedColumnName = "C_COD_CONV"),
@JoinColumn(name = "C_COD_CATE", referencedColumnName = "C_COD_CATE")}, inverseJoinColumns = {
@JoinColumn(name = "C_COD_TITU", referencedColumnName = "C_CODIGO")})
@ManyToMany
private List<BapeTitulaExper> bapeTitulaExperCollection;
@JoinColumn(name = "C_COD_CONV", referencedColumnName = "C_CODIGO", insertable = false, updatable = false)
@ManyToOne(optional = false)
private BapeConvocatorias bapeConvocatorias;
@JoinColumn(name = "C_COD_CATE", referencedColumnName = "C_CODIGO", insertable = false, updatable = false)
@ManyToOne(optional = false)
private BapeCategorias bapeCategorias;
private static final long serialVersionUID = 1L;
@EmbeddedId
protected BapeConvCateTopesPK bapeConvCateTopesPK;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 1)
@Column(name = "B_ACTIVO")
private String bActivo;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 1)
@Column(name = "B_NOTIF_AUTOMATICA")
private String bNotifAutomatica;
// @Max(value=?) @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
@Column(name = "TOPE")
private BigDecimal tope;
@OneToMany(cascade = CascadeType.REFRESH, mappedBy = "bapeConvCateTopes")
@OrderColumn(name="C_COD_GSA")
private List<BapeConvCateZonas> bapeConvCateZonasList;
和BapeConvCateTopesPK:
@Embeddable
public class BapeConvCateTopesPK implements Serializable {
@Basic(optional = false)
@NotNull
@Column(name = "C_COD_CONV")
private long cCodConv;
@Basic(optional = false)
@NotNull
@Column(name = "C_COD_CATE")
private long cCodCate;
该表具有符合条件的值。 根据最后的改变,它必须是与关系和复杂身份相关的东西,但我不会忘记把握可能的东西。编辑:添加了有关ID更改的更多信息。
答案 0 :(得分:0)
虽然您的ID更改会混淆问题,但正在使用的JPQL不正确。具体来说,“u.bapeCentrossanitarios为null”正在强制对表进行内连接。由于内部联接过滤掉空值,因此该子句永远不会为真,因此不会返回任何结果。
查询应该在此关系上使用左外连接。类似的东西:
"select u from BapeConvCateZonas u left outer join u.bapeCentrossanitarios bapeCentrossanitarios where u.bapeConvCateTopes.bapeConvCateTopesPK.cCodConv=:convocatoria and u.bapeConvCateTopes.bapeConvCateTopesPK.cCodCate=:categoria and bapeCentrossanitarios is null and u.bInterinidad='S' order by u.cCodGsa.cCodigo ASC"
可行,但可能有很多方法可以形成类似的查询。
它可能首先工作的原因是你对复合PK的更改可能会绕过EclipseLink优化,允许它在不执行连接的情况下评估'u.bapeCentrossanitarios is Null' - 它可以只检查pk值它是空的。通过更改,一个或多个外键可能为空,也可能不为空(它们不受映射控制),因此必须连接表才能确定。