数据插入对两个类似实体的作用不同

时间:2018-04-13 20:59:39

标签: java android sqlite android-room

我的简单电影应用程序中有两个实体:电影。当我尝试将初始数据添加到数据库时,发生了一些奇怪的事情。即使我的两个实体非常相似,插入人工作正常,插入电影导致11(当7的数组作为参数传递)电影元素,其中包含空字段:

I/System.out: [Movie{id=23, title='null', genre='null', 
posterResource=null}, Movie{id=24, title='null', genre='null', 
posterResource=null} ... Movie{id=33, title='null', genre='null', 
posterResource=null}]
I/System.out: [Person{id=23, firstName='Brad', lastName='Pitt', age=55, 
imgResource=2131165277}, Person{id=24, firstName='Edward', 
lastName='Norton', age=49, imgResource=2131165286}, ... , Person{id=33, 
firstName='Joaquin', lastName='Phoenix', age=44, imgResource=2131165293}]

我的LocalDataSource的内部,我在其中插入数据:

private AppDatabase mDataBase;

public LocalDataSource(Context mContext) {
    mDataBase = AppDatabase.getAppDatabase(mContext);
    mDataBase.personDao().deleteAllPeople();
    mDataBase.movieDao().deleteAllMovies();
    setupTestMovieData();
    setupTestActorsData();
    setupTestMovieActorRelationship();
    System.out.println(getAllMovies());
    System.out.println(getAllActors());
}

public List<Movie> getAllMovies(){
    return mDataBase.movieDao().loadAllMovie();
}

public List<Person> getAllActors(){
    return mDataBase.personDao().loadAllPeople();
}

public List<Person> getActorsFromMovie(long movieId){
    return mDataBase.actorMovieJoinDao().getActorsFromMovie(movieId);
}

private void setupTestMovieData() {
    MovieDao dao = mDataBase.movieDao();
    dao.insertMovie(getTestMovieArray());
}

private Movie[] getTestMovieArray() {
    Movie[] movies = new Movie[7];
    movies[0] = new Movie("Fight club", "Thriller", R.drawable.fightclub);
    movies[1] = new Movie("Prestige", "Thriller",R.drawable.prestige);
    movies[2] = new Movie("500 days of Summer","Drama", R.drawable.the500daysofsummer);
    movies[3] = new Movie("Seven","Detective", R.drawable.kiler);
    movies[4] = new Movie("Inception","Action", R.drawable.pulpfiction);
    movies[5] = new Movie("American Hustle","Crime", R.drawable.phantomthread);
    movies[6] = new Movie("Her","Drama", R.drawable.dancerinthedark);
    return movies;
}

private void setupTestActorsData(){
    PersonDao dao = mDataBase.personDao();
    dao.insertPeople(getTestActorArray());
}

private Person[] getTestActorArray() {
    Person[] actors = new Person[11];
    actors[0] = createPerson("Brad", "Pitt", "18/12/1963", R.drawable.bradpitt);
    actors[1] = createPerson("Edward", "Norton", "18/8/1969", R.drawable.edwardnorton);
    actors[2] = createPerson("Helena", "Bonham-Carter", "26/5/1966", R.drawable.helenabonhamcarter);
    actors[3] = createPerson("Joseph", "Gordon-Levitt", "17/2/1981", R.drawable.josephgordonlevitt);
    actors[4] = createPerson("Zooey", "Deschanel", "17/1/1980", R.drawable.zooeydeschanel);
    actors[5] = createPerson("Christian", "Bale", "30/1/1974", R.drawable.christianbale);
    actors[6] = createPerson("Leonardo", "DiCaprio", "11/11/1974", R.drawable.leonardodicaprio);
    actors[7] = createPerson("Morgan", "Freeman", "1/6/1937", R.drawable.morganfreeman);
    actors[8] = createPerson("Amy", "Adams", "20/8/1974", R.drawable.amyadams);
    actors[9] = createPerson("Scarlett", "Johansson", "22/11/1984", R.drawable.scarlettjohansson);
    actors[10] = createPerson("Joaquin", "Phoenix", "28/10/1974", R.drawable.joaquinphoenix);
    return actors;
}

private Person createPerson(String firstName, String lastName, String date, Integer imgResource){
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    GregorianCalendar gregorianCalendar = new GregorianCalendar();
    try {
        gregorianCalendar.setTime(sdf.parse(date));
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return new Person(firstName, lastName, gregorianCalendar, imgResource);
}

0 个答案:

没有答案