Galera, estou com um pepino aqui há algumas horas e agora estou pedindo socorro. Há algo que não estou percebendo e gostaria da ajuda de vocês.
Segue meus código e o problema.
@Entity
@Table(name="erp_person")
public class Person implements MyEntity{
private static final long serialVersionUID = 1L;
@EmbeddedId
private PersonId id;
@Column
private String name;
@OneToOne(fetch=FetchType.LAZY, cascade={CascadeType.MERGE, CascadeType.PERSIST})
@PrimaryKeyJoinColumn
private Document document;
public void addDocumentCompany(String _CNPJ, String _stateRegistration,
String _fantasyName, String _director, String _businessType){
document = new DocumentCompany(this, _CNPJ, _stateRegistration,
_fantasyName, _director, _businessType);
type = PersonType.company;
}
public void addDocumentIndividual(String _CPF, String _RG, GenderType _gender,
String _maritalStatus, String _nationality, Date _dateOfBirth,
String _father, String _mother){
document = new DocumentPerson(this, _CPF, _RG, _gender, _maritalStatus,
_nationality, _dateOfBirth, _father, _mother);
type = PersonType.individual;
}
...
}
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Document implements MyValueObject{
private static final long serialVersionUID = 1L;
@EmbeddedId
@AttributeOverride(name="id", column=@Column(name="idperson"))
protected PersonId idperson;
public Document(Person _person){
if (_person != null)
idperson = (PersonId) _person.getIdentity();
}
}
@Entity
@Table(name="erp_persondoccompany")
@DiscriminatorValue(value="company")
public class DocumentCompany extends Document {
private static final long serialVersionUID = 1L;
private String cnpj;
private String ie;
private String fantasyName;
private String director;
private String activity;
protected DocumentCompany() {
super(null);
}
...
}
@Entity
@Table(name="erp_persondocindividual")
public class DocumentPerson extends Document {
public enum GenderType {
male, female
}
private static final long serialVersionUID = 1L;
private String cpf;
private String rg;
private String maritalStatus;
private String nationality;
private String father;
private String mother;
@Enumerated(EnumType.STRING)
private GenderType gender;
@Temporal(TemporalType.DATE)
private Date birthday;
protected DocumentPerson() {
super(null);
}
...
}
Tudo ia bem até que eu resolvi mudar um cliente de pessoa fisica para juridica, veio o erro que não consegui resolver.
a different object with the same identifier value was already associated with the session: [br.com.mysoft.myperson.domain.person.DocumentPerson#br.com.mysoft.myperson.domain.person.PersonId@1]
...
Veja só. Tudo está funcionando, inclusive as alterações e exclusões. O que não está funcionando é somente quando eu mudo de um tipo de documento para o outro(de o1:DocumentCompany para a o2:DocumentIndividual). O que estou fazendo de errado?
Desde já, obrigado!