我有以下实体:
@Embeddable
public class Noticia_i18nPK implements Serializable {
private static final long serialVersionUID = 4556976991711237055L;
private Noticia noticia;
private String codigoIdioma;
public Noticia_i18nPK() {}
public Noticia_i18nPK (Noticia noticia, String codigoIdioma) {
this.noticia = noticia;
this.codigoIdioma = codigoIdioma;
}
@ManyToOne
@JoinColumn(name="noticia_id")
@NotNull
public Noticia getNoticia() {
return noticia;
}
public void setNoticia (Noticia noticia) {
this.noticia = noticia;
}
@Column(name = "codigoidioma")
@NotNull
public String getCodigoIdioma() {
return codigoIdioma;
}
public void setCodigoIdioma (String codigoIdioma) {
this.codigoIdioma = codigoIdioma;
}
@Override
public int hashCode() {
int result = 31;
result += (codigoIdioma == null) ? 0 : codigoIdioma.hashCode();
result += (noticia == null) ? 0 : noticia.getId().intValue();
return result;
}
@Override
public boolean equals (Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass())
return false;
Noticia_i18nPK other = (Noticia_i18nPK) obj;
if (codigoIdioma == null) {
if (other.getCodigoIdioma() != null)
return false;
} else if (!codigoIdioma.equals (other.getCodigoIdioma()))
return false;
if (noticia == null) {
if (other.getNoticia() != null)
return false;
} else if (noticia.getId().compareTo (other.getNoticia().getId()) != 0)
return false;
return true;
}
}
国际化POJO:
@Entity
@Table(name = "Noticia_i18n")
public class Noticia_i18n implements Serializable {
private static final long serialVersionUID = 9093075808608249854L;
private Noticia_i18nPK noticia_i18nPK;
private String descripcion;
@EmbeddedId
public Noticia_i18nPK getNoticia_i18nPK() {
return noticia_i18nPK;
}
public void setNoticia_i18nPK (Noticia_i18nPK noticia_i18nPK) {
this.noticia_i18nPK = noticia_i18nPK;
}
@Column(name="descripcion")
@Length(max=5000)
@NotNull
public String getDescripcion() {
return descripcion;
}
public void setDescripcion (String descripcion) {
this.descripcion = descripcion;
}
@Override
public int hashCode() {
return noticia_i18nPK.hashCode();
}
@Override
public boolean equals (Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (getClass() != obj.getClass())
return false;
Noticia_i18n other = (Noticia_i18n) obj;
if (noticia_i18nPK == null) {
if (other.getNoticia_i18nPK() != null)
return false;
} else if (!noticia_i18nPK.equals (other.getNoticia_i18nPK()))
return false;
return true;
}
}
和 POJO:
@Entity
@Table(name="noticia")
@SequenceGenerator(
name = "noticia_id_seq",
sequenceName = "ef.noticia_id_seq",
allocationSize=1)
public class Noticia extends AuctionsBaseEntity {
private static final long serialVersionUID = -5402495697859251461L;
private Long id;
private Date fecha;
private Set<Noticia_i18n> noticia_i18n;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "noticia_id_seq")
@Column(name = "id")
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getFecha() {
return fecha;
}
public void setFecha(Date fecha) {
this.fecha = fecha;
}
@JoinColumn(name="noticia_id")
@OneToMany(cascade={CascadeType.ALL})
public Set<Noticia_i18n> getNoticia_i18n() {
return noticia_i18n;
}
public Noticia_i18n getNoticia_i18n (String codigoIdioma) {
getNoticia_i18n();
if (!VacioHelper.esVacio (this.noticia_i18n)) {
for (Noticia_i18n i18nActual : this.noticia_i18n) {
if ((i18nActual.getNoticia_i18nPK() != null) &&
(i18nActual.getNoticia_i18nPK().getCodigoIdioma().compareTo (codigoIdioma) == 0))
return i18nActual;
}
}
return null;
}
public void setNoticia_i18n (Set<Noticia_i18n> noticia_i18n) {
this.noticia_i18n = noticia_i18n;
}
public void addNoticia_i18n (Noticia_i18n noticia_i18n) {
if (noticia_i18n != null) {
if (this.noticia_i18n == null)
this.noticia_i18n = new HashSet<Noticia_i18n>();
noticia_i18n.getNoticia_i18nPK().setNoticia (this);
this.noticia_i18n.add (noticia_i18n);
}
}
}
问题是,如何删除 noticia_i18n 元素?例如,如果我得到一个包含三个noticia_i18n元素的通知,如下所示:
EntityManager em;
....
noticia = DaoFactory.getDefault().getNoticiaDao().findById (em, noticiaActual.getIdNoticia());
...
如果我尝试删除一个noticia_i18n实体:
Noticia_i18n traduccionNoticia = noticia.getNoticia_i18n (noticiaActual.getCodigoIdioma());
DaoFactory.getDefault().getNoticia_i18nDao().remove (em, traduccionNoticia);
我得到错误:
deleted entity passed to persist Noticia_i18n#<null>
但是如果我添加代码(在remove之前):
noticia.getNoticia_i18n().remove (traduccionNoticia);
错误在于更新,因为 Hibernate 尝试使用空值 noticia_id 更新 Noticia_i18n。
问题是:如何删除这个集合的元素?类的映射需要进行哪些更改?