簡體   English   中英

休眠復雜的數據檢索?

[英]Hibernate complex data retrieval?

我的數據庫中有九個相關表。我必須在過濾用戶請求后檢索記錄。

我的實體如下,

電影

@Entity
@Table(name = "movie")
public class Movie implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "movie_id")
    private int movieId;

    @Column(name = "category_id")
    private Integer categoryId;

    @Column(name = "movie_title")
    private String movieTitle;

    @Column(name = "movie_description", columnDefinition = "TEXT")
    private String movieDescription;

    @Column(name = "movie_summary", columnDefinition = "TEXT")
    private String movieSummary;

    private Integer status;

    @Column(name = "language_id")
    private Integer languageId;

    @Column(name = "banner_image_url")
    private String bannerImageUrl;

    @Column(name = "imdb_rating")
    private Integer imdbRating;

    @Column(name = "rotten_tomatoes_rating")
    private Integer rottenTomatoesRating;

    @Column(name = "user_avg_rating")
    private Float userAvgRating;

    @Column(name = "main_genre_id")
    private Integer mainGenreId;

    @Column(name = "secondary_genre_id")
    private Integer secondaryGenreId;

    @Column(name = "created_by_user_id")
    private Integer createdByUserId;
}

類別

@Entity
@Table(name = "category")
public class FetchSubCategory implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "category_id")
    private Integer categoryId;

    @Column(name = "category_name")
    private String categoryName;
}

電影演員映射

@Entity
@Table(name = "movie_actor_mapping")
public class MovieActorMapping implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "mapping_id")
    private int mappingId;

    @Column(name = "movie_id")
    private Integer movieId;

    @Column(name = "actor_id")
    private Integer actorId;
}

電影演員

@Entity
@Table(name = "movie_actors")
public class MovieActors implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "actor_id")
    private int actorId;

    @Column(name = "actor_name")
    private String actorName;
}

電影流派

@Entity
@Table(name = "movie_genre")
public class MovieGenre implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "genre_id")
    private int genreId;

    @Column(name = "genre_name")
    private String genreName;

    @Column(name = "created_by_user_id")
    private Integer createdByUserId;
}

電影語言

@Entity
@Table(name = "movie_language")
public class MovieLanguage implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "language_id")
    private int languageId;

    @Column(name = "language_name")
    private String languageName;

    @Column(name = "created_by_user_id")
    private Integer createdByUserId;

    @Column(name = "last_updated_user_id")
    private Integer lastUpdatedUserId;
}

用戶將如下要求。all是可選字段,

{
"subCategory":"New Release",
"language":"Malayalam",
"actor":"allu arjun",
"filmGenre":"'Family'"
}

根據請求,我將使用子查詢通過檢查相應表中的條件來返回movie列表。

方法

