Java 如何在hibernate中删除具有复合主键的集合的元素?
我拥有以下实体:Java 如何在hibernate中删除具有复合主键的集合的元素?,java,hibernate,jpa,jboss6.x,Java,Hibernate,Jpa,Jboss6.x,我拥有以下实体: @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
@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);
}
}
}
如果我试图删除通知实体:
我得到一个错误:
deleted entity passed to persist Noticia_i18n#<null>
update出错,因为Hibernate试图用Noticia_id的空值更新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>
noticia.getNoticia_i18n().remove (traduccionNoticia);