arquillian / arquillian-extension-persistence

Arquillian Database / Persistence Extension
Apache License 2.0
75 stars 68 forks source link

@OneToMany relationship returns empty #121

Open tmussi opened 5 years ago

tmussi commented 5 years ago
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.

bartoszmajsak commented 5 years ago

Hi @tmussi thanks for detailed issue report. Is there any chance you can share this modified version?

Additional question - in your personal project what exact environment you are running tests against? This like those on the list below would greatly help troubleshooting

tmussi commented 5 years ago

Sorry for late reply.

I provided some changes over arquillian-persistence-tutorial on my github. Please, have a look: https://github.com/tmussi/arquillian-examples

You will find everything on GamePersistenceTest.java related to both JPA entities: Game and GameReview. There, you will also find two test cases:

Edit: I'm running tests from Eclipse through JUnit (righ click on GamePersistenceTest class > Run As > JUnit Test)

bartoszmajsak commented 5 years ago

Thanks, I will have a look