public List<Movie> getFilterMovieList(FilterMovieRequest filterMovieRequest) throws SQLException, ClassNotFoundException, IOException {

        List<Movie> movies = null;
        try {
            String subCategory = filterMovieRequest.getSubCategory();
            String language = filterMovieRequest.getLanguage();
            String actor = filterMovieRequest.getActor();
            String filmGenre = filterMovieRequest.getFilmGenre();

            String contained = "where";
            String sql = "from Movie as M ";

            if (actor.length() > 1) {
                sql += contained + " movieId in(select movieId from MovieActorMapping where actorId in(select actorId from MovieActors where actorName='" + actor + "')) ";

                contained = "and";
            }

            if (subCategory.length() > 1) {

                sql += contained + " M.categoryId=(select categoryId from FetchSubCategory where categoryName='" + subCategory + "') ";
                contained = "and";
            }

            if (language.length() > 1) {

                sql += contained + " M.languageId=(select languageId from MovieLanguage where languageName='" + language + "') ";
                contained = "and";
            }

            if (filmGenre.length() > 1) {

                sql += contained + " (M.mainGenreId in(select genreId from MovieGenre where genreName in(" + filmGenre + ")) or M.secondaryGenreId in(select genreId from MovieGenre where genreName in(" + filmGenre + ")))";
                contained = "and";
            }

            if (contained.equals("and")) {
                Session session = sessionFactory.getCurrentSession();
                Query query = session.createQuery(sql);
                movies = query.list();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return movies;

    }

而且效果很好。問題是我現在必須結合劇院電影和放映時間的結果。

我的劇院相關表格如下,

theatre_movie_mapping

在此處輸入圖片說明 Theatre_list

在此處輸入圖片說明

show_timings

在此處輸入圖片說明

您可以在movie_idtheater_movie_mapping列中theater_movie_mapping與我的基本表movie有關的列。 使用我們可以獲取theater_idshow_id為獲取劇院和顯示timing..note,我有一個電影列表上方conditions.How我可以從show_timings結合劇院從theater_list和放映時間檢查后早期牽強? 作為一名Android開發人員,對我來說似乎很復雜。 任何幫助將不勝感激。正在使用Spring restful webservice

現在我得到以下格式的結果,

[
    {
        "movieId": 8,
        "categoryId": 14,
        "movieTitle": "Kanyaka Talkies",
        "movieDescription": "CRITICS 3 out of 5 (Good) 3 out of 5 (Good) The composite emotional weather that the film sports makes it maddening and nurturing at once, rendering it an almost enigmatic feel. And it is this ethereal complexity that 'Kanyaka Talkies' inherently has, that makes the film singular. ",
        "movieSummary": "The concurrence of the three key characters in 'Kanyaka Talkies' isn't of the traditionalist kind; rather, by throwing the three of them together, the film does achieve the ostensibly improbable feat of placing the unlikeliest of players collectively on board, with their fates irrevocably intertwined with each other. ",
        "status": 1,
        "languageId": 1,
        "bannerImageUrl": "0",
        "imdbRating": 1,
        "rottenTomatoesRating": 3,
        "userAvgRating": 2,
        "mainGenreId": 1,
        "secondaryGenreId": 2,
        "createdByUserId": 16
    },
    {
        "movieId": 9,
        "categoryId": 14,
        "movieTitle": "Wonderful Journey",
        "movieDescription": "Wonderful Journey' is one of the most misdirecting titles as yet for a film this year. Anything but wonderful, this is an absolute cinematic misadventure that will have you pulling out your hair strands in no time. ",
        "movieSummary": "Some things in life simply cannot be averted, they say. I do agree, what with the late night show of 'Wonderful Journey' getting cancelled yesterday night and me courageously venturing out for it yet again today noon, only to embark on one of the most horrendous journeys I have ever gone for in my entire life. ",
        "status": 1,
        "languageId": 1,
        "bannerImageUrl": "0",
        "imdbRating": 1,
        "rottenTomatoesRating": 3,
        "userAvgRating": 2,
        "mainGenreId": 1,
        "secondaryGenreId": 1,
        "createdByUserId": 16
    },
    {
        "movieId": 10,
        "categoryId": 14,
        "movieTitle": "Oru New Generation Pani",
        "movieDescription": "Very occasionally does a movie come along that almost makes you vow to stay off the screens for a few weeks, and this year, the one has finally arrived. I'd gladly go ahead with a no-star rating for this one, had it not been for a technical glitch that prevents me from doing so! ",
        "movieSummary": "'Oru New Generation Pani' is an atrocity that shocks you with its attempt to spin out a story line that will have you banging you head against the rails. Inauthentic to the core, the film tells a story that will have an insomniac snoring away in no time. ",
        "status": 1,
        "languageId": 1,
        "bannerImageUrl": "0",
        "imdbRating": 1,
        "rottenTomatoesRating": 3,
        "userAvgRating": 2,
        "mainGenreId": 1,
        "secondaryGenreId": 2,
        "createdByUserId": 16
    }
]

我必須在每個json對象(即每部電影)中添加劇院並放映時間。

假設Theater_movie_mapping映射到其中包含Movie movie Class TheaterMovie

ArrayList<TheaterMovie> list = new ArrayList<TheaterMovie>();
for(int i = 0 ; i < movies.size() ; i++){
list.addAll((ArrayList<TheaterMovie>)createQuery("From TheaterMovie tm where tm.movies = :mo").setParameter("mo",movies.get(i)).getList());
}

現在假設將show_timings映射到其中包含Theater theater Class ShowTiming

ArrayList<ShowTiming> showTimeList = new ArrayList<ShowTiming>();
for(int i = 0 ; i < list.size() ; i++){
showTimeList = (ArrayList<ShowTiming>)createQuery("From ShowTiming st where st.theater = :th").setParameter("th",list.get(i).getTheater()).getList();
}

我希望這對你有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM