如何处理构造函数中的无效参数?

发布于 2025-01-23 05:06:32 字数 2491 浏览 0 评论 0原文

我有两个类,用户team。每个用户可以具有favouriteTeam,但不必。

创建新用户时,请求通过,其中包含用户的用户名和他们喜欢的团队的teamId。如果用户没有喜欢的团队,则teamId值是null

中,使用提供的teamId值来调用TeamRepository findbyTeamId 。如果teamIdnull,则表中没有teamId null so so 的值代码> FindbyTeamID 将返回空值。因此,需要可选<

但是,这会在构造新的用户时会导致问题,因为需要类型team的参数,而不是可选< team>。我尝试将用户的类型更改为 可选< team>,但这会导致不同的问题(org.springframework.orm.orm.jpa.jpa.jpasystempection:错误:错误:错误:错误:错误:错误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 :错误访问字段[private long mygroup.tqbcbackend.model.team.teamid]通过持久属性的反射[mygroup.tqbcbackend.model.team#teamId]:optional.empty)。

解决这个问题的最佳方法是什么?

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);

}

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);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街道布景 2025-01-30 05:06:32

因此可选&lt; team&gt;是必需的。,不,不是(但方便)。

您可以声明

public Team findByTeamID(Long teamID);

,如果不存在TeamID,则会将(团队)null回来。

如果您待在可选的情况下,您可以致电用户的CTOR

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

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);
空城旧梦 2025-01-30 05:06:32

尝试在用户的构造函数中处理它,

如果(fairm team == null){
...
}

别的{
...
}

Try to handle it in the constructor of user

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

else{
...
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文