Skip to content

@OneToMany relationship returns empty #121

@tmussi

Description

@tmussi
Issue Overview

Relationship mapped with @OneToMany always returning an empty list.

I made some changes on Arquillian Persistence Tutorial adding consoles attribute on Game.java as you can see below:

public class Game implements Serializable {

    @OneToMany(fetch = FetchType.EAGER, mappedBy = "game")
    private List<Console> consoles;
    
    // getters and setters

    public List<Console> getConsoles() {
        return consoles;
    }

    public void setConsoles(List<Console> consoles) {
        this.consoles = consoles;
    }
}

And creating a new class called Console.java

package org.arquillian.example;

import javax.persistence*;
import javax.validation.constraints.*;

@Entity
public class Console {

    @Id
    @GeneratedValue
    private Long id;
    @NotNull
    @Size(min = 3, max = 50)
    private String name;
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "game_id")
    private Game game;

    // getters and setters
}
Expected Behaviour

On GamePersistenceTest.java class, I updated the method insertData() to look like this:

private static final String[] CONSOLES = { "PS4", "XBOX360" };

private void insertData() throws Exception {
    utx.begin();
    em.joinTransaction();
    System.out.println("Inserting records...");
    for (String title : GAME_TITLES) {
        Game game = new Game(title);
        em.persist(game);
        for (String consoleName : CONSOLES) {
            Console console = new Console();
            console.setGame(game);
            console.setName(consoleName);
            em.persist(console);
        }
    }

    utx.commit();
    // reset the persistence context (cache)
    em.clear();
}

But, when I get the Game from the database it is always returning me an empty list

@Test
public void shouldFindAllGamesUsingJpqlQuery() throws Exception {
    // given
    String fetchingAllGamesInJpql = "select g from Game g order by g.id";

    // when
    System.out.println("Selecting (using JPQL)...");
    List<Game> games = em.createQuery(fetchingAllGamesInJpql, Game.class).getResultList();

    // then
    System.out.println("Found " + games.size() + " games (using JPQL):");

    for (Game game : games) {
        System.out.println(game);
        System.out.println("Consoles: " + game.getConsoles().size());
        for (Console console : game.getConsoles()) {
            System.out.println(console);
        }
        System.out.println("*********");
    }

    assertContainsAllGames(games);
}

And when I try get the consoles on Console table on database, I can retrieve all of them associated with a Game.

@Test
public void shouldFindAllConsolesWithGamesWithJPQL() throws Exception {
    String sql = "SELECT c FROM Console c ORDER by c.id";
    List<Console> consoles = em.createQuery(sql, Console.class).getResultList();
    System.out.println("Found " + consoles.size() + " consoles (JPQL)");
    for (Console console : consoles) {
        System.out.println(console);
    }
}
Current Behaviour

When I get a Game from the database it is always returning me an empty list.

Additional Information

All changes was made over Arquillian Persistence Tutorial
I'm running GamePersistenceTest.java as a JUnit Test on Eclipse IDE Version: Oxygen.2 (4.7.2)

I'm facing this problem on a personal project when running on a JUnit environment, but if I publish it on my local WebLogicServer I do not have this problem and the list returns with the expected result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions