Use Hibernate to save two equal classes

Asked

Viewed 38 times

0

Good afternoon,

I’d like to relate the class Partida with class Time, where departure would receive the PK(Id) of the home team and the visiting team. I am doubtful how to relate in both classes, because use in the class Partida, as below:

@ManyToOne
private List<Time> times;

Seems incorrect to me.

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

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;

Already in the Departure class, you would have the relationship with the two teams, the home and the outside.

@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 restriction key.

  • In case it is not necessary to add the @Onetomany annotation in the Time class?

  • The way I showed above, the relationship already accomplished

Browser other questions tagged

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