Ajuda com DELETE_ORPHAN no HIBERNATE

3 respostas
R

Olá Pessoal,

Possuo as seguintes entidades,
Funcionario.java

@Entity
@Table(name = "TB_FUNCIONARIO")
public class Funcionario extends PessoaFisica {

	/** serialversionUID **/
	private static final long serialVersionUID = 1350744945734500949L;

	/** lista de idiomas do funcionário **/	
	@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinTable(name = "TB_FUNCIONARIO_IDIOMA", joinColumns = @JoinColumn(name = "ID_PESSOA", referencedColumnName = "ID_PESSOA"), inverseJoinColumns = @JoinColumn(name = "ID_IDIOMA", referencedColumnName = "ID_IDIOMA"), uniqueConstraints = @UniqueConstraint(columnNames = {
			"ID_PESSOA", "ID_IDIOMA" }))
	@Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
	private List<Idioma> idiomas;

Idioma.java

@Entity
@Table(name = "TB_IDIOMA")
@TableGenerator(name = "idGenerator", table = "TB_SEQUENCE", allocationSize = 1, pkColumnName = "SEQ_NAME", valueColumnName = "SEQ_COUNT", pkColumnValue = "SEQ_IDIOMA")
public class Idioma extends EntityObject {

	/** serialversionUID **/
	private static final long serialVersionUID = -4488811991972462034L;

	/** identificador **/
	@Id
	@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenerator")
	@Column(name = "ID_IDIOMA", length = 10, precision = 0, insertable = true, updatable = false)
	private Long id;

	/** descrição do idioma **/
	@Column(name = "DS_IDIOMA", length = 120, precision = 0, insertable = true, updatable = true)
	private String idioma;

O sistema está deletando normalmente os registros orfãos da tabela idiomas, no entanto é mostrado o seguinte erro no console:

47617 [http-8080-3] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 547, SQLState: 23000
47617 [http-8080-3] ERROR org.hibernate.util.JDBCExceptionReporter - The DELETE statement conflicted with the REFERENCE constraint “FKD395714A80D93B48”. The conflict occurred in database “xxxxxx”, table “dbo.TB_FUNCIONARIO_IDIOMA”, column ‘ID_IDIOMA’.
47618 [http-8080-3] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.exception.ConstraintViolationException: could not delete: [br.com.mccann.activecollab.domain.entity.pessoa.Idioma#38]

Caused by: java.sql.SQLException: The DELETE statement conflicted with the REFERENCE constraint “FKD395714A80D93B48”. The conflict occurred in database “xxxxxxx”, table “dbo.TB_FUNCIONARIO_IDIOMA”, column ‘ID_IDIOMA’.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:365)

Desde já agradeço pela ajuda.

3 Respostas

A

cara,

ele ta apagando o funcionario e os idiomas relacionados a esse funcionario?
vc nao tem mais relacionamento aonde o depende de funcionario ou idioma?

t+

R

Vamos ao seguinte cenário

O funcionario 1 possui os seguintes idiomas

Português
Inglês

O funcionário 2 possui os seguintes idiomas

Português
Inglês

Eu tenho duas tabelas

TB_FUNCIONARIO_IDOMA

E

TB_IDIOMAS

Se eu remover o idioma português dos dois funcionários, o idioma português vai ficar orfão na tabela TB_IDIOMA e consequentemente ele será apagado, devido a anotação DELETE_ORPHAN.

Tá funcionando beleza isso, mas o problema é o seguinte:

Quando eu removo o idioma português do funcionario 1, parece que o hibernate tenta remover o idioma português da tabela TB_IDIOMA, como ele está vinculado ao registro português do funcionário 2 a exception citada no tópico é lançada.

A

cara,
vc ta pensando errado.

vc vai ter que fazer o seguinte,
qdo vc remover 1 idioma do funcionario, ele nao deve apagar na tabela idioma.
qdo vc excluir 1 registro da tabela idioma, ai sim que vc vai ter DELETE_ORPHAN, pois ele tem q ir na tabela Funcionario_idioma, e remover todos que se relaciona com aquele idioma removido.

entendeu?

t+

Criado 24 de outubro de 2011
Ultima resposta 26 de out. de 2011
Respostas 3
Participantes 2