Home > front end >  How to deal with nullable parameter in constructor?
How to deal with nullable parameter in constructor?

Time:04-22

I have two classes, User and Team. Each User can have a favouriteTeam but doesn't have to.

When creating a new user, a request comes through which contains the user's username and the teamID of their favourite team. If the user doesn't have a favourite team, the teamID value is null.

In TeamRepository, findByTeamId is called using the provided teamID value. If teamID is null, there are no rows in the table being queried with a teamID value of null so findByTeamId will return a null value. Hence Optional<Team> is required.

However, this causes problems when constructing a new User, because a parameter of type Team is required, not Optional<Team>. I have tried changing the type of Users.favouriteTeam to Optional<Team> but that causes a different problem (org.springframework.orm.jpa.JpaSystemException: Error accessing field [private long mygroup.tqbcbackend.model.Team.teamID] by reflection for persistent property [mygroup.tqbcbackend.model.Team#teamID] : Optional.empty; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private long mygroup.tqbcbackend.model.Team.teamID] by reflection for persistent property [mygroup.tqbcbackend.model.Team#teamID] : Optional.empty).

What would be the best way of dealing with this problem?

User.java

@Entity
@Table(name = "Users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "UserID")
    private long userID;
    
    @Column(name = "Username")
    private String username;

    @Nullable
    @ManyToOne(
            targetEntity = Team.class,
            fetch = FetchType.LAZY
    )
    @JoinColumn(name = "FavouriteTeamID")
    private Team favouriteTeam;

    public User() {
    }

    public User(String username, Team favouriteTeam) {
        this.username = username;
        this.favouriteTeam = favouriteTeam;
    }

    // getters and setters
}

Team.java

@Entity
@Table(name = "Teams")
public class Team {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "TeamID")
    private long teamID;

    @Column(name = "TeamName")
    private String teamName;

    @OneToMany(
            targetEntity = User.class,
            fetch = FetchType.LAZY,
            mappedBy = "favouriteTeam"
    )
    private List<User> fans;

    // constructors and getters and setters 
}

TeamRepository.java

@Repository
public interface TeamRepository extends JpaRepository<Team,String>{
    
    public Optional<Team> findByTeamID(Long teamID);

}

CodePudding user response:

Hence Optional<Team> is required., no, it is not (but convenient).

You may declare

public Team findByTeamID(Long teamID);

And you get a (Team) null back, if teamID not exists.

If you stay with Optional you may call the ctor of User with

new User(((Optional)team).isPresent()?((Optional)team).get():null);

CodePudding user response:

Try to handle it in the constructor of user

If(favourite Team == null) { ... }

else{ ... }

  • Related