Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@OneToMany relationship returns empty #121

Open
tmussi opened this issue Mar 10, 2019 · 3 comments
Open

@OneToMany relationship returns empty #121

tmussi opened this issue Mar 10, 2019 · 3 comments

Comments

@tmussi
Copy link

tmussi commented Mar 10, 2019

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
Copy link
Member

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

  • application container
  • java version

@tmussi
Copy link
Author

tmussi commented Jun 28, 2019

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:

  • shouldFindGameReviewWithGame where I try to get GameReview with Game entity fetched .
  • shouldFindAllGamesAndReviewsJpqlQuery where I try to get Game with the list of GameReview loaded.

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

@bartoszmajsak
Copy link
Member

Thanks, I will have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants