gpt4 book ai didi

java - 外键仅适用于 Spring JPA 项目中的某些模型

转载 作者:行者123 更新时间:2023-12-02 08:54:12 24 4
gpt4 key购买 nike

我的 spring jpa 项目遇到了一个奇怪的问题。基本上对于我的一个模型来说,外键似乎没有按预期工作。在我的项目中,我有一个模型类“Player”和另一个“PlayerStats”。

在“玩家统计数据”中,我添加单个玩家的统计数据并将其存储在我的数据库中。我使用基本的 html 作为简单的 Web 表单来输入数据。这工作正常,但“player_id”列始终为 NULL。这不会打扰我,但我想在另一个表中显示每个玩家的个人统计数据,如果我无法解决此问题,则所有其他玩家的统计数据都可以查看。

但是,我对项目中的不同模型类以相同的方式进行了设置,没有任何问题。例如,我有一个用户模型类,“user_id”用作模型类“Team”和“Player”中的外键。那里没有看到问题。因此,非常感谢任何有关我出错的地方的帮助。

1。玩家模型类

@Data
@Entity
@Table(name="User_Player")
public class Player implements Serializable {

@OneToOne
private User user;
private static final long serialVersionUID = 1L;

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

private String playerName;
private String dob;
private String hometown;
private String weight;
private String height;
private Position position;



}

2。 PlayerStats 模型

@Data
@Entity
@Table(name="Player_Stats")


public class PlayerStats implements Serializable {

@ManyToOne
private Player player;


private static final long serialVersionUID = 1L;

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

private int td;
private int rushYards;
private int recYards;
private int returnYards;
private int throwingYards;
private int throwAttenpts;
private int throwSuccess;
private int receptions;
private int rushTD;
private int recTD;
private int fumbles;
private int fumblesRecovered;
private int forcedFumbles;
private int sacks;
private int puntsBlocked;
private int fgScored;
private int fgMissed;
private int punts;
private int tackle;

public Opponents opponents;
}

3。 PlayerStats Controller

@Slf4j
@Controller
@SessionAttributes("PlayerStats")
public class PlayerStatsController {

private PlayerStatsRepository playerStatsRepository;

public PlayerStatsController(PlayerStatsRepository playerStatsRepository)
{this.playerStatsRepository = playerStatsRepository;}

@Autowired
PlayerStatsRepository service;

@GetMapping("/addPlayerStats/{id}")
public String showSignUpFormPlayer(Player player) {
return "addPlayerStats";
}

@PostMapping("/addPlayerStats/{id}")
public String processPlayer(@Valid PlayerStats playerStats, BindingResult result,
SessionStatus sessionStatus,
@AuthenticationPrincipal Player player, Model model) {
if (result.hasErrors()) {
return "playerStats";
}

//playerStats.setPl(player);
service.save(playerStats);
model.addAttribute("playerStats", service.findAll());
return "login";
}

4。用户模型

   @Entity
@Data
@NoArgsConstructor(access= AccessLevel.PRIVATE, force=true)
@RequiredArgsConstructor
public class User implements UserDetails {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private Long id;
private final String username;
private final String password;
//private final String fullname;


@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}

最佳答案

正如 Sariq 所说,你应该将Player 设置为playerStats。

playerStats.setPlayer(player);

我还建议在 PlayerStats 类中显式声明外键。通过使用 @JoinColumn 注释来执行此操作,并确保使用数据库中的确切名称。

@ManyToOne
@JoinColumn(name = "player_id") // player_id has to match your foreign key in the database
private Player player;

关于java - 外键仅适用于 Spring JPA 项目中的某些模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60591861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com