Como bloquear update numa lista ManyToOne ?

18/08/2010

0

Olá pessoal,

To com um problema no CRUD de pessoas num sistema q estou trabalhando utilizando o SEAM + HIBERNATE.
Mais precisamente, o problema esta no Update.

Tenho meu CRUD q está funcionando corretamente. Cadastro uma pessoa com seus respectivos endereços, pesquiso e excluo a pessoa com todos endereços.
Os estados e cidades já estão cadastrados no BD e utilizo essas tabelas somente para pesquisar.

So que quando eu vou editar um endereço e altero o ESTADO ou CIDADE ele faz um UPDATE também no meu ESTADO ou CIDADE.

Ex: tenho um endereço cadastrado no estado de MG, ao alterar este endereço para o estado de SP. Eu fico com dois SP no meu BD. Ou seja, fez um UPDATE no meu estado passando de MG pra SP.

Se alguem tiver alguma dica???

Já tentei usar o "cascade = CascadeType.REFRESH" e "updatable = false" mas não resolveu.

Segue-se o meu ORM de PESSOA, ENDERECO e CIDADE:


@MappedSuperclass
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="DTYPE", discriminatorType=DiscriminatorType.STRING, length = 1)
public abstract class Pessoa extends BaseEntity {

@Transient
private static final long serialVersionUID = -148850091927978160L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID_PESSOA", unique = true, nullable = false, length =
private Long id;

@ForeignKey(name = "FK_PESS_CATE")
@ManyToOne(targetEntity = CategoriaPessoaCBEntity.class, fetch = FetchType.LAZY)
@JoinColumn(name = "ID_CATEPESS")
private CategoriaPessoaCBEntity categoriaPessoa;

@Length(max = 100)
@Column(name = "NOME", length = 100)
private String nome;

@Valid
@OneToMany(targetEntity = EnderecoCBEntity.class, fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "pessoa")
@ForeignKey(name = "FK_ENDE_PESS")
@JoinColumn(name = "ID_PESSOA")
private List<EnderecoCBEntity> enderecos;

... get´s and set´s ...

}





@Entity
@Table(name = Constantes.TABELA.TBCB_ENDERECO, catalog = "HABILIS")
public class EnderecoCBEntity implements Serializable {

@Transient
private static final long serialVersionUID = -8092794266915113964L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID_ENDERECO", unique = true, nullable = false, length =
private Long id;

@ForeignKey(name = "FK_ENDE_PESS")
@JoinColumn(name = "ID_PESSOA", nullable = false)
@ManyToOne(targetEntity = PessoaEntity.class, fetch = FetchType.LAZY)
private PessoaEntity pessoa;

@ForeignKey(name = "FK_ENDE_CIDA")
@JoinColumn(name = "ID_CIDADE", nullable = false, updatable = false)
@ManyToOne(targetEntity = CidadeCBEntity.class, fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
private CidadeCBEntity cidade;

@NotNull
@Length(max = 25)
@Column(name = "TIPO_ENDE", nullable = false, length = 25)
private String tipoEndereco;

@NotNull
@Length(max = 50)
@Column(name = "LOGRADOURO", length = 50)
private String logradouro;

@Length(max = 25)
@Column(name = "NUMERO", length = 25)
private String numero;

@Length(max = 50)
@Column(name = "BAIRRO", length = 50)
private String bairro;

@Length(max = 50)
@Column(name = "COMPLEMENTO", length = 50)
private String complemento;

@Column(name = "CEP")
private Integer cep;

@Length(max = 25)
@Column(name = "TELEFONE", length = 25)
private String telefone;

@Length(max = 25)
@Column(name = "FAX", length = 25)
private String fax;

@Version
@Column(name = "AD_VERSAO", nullable = false, length =
private Integer versao;

... get´s and set´s ...

}



@Entity
@Table(name = Constantes.TABELA.TBCB_CIDADE, catalog = "HABILIS")
public class CidadeCBEntity extends BaseEntity {

@Transient
private static final long serialVersionUID = -393336784029249201L;

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "ID_CIDADE", unique = true, nullable = false, length =
private Long id;

@NotNull
@ForeignKey(name = "FK_CIDA_PAIS")
@JoinColumn(name = "ID_PAIS", nullable = false)
@ManyToOne(targetEntity = PaisTXEntity.class, fetch = FetchType.LAZY)
private PaisTXEntity pais;

@NotNull
@ForeignKey(name = "FK_CIDA_ESTA")
@JoinColumn(name = "ID_ESTADO", nullable = false)
@ManyToOne(targetEntity = EstadoCBEntity.class, fetch = FetchType.LAZY)
private EstadoCBEntity estado;

@NotNull
@Length(max = 50)
@Column(name = "DS_CIDADE", nullable = false, length = 50)
private String descricao;

... get´s and set´s ...

}
Marcos Andrade

Marcos Andrade

Responder

Posts

19/08/2010

Dyego Carmo

Retire a clausula "cascade = CascadeType.ALL" do relacionamento que deseja tornar "read only".

Responder

19/08/2010

Marcos Andrade

Ao retirar a clausula "cascade = CascadeType.ALL" conforme ORM abaixo o endereço da pessoa não é mais persistido. Já tentei com os demais tipos de CascadeType e não resolveu.

Alguma outra sugestão?



Obrigado!


@Valid
@OneToMany(targetEntity = EnderecoCBEntity.class, fetch = FetchType.LAZY, mappedBy = "pessoa")
@ForeignKey(name = "FK_ENDE_PESS")
@JoinColumn(name = "ID_PESSOA")
private List<EnderecoCBEntity> enderecos;

Responder

21/08/2010

Dyego Carmo

troque de Cascade.ALL para Cascade.PERSIST

Pronto , ele vai persistir :)
mas nao alterar !

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar