如何处理构造函数中的无效参数?
我有两个类,用户
和team
。每个用户
可以具有favouriteTeam
,但不必。
创建新用户时,请求通过,其中包含用户的用户名和他们喜欢的团队的teamId
。如果用户没有喜欢的团队,则teamId
值是null
。
在中,使用提供的
findbyTeamId 。如果teamId
值来调用TeamRepositoryteamId
是null
,则表中没有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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
因此可选&lt; team&gt;是必需的。
,不,不是(但方便)。您可以声明
,如果不存在TeamID,则会将
(团队)null
回来。如果您待在可选的情况下,您可以致电用户的CTOR
Hence Optional<Team> is required.
, no, it is not (but convenient).You may declare
And you get a
(Team) null
back, if teamID not exists.If you stay with Optional you may call the ctor of User with
尝试在用户的构造函数中处理它,
如果(fairm team == null){
...
}
别的{
...
}
Try to handle it in the constructor of user
If(favourite Team == null) {
...
}
else{
...
}