我创建了一个由休眠完美创建的“用户”表,对此没有任何问题。问题出在我的第二个模型(称为“角色”的实体)上,该模型是具有@ManyToOne关系的模型,我不知道为什么,但是休眠无法创建第二个表。
我的服务器是具有AWS RDS的Mysql实例。第一个表是由休眠创建的,而不是第二个。
@实体 公共类字符{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
@ManyToOne
private User joueur;
private String pseudo;
private Integer points;
public Character() {
super();
}
public Character(User joueur) {
// appel a l'autre constructeur
this(joueur, null, null);
}
public Character(User joueur, String pseudo,Integer points) {
super();
this.joueur = joueur;
this.pseudo = pseudo;
this.points = points;
}
public Integer getId() {
return id;
}
public String getPseudo() {
return pseudo;
}
public void setPseudo(String pseudo) {
this.pseudo = pseudo;
}
public Integer getPoints() {
return points;
}
public void setPoints(Integer points) {
this.points = points;
}
public User getJoueur() {
return joueur;
}
}
@Entity // //告诉Hibernate用该类制作一个表 公共类用户{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String username;
private String password;
@OneToMany(mappedBy = "joueur")
@OrderBy("id ASC")
private List<Character> personnages;
private String league;
private Integer points;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public List<Character> getPersonnages() {
return personnages;
}
public void setPersonnages(List<Character> personnages) {
this.personnages = personnages;
}
public String getLeague() {
return league;
}
public void setLeague(String league) {
this.league = league;
}
public Integer getPoints() {
return points;
}
public void setPoints(Integer points) {
this.points = points;
}
}
@Controller
@RequestMapping(path =“ / init”)
公共类MainController {
@Autowired // This means to get the bean called userRepository
// Which is auto-generated by Spring, we will use it to handle the data
private UserRepository userRepository;
@Autowired
private CharacterRepository characterRepository;
@GetMapping(path="/add") // Map ONLY GET Requests
public String addNewUser () {
User joueur = new User();
joueur.setUsername("testUser");
joueur.setPassword("password");
joueur.setLeague("Bronze");
joueur.setPoints(10000);
userRepository.save(joueur);
Character perso = new Character(joueur,"testPerso1",1000);
characterRepository.save(perso);
return "";
}
@GetMapping(path="/all")
public @ResponseBody Iterable<User> getAllUsers() {
// This returns a JSON or XML with the users
return userRepository.findAll();
}
}
我有此错误: “ com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以获取在'字符添加约束FKdf2yvyvitaqt2u7de3ywfjcv外键(joueur_id)引用附近使用正确的语法'在第1行”
答案 0 :(得分:0)
好吧,我找到了解决方法... JPA批注放弃了表'Character'的名称,只需更改表的名称即可,它将完美地工作,愚蠢的JPA ...