I think it's a simple question, but I could not find any examples that would make that clear. I have already done my database model in PostGreSQL and at the time of creating the classes I got a little lost. I have List entities (stores user's favorite movies) and Movie (which stores movie data) and there is a N: N relationship between these entities, ie multiple movies can be associated with multiple lists and vice versa. I created a ListaXFilme table that will have 2 foreign keys (one to identify the other one to identify the list) and the primary key of this table will be composed by the 2 foreign keys. I think I've been doing it right ... My question is how do I create classes that represent this relationship.
The table scripts 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:
Favorites.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*/
}
Movie.java:
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*/
}