1

Getting the following error when I try to add a "Game" object to my ArrayList specified in my User class. This action was done in my controller class:

"org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.billsheng.huddlespringmvc.models.Game; nested exception is java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.billsheng.huddlespringmvc.models.Game"

User Entity

@Data
@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;

    private String email;
    private String firstName;
    private String lastName;
    private String password;
    private String chosenTeam;
    private int gamesPlayed;
    private int gamesWon;

    @ElementCollection
    private List<Game> games = new ArrayList<>();

    public User(String firstName, String lastName, String email, String password, String chosenTeam, int gamesPlayed, int gamesWon, ArrayList<Game> games) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
        this.password = password;
        this.chosenTeam = chosenTeam;
        this.gamesPlayed = gamesPlayed;
        this.gamesWon = gamesWon;
        this.games = games;
    }

    public User(String firstName, String lastName, String email, String password, String chosenTeam) {
        this(firstName, lastName, email, password, chosenTeam, 0, 0, null);
    }

    public User(int gamesPlayed, int gamesWon, ArrayList<Game> games) {
        this.gamesPlayed = gamesPlayed;
        this.gamesWon = gamesWon;
        this.games = games;
    }

    public User() {
    }

//getters and setters
}

Game Entity

@Data
@Entity
public class Game {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Integer id;
    private String homeTeam;
    private String awayTeam;
    private Date date;
    private String location;
    private String bettingOdds;
    private boolean inProgress;
    private boolean isReviewed;

//getters and setters
}

I did some research and I believe this issue stems from me using my Game object inside my User object but I'm not sure what the problem is specifically. All help is appreciated.

Bill Sheng
  • 101
  • 2
  • 3
  • 10
  • 1
    Have you tried to map the collection with `@OneToMany` and configure the cascade? – nortontgueno Mar 19 '19 at 00:08
  • thanks, i tried to map @OneToMany and I got the same error. What do you mean by "configure the cascade" – Bill Sheng Mar 19 '19 at 00:13
  • 1
    [Cascade thread](https://stackoverflow.com/questions/3667387/what-is-the-difference-between-cascade-inverse-in-hibernate-what-are-they-use) and a [good reading](https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/) for mapping – nortontgueno Mar 19 '19 at 00:17
  • 1
    This isn't a Spring thing; `@Entity` is about JPA. Spring provides help for using JPA features, but this is entirely JPA. Regarding ngueno's comment, read the documentation for the `cascade` attribute on `@OneToMany`. – chrylis -cautiouslyoptimistic- Mar 19 '19 at 00:21
  • Oops, yeah I'm new to Java api development. Thanks for the help guys. The following annotation worked: `@OneToMany(cascade = CascadeType.ALL)` – Bill Sheng Mar 19 '19 at 00:24

1 Answers1

0

@ElementCollection is not supposed to be used with collections of entities; it's used with collections of @Embeddable. If Thing is an entity, you don't use @ElementCollection, you use @OneToMany.

@ElementCollection: Defines a collection of instances of a basic type or embeddable class

You can use @OneToMany mapping for establishing the relationship between User and Game entity.

User.java

@OneToMany(cascade=CascadeType.ALL,fetch= FetchType.LAZY,mappedBy = "user")
private List<Game> games = new ArrayList<>();

//getters and setters

Game.java

@ManyToOne
private User user;

//getters and setters
Alien
  • 15,141
  • 6
  • 37
  • 57