1
I own a student class and she has two relationships with Pessoa one OneToOne and OneToMany and person in turn a relationship with Endereco, as shown below:
Classe Estudante
@Entity
@Table(name = "ESTUDANTE")
public class Estudante implements Serializable {
private static final long serialVersionUID = 1335678886512650803L;
@Id
@Column(name = "ID_PESSOA_FISICA")
private Long id;
@OneToOne(optional = false)
@JoinColumn(name = "ID_PESSOA_FISICA", referencedColumnName = "ID_PESSOA_FISICA")
private PessoaFisica pessoaFisica;
@ManyToOne(optional = false)
@JoinColumn(name = "ID_PESSOA_CADASTRO", referencedColumnName = "ID_PESSOA_FISICA")
private PessoaFisica cadastrante;
//Get & Set
}
Classe Pessoa
@Entity
@Table(name = "PESSOA_FISICA")
public class PessoaFisica implements Serializable {
private static final long serialVersionUID = -6877588433120679748L;
@Id
@Column(name = "ID_PESSOA_FISICA")
private long codigo;
@OneToOne(cascade = CascadeType.ALL, mappedBy = "pessoaFisica")
private Estudante estudante;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "cadastrante")
private List<Estudante> listaEstudanteCadastrados;
//Get & Set
A student is a person (Onetoone), and he also to be registered receives information of who made this registration (OneToMany). The problem is at the time that the JPA generates the SQL, because he’s using the relationship OneToMany to link to the address table instead of using the OneToOne.
select
this_.ID_PESSOA_FISICA as ID1_7_7_,
pessoafis3_.ID_PESSOA_FISICA as PFIS1_8_0_,
endereco5_.ID_ENDERECO as EPES2_9_,
pessoafis10_.ID_PESSOA_FISICA as PFIS1_8_6_,
from
ESTUDANTE this_,
PESSOAS_FISICAS pessoafis3_,
ENDERECO endereco5_,
PESSOAS_FISICAS pessoafis10_
where
this_.ID_PESSOA_CADASTRO=pessoafis3_.ID_PESSOA_FISICA(+)
and epessoafis3_.ID_PESSOA_FISICA=endereco5_.ID_PESSOA_FISICA(+)
and this_.ID_PESSOA_FISICA=pessoafis10_.ID_PESSOA_FISICA(+)
order by
this_.ID_PESSOA_FISICA desc;
I checked this error is really does not make sense, I removed this relationship the most and found the solution forcing the @Manytoone(fetch = Fetchtype.LAZY) relationship so SQL was created correctly. Thank you for your attention!
– wesley modanez