我创建了一个“user”表,它完全是由hibernate创建的,在这个表上没有问题。问题出在我的第二个模型(一个名为“character”的实体)上,它是一个具有 @ManyToOne
我不知道为什么,但是hibernate不能创建第二个表。
我的服务器是一个带有awsrds的mysql示例。第一个表是由hibernate创建的,而不是第二个表。
@Entity
public class Character {
@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 // This tells Hibernate to make a table out of this class
public class User {
@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")
public class 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服务器版本对应的手册,以获得使用第1行“character add constraint kdf2yvyvitaqt2u7de3ywfjcv foreign key(joueur_id)refe”附近的正确语法
1条答案
按热度按时间fykwrbwg1#
好吧,我找到了解决办法。。。jpa注解fordid表名'character',只要改变表名,它就会工作得很好,愚蠢的jpa。。。