How to represent an N:N relationship in java?

Asked

Viewed 1,041 times

1

I think it’s a very simple question, but I couldn’t find any example that would make that clear. I already made my database model in Postgresql and when it came to creating the classes I was a little lost. I have the entities List (stores the user’s favorite movies) and Movie (which stores the film’s data) and there is a relationship N:N between these entities, that is, several films can be associated with various lists and vice versa. I created a Listxmovie table that will have 2 foreign keys (one to identify the film another to identify the list) and the primary key of this table will be composed of 2 foreign keys. I think I’ve done quite well... My question is how do I create classes that represent this relationship.

The scripts of the tables I mentioned:

CREATE TABLE app.tb_lista_favoritos(
id SERIAL NOT NULL,
nome VARCHAR(45) NOT NULL,
data_criacao DATE NOT NULL,
CONSTRAINT pk_lista_favoritos PRIMARY KEY (id)
);

CREATE TABLE app.tb_filme(
cod SERIAL NOT NULL,
nome VARCHAR(45) NOT NULL,
data_lancamento DATE NOT NULL,
genero VARCHAR(45) NOT NULL,
duracao TIME NOT NULL,
avaliacao REAL NOT NULL,
CONSTRAINT pk_filme PRIMARY KEY (cod)
);

CREATE TABLE app.tb_listaXfilme(
id_lista INT NOT NULL,
cod_filme INT NOT NULL,
CONSTRAINT pk_listaXfilme PRIMARY KEY (id_lista,cod_filme),
CONSTRAINT fk1_listaXfilme FOREIGN KEY (id_lista) REFERENCES app.tb_lista_favoritos (id),
CONSTRAINT fk2_listaXfilme FOREIGN KEY (cod_filme) REFERENCES app.tb_filme (cod)
);

Classes:

Playlist.java:

public class ListaFavoritos {
    int codLista;
    String nomeLista;
    Date dataCriacao;

    public ListaFavoritos(int codLista, String nomeLista, Date dataCriacao) {
        this.codLista = codLista;
        this.nomeLista = nomeLista;
        this.dataCriacao = dataCriacao;
    }

    public ListaFavoritos() {

    }

    /*Getters e Setters*/
}

Java movie.:

public class Filme {
    int codFilme;
    double avaliacao;
    Date anoLancamento,duracao;
    String nome, genero;

    public Filme(int codFilme, double avaliacao, Date anoLancamento, Date duracao, String nome, String genero) {
        this.codFilme = codFilme;
        this.avaliacao = avaliacao;
        this.anoLancamento = anoLancamento;
        this.duracao = duracao;
        this.nome = nome;
        this.genero = genero;
    }

    public Filme() {
    }

    /*Getters e setters*/

}
  • You are using some Persistence layer (Hibernate) or JDBC?

  • No... can you explain to me what this is about and how it would help me? I have no experience with it

  • If you said you set up the structures, the classes etc, you can post this.?

  • 1

    @Éowyn Hibernate/JDBC are frameworks developed to help in some occasions, more used for manipulation Entity - Bank, with it you can "map" the entities and define which attribute refers to a certain column. Some basic tutorial on the internet would show you how to do this.

  • I’ll take a look at it. Thank you! :)

1 answer

2


In a database, n to n relationship generates another intermediate table to store the ids of both sides.

When we think of objects, it’s a little different.

In this case it makes sense in the Favorite List class to have an attribute that stores a list of Movies, thus:

Java movie.:

public class Filme {
    int codFilme;
    double avaliacao;
    Date anoLancamento,duracao;
    String nome, genero;

    public Filme(int codFilme, double avaliacao, Date anoLancamento, Date duracao, String nome, String genero) {
        this.codFilme = codFilme;
        this.avaliacao = avaliacao;
        this.anoLancamento = anoLancamento;
        this.duracao = duracao;
        this.nome = nome;
        this.genero = genero;
    }

    public Filme() {
    }

    /*Getters e setters*/

}

Playlist.java

public class ListaFavoritos {
    int codLista;
    String nomeLista;
    Date dataCriacao;
    List<Filme> filmes;

    public ListaFavoritos(int codLista, String nomeLista, Date dataCriacao, List<Filme> filmes) {
        this.codLista = codLista;
        this.nomeLista = nomeLista;
        this.dataCriacao = dataCriacao;
        this.filmes = filmes;
    }

    public ListaFavoritos() {

    }

    /*Getters e Setters*/
}
  • True! I had thought about it but was afraid to run away from the model I made. Thank you for clarifying :)

Browser other questions tagged

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