JPA - Two OneToOne and OneToMany relationships between the same class

1

I have a Student class and it has two relationships with Pessoa a OneToOne and OneToMany and person in turn a relationship with Endereco , as shown below:

Student Class

@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

}

Person Class

@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 also when he / she is registered he / she receives the information of who made this register ( OneToMany ). The problem is at the time that JPA generates SQL because it is using the OneToMany relationship to bind to the address table instead of using the OneToOne relationship.

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;
    
asked by anonymous 29.04.2015 / 13:27

1 answer

1

As it stands, its @OneToOne between Estudante and PessoaFisica does not make much sense, because not necessarily a person is a student, and a cadaster should not be a student either.

I would do Estudante inherit from PessoaFisica using 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 that. It is also possible to separate the subscriber into a class also derived from PessoaFisica .

    
04.05.2015 / 17:44