在hibernate中运行M:N查询的最佳方法是什么?

时间:2014-10-05 15:14:36

标签: java sql hibernate hql

试图找出如何使用hibernate来调用以获取给定类型的电影列表?!

我尝试使用GenreDTO中的方法获取一组特定类型的电影。

Query hqlQuery = session.createQuery("FROM GenreDTO WHERE genre like :genre");

GenreDTO g = hqlQuery.setParameter("title","%" + term + "%").uniqueResult();

return g.getMovies();

有更好的方法吗?

CREATE TABLE Movie (
    id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
    title VARCHAR(255),
    poster VARCHAR(255),
    director VARCHAR(255),
    actors VARCHAR(255),
    synopsis VARCHAR(3000), 
    release_date TIMESTAMP
);

CREATE TABLE Genre(
    id INTEGER PRIMARY KEY NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
    genre VARCHAR(255)
);

CREATE TABLE MovieHasGenre(
    movie_id INTEGER REFERENCES Movie (id) NOT NULL,
    genre_id INTEGER REFERENCES Genre (id) NOT NULL,
    PRIMARY KEY (movie_id, genre_id)
);

package edu.unsw.comp9321.jdbc;

import java.util.HashSet;
import java.util.Set;

public class GenreDTO {


    public GenreDTO() {}

    public GenreDTO(int id, String genre) {
        super();
        this.id = id;
        this.genre = genre;
    }

    private int id;
    private String genre;
    private Set<MovieDTO> movies = new HashSet<MovieDTO>();

    public int getId() {
        return id;  
    }

    public void setId(int id) {
        this.id = id;
    }
    public String getGenre() {
        return genre;
    }
    public void setGenre(String genre) {
        this.genre = genre;
    }

    public Set<MovieDTO> getMovies() {
        return movies;
    }
    public void setMovies(Set<MovieDTO> movies) {
        this.movies = movies;
    }
}


package edu.unsw.comp9321.jdbc;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.OneToMany;


public class MovieDTO implements Comparable {
    private int id;
    private String title;
    private String poster;
    private String director;
    private String actors;
    private String synopsis;
    private String release_date;
    private int cinema_id;
    private Set<GenreDTO> genres = new HashSet<GenreDTO>();
    private Set<ReviewDTO> reviews = new HashSet<ReviewDTO>();
    private double rating;

    public MovieDTO() {
    }

    public MovieDTO(int id, String title, String poster, String director,
            String actors, String synopsis, String release_date, double rating) {
        super();
        this.id = id;
        this.title = title;
        this.poster = poster;
        this.director = director;
        this.actors = actors;
        this.synopsis = synopsis;
        this.release_date = release_date;
        this.rating = rating;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }

    public String getPoster() {
        return poster;
    }
    public void setPoster(String poster) {
        this.poster = poster;
    }

    public String getDirector() {
        return director;
    }
    public void setDirector(String director) {
        this.director = director;
    }

    public String getActors() {
        return actors;
    }
    public void setActors(String actors) {
        this.actors = actors;
    }

    public String getSynopsis() {
        return synopsis;
    }
    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public String getRelease_date() {
        return release_date;
    }
    public void setRelease_date(String release_date) {
        this.release_date = release_date;
    }

    public Set<GenreDTO> getGenres() {
        return genres;
    }
    public void setGenres(Set<GenreDTO> genres) {
        this.genres = genres;
    }

    public Set<ReviewDTO> getReviews() {
        return reviews;
    }
    public void setReviews(Set<ReviewDTO> reviews) {
        this.reviews = reviews;
    }

    public int getCinema_id() {
        return cinema_id;
    }
    public void setCinema_id(int cinema_id) {
        this.cinema_id = cinema_id;
    }

    public double getRating() {
        return rating;
    }
    public void setRating(double rating) {
        this.rating = rating;
    }

    @Override
    public int compareTo(Object o) {
        MovieDTO other = (MovieDTO) o;
        if (this.rating > other.rating) return -1;
        if (this.rating < other.rating) return 1;
        return 0;
    }
}

1 个答案:

答案 0 :(得分:2)

是。有一个更好的办法。只需使用联接:

select m from Movie m join m.genres g where g.genre like :genre

select m from Genre g join g.movies m where g.genre like :genre

请注意,使用like子句有点奇怪。您应该使用该类型的ID来唯一标识它。如果你只有一个流派的名字的一部分,你不应该假设只有一种类型包含这种类型的部分。

此外,DTO不是实体,反之亦然。不要将实体命名为GenreDTO或MovieDTO。将其命名为流派或电影。