Use Hibernate to record two equal classes

0

Good afternoon,

I would like to relate the Partida class to the Time class, where I would get the PK (Id) of the home team and the visiting team. I'm wondering how to relate in both classes, since use in class Partida , as below:

@ManyToOne
private List<Time> times;

It seems wrong to me.

How could I relate both classes and generate the tables in the database?

    
asked by anonymous 19.11.2018 / 21:40

1 answer

0

You could transform the mapping of your classes as follows:

@Entity
@Table(name="time")
public class Time  {
    @Id
    @Column(name = "time_id")
    private Integer id;

    @Column(name = "time_nome")
    private String nome;

In the Match class, you would have a relationship with both teams, the home team and the outside team.

@Entity
@Table(name="partida")
public class Partida 
{
    @Id
    @Column(name = "partida_id")
    private Integer id;

    @ManyToOne
    @JoinColumn(name = "time_casa_fk")
    private Time timeCasa;

    @ManyToOne
    @JoinColumn(name = "time_fora_fk")
    private Time timeFora;

To complement, so that the 2 teams are not equal in the same match, you can put a restraint key.

    
20.11.2018 / 00:09