Kri*_*ish 0 java sql spring set spring-boot
我是 Hibernate 的新手,我正在尝试使用下面的代码处理一对多关系,但我遇到了异常。有人可以帮助我吗我错了
@SpringBootApplication
public class TicketBookingManagementAppApplication implements CommandLineRunner {
@Autowired
PostsRepository postsRepository;
public static void main(String[] args) {
SpringApplication.run(TicketBookingManagementAppApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
Comments comments1 = new Comments();
comments1.setName("comment1");
comments1.setDesc("desc1");
Comments comments2 = new Comments();
comments2.setName("comment2");
comments2.setDesc("desc2");
Posts posts = new Posts();
posts.setName("post5");
posts.getComments().add(comments1);
posts.getComments().add(comments2);
postsRepository.save(posts);
}
}
Run Code Online (Sandbox Code Playgroud)
@Entity
@Table(name = "posts")
public class Posts {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int post_id;
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "posts", cascade = CascadeType.ALL)
private Set<Comments>comments=new HashSet<>();
public int getPost_id() {
return post_id;
}
public void setPost_id(int post_id) {
this.post_id = post_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Comments> getComments() {
return comments;
}
public void setComments(Set<Comments> comments) {
this.comments = comments;
}
}
Run Code Online (Sandbox Code Playgroud)
@Entity
@Table(name = "comments")
public class Comments {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int comment_id;
private String name;
private String desc;
@ManyToOne
@JoinColumn(name = "post_id", nullable = false)
private Posts posts;
public int getComment_id() {
return comment_id;
}
public void setComment_id(int comment_id) {
this.comment_id = comment_id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
Run Code Online (Sandbox Code Playgroud)
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc, name, post_id) values ('desc1', 'comment1', null)' at line 1
Run Code Online (Sandbox Code Playgroud)
孩子持有关系,所以需要设置父字段:
@Override
public void run(String... args) throws Exception {
Comments comments1 = new Comments();
comments1.setName("comment1");
comments1.setDesc("desc1");
Comments comments2 = new Comments();
comments2.setName("comment2");
comments2.setDesc("desc2");
Posts posts = new Posts();
comments1.setPosts(posts);
comments2.setPosts(posts);
posts.setName("post5");
posts.getComments().add(comments1);
posts.getComments().add(comments2);
postsRepository.save(posts);
}
Run Code Online (Sandbox Code Playgroud)
编辑:问题是您已经命名了字段desc,女巫将被翻译为列名desc。这是sql()中的保留关键字order by ... desc。
| 归档时间: |
|
| 查看次数: |
6747 次 |
| 最近记录: |