JPA - Two Onetoone and Onetomany relationships between the same class

Asked

Viewed 1,310 times

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;

1 answer

1


The way you are, your @OneToOne amid Estudante and PessoaFisica doesn’t make much sense, because not necessarily a person is a student, and a registrant shouldn’t be a student either.

I would do Estudante inherit from PessoaFisica using the Annotation Inheritance:

@Entity
@Inheritance(strategy=SINGLE_TABLE, discriminatorValue="P")
@Table(name = "PESSOA_FISICA")
public class PessoaFisica implements Serializable 
{
    private static final long serialVersionUID = -6877588433120679748L;

    @Id
    @Column(name = "ID_PESSOA_FISICA")
    private long codigo;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cadastrante")
    private List<Estudante> listaEstudanteCadastrados;    

  //Get & Set
}

@Entity
@Inheritance(discriminatorValue="E")
public class Estudante extends PessoaFisica 
{
    @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

}

I did not test this code, but it would be more or less so. It is still possible to separate the cadastrant into a class also derived from PessoaFisica.

  • 1

    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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.