2

I have a class called User, within this class they previously had the following properties:

@Entity(name = "User")
@Table(name = "user")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "username", unique = true)
    private String username;

    @Column(name = "password")
    private String password;
    
    private Integer victories;
    private Integer defeats;

    private Double victoryRatio;
}

However, I now need to draw a distinction between different game types, for example there will be competitive games and non-competitive games.

This means that every time a comp OR non-comp game is played the standard victories, defeats and victory ratio must be updated. But when a comp game is played I also need the new 'comp' variables to increase also as below:

@Entity(name = "User")
@Table(name = "user")
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name = "username", unique = true)
    private String username;

    @Column(name = "password")
    private String password;
    
    private Integer victories;
    private Integer defeats;

    private Double victoryRatio;

    private Integer compVictories;
    private Integer compDefeats;        
    private Double compVictoryRatio;
}

This leads to a lot of code duplication within other classes such as service classes. I want to create a new class called UserStats as such:

public class UserStats {
   
    private Integer victories;
    private Integer defeats;
    
    private Double victoryRatio;

}

And then have within the original User entity:

private UserStats userStats;
private UserStats compUserStats;

But unfortunately this is not working and IDEA has highlighted the 'UserStats' in the above code in red.

Do I need to create the UserStats as a seperate entity and then do like:

@OneToMany
private UserStats userStats;

@OneToMany
private UserStats compUserStats;

I am unsure how to do this the best way, any help would be appreciated, thanks.

devo9191
  • 219
  • 3
  • 13
  • Is there a reason you are using a `@OneToMany` here? Why would a single user have many different competitive victory ratios? – cjnash Jan 12 '22 at 15:31
  • They wouldn't but they would have many different UserStats, 1 for competitive games and 1 for non-comp games? – devo9191 Jan 12 '22 at 15:40
  • Well then wouldn't it be a `@OneToOne` with userStats and a `@OneToOne` with compuUserStats? It doesn't fix your red underline of UserStats - I would recommend creating UserStats as an entity like you said and importing UserStats to fix that issue. – cjnash Jan 12 '22 at 15:48

1 Answers1

0

And then have within the original User entity:

private UserStats userStats;
private UserStats compUserStats;

That looks doable in Java. What is the IDE's error message? It most likely isn't able to map to a JPA entity. In your UserStats Class, did you correctly define this? Does your database have table a for this? https://www.baeldung.com/jpa-entities

JonR85
  • 700
  • 4
  • 12
  • The IDE offered no reason as to why it was red, it just said 'inspect jpa', but no I didn't define the UserStats class as a JPA entity, was I supposed to? thanks. – devo9191 Jan 12 '22 at 15:05
  • When I have just tried this, the red line says 'mark as a ManyToOne or OneToOne relationship" – devo9191 Jan 12 '22 at 15:08
  • But it can't be one to one can it? Since each user will be within 2 UserStats, or is that still one to one as there is only one user in each UserStats? thanks. – devo9191 Jan 12 '22 at 15:08
  • Sounds like you want a One to Many relationship ( User -> UserStats). To have this though. You will need UserId either on your or UserStats table or a joining table. To give JPA something to map to. https://stackoverflow.com/questions/5478328/in-which-case-do-you-use-the-jpa-jointable-annotation – JonR85 Jan 12 '22 at 17:42
  • Also, you might have to have a column on the table to define which type of UserStat it is. – JonR85 Jan 12 '22 at 18:33