作为Spring的新手,我试图填充两个通过键列连接的表。我可以在此处看到我的完整堆栈跟踪和创建的表:spring boot hibernate query invalid user error
另外,这是我的表格:
package blog.forms;
import org.springframework.format.annotation.DateTimeFormat;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.Date;
import java.util.List;
public class NewPostForm {
@Size(min=2, max=30, message = "Username size should be in the range [2...30]")
@NotNull
private String username;
private String FullName;
//@DateTimeFormat(pattern = "dd.MM.yyyy")
//private Date date;
//private Date date = new Date();
@NotNull
private String title;
@NotNull
private String post;
private List<String> references;
public List<String> getReferences() {
return references;
}
public void setReferences(List<String> references) {
this.references = references;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFullName() {
return FullName;
}
public void setFullName(String fullName) {
FullName = fullName;
}
/*public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}*/
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPost() {
return post;
}
public void setPost(String post) {
this.post = post;
}
}
控制器:
package blog.controllers;
import blog.LSH.Combinations_Ngrams;
import blog.LSH.Combinations_Ngrams2;
import blog.forms.NewPostForm;
import blog.models.Post;
import blog.models.User;
import blog.services.NotificationService;
import blog.services.PostService;
import blog.services.PostServiceStubImpl;
import blog.services.UserService;
import com.soundcloud.lsh.Main5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.validation.Valid;
import java.io.*;
import java.util.*;
@Controller
public class CreatePostController {
@Autowired
private PostService postService;
@Autowired
private UserService userService;
@Autowired
private NotificationService notifyService;
@RequestMapping(value = "/posts/create")
public String create(NewPostForm newPostForm) {
return "posts/create";}
@RequestMapping(value = "/posts/create", method = RequestMethod.POST)
public String createPost(@Valid NewPostForm newPostForm, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
notifyService.addErrorMessage("Please fill the form correctly!");
return "posts/create";
}
String new_name = newPostForm.getFullName();
String new_username = newPostForm.getUsername();
//Date new_date = newPostForm.getDate();
String new_title = newPostForm.getTitle();
String new_post = newPostForm.getPost();
Post new_post_record = new Post(0L, new_title, new_post,new User(10L, new_username, new_name));
//User new_user_record = new User(10L,new_username, new_name);
//userService.create(new_user_record);
postService.create(new_post_record);
notifyService.addInfoMessage("A new post is created!");
return "redirect:/";
}
触发postService.create(new_post_record);
时,我收到以下日志和错误:
Hibernate: select post0_.id as id1_0_0_, post0_.author_id as author_id5_0_0_, post0_.body as body2_0_0_, post0_.creation_date as creation_date3_0_0_, post0_.title as title4_0_0_ from posts post0_ where post0_.id=?
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into posts (author_id, creation_date, title, id, body) values (?, ?, ?, ?, ?)
2017-02-25 18:12:07.215 WARN 68332 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1, SQLState: 23000
2017-02-25 18:12:07.215 ERROR 68332 --- [nio-8080-exec-5] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-00001: unique constraint (MEHMET.POSTS_PK) violated
首先,为什么hibernate会改变列的顺序?也就是说,正如您从上面的链接中看到的那样,订单类似于:posts表中的 id,author_id,title,body,creation_date 。但是,从上面的日志中可以看出,hibernate会改变它吗?
作为我的第二次试用,我更改了@GeneratedValue注释,如下所示:
对于帖子表:
@Entity
@Table(name = "posts")
public class Post {
@Id
@SequenceGenerator(name="seq_post",sequenceName="oracle_posts_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_post")
对于用户表:
@Entity
@Table(name = "users")
public class User {
@Id
@SequenceGenerator(name="seq_user",sequenceName="oracle_users_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_user")
此外,我已经在oracle db中创建了序列
create sequence oracle_users_seq;
create sequence oracle_posts_seq;
当依靠更改运行我的代码时,我得到了:
Hibernate: insert into posts (author_id, creation_date, title, id, body) values (?, ?, ?, ?, ?)
2017-02-25 18:28:10.086 WARN 69700 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 2291, SQLState: 23000
2017-02-25 18:28:10.087 ERROR 69700 --- [nio-8080-exec-6] o.h.engine.jdbc.spi.SqlExceptionHelper : ORA-02291: integrity constraint (MEHMET.FK6XVN0811TKYO3NFJK2XVQX6NS) violated - parent key not found
2017-02-25 18:28:10.088 INFO 69700 --- [nio-8080-exec-6] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2017-02-25 18:28:10.116 ERROR 69700 --- [nio-8080-exec-6] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [MEHMET.FK6XVN0811TKYO3NFJK2XVQX6NS]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause
java.sql.SQLIntegrityConstraintViolationException: ORA-02291: integrity constraint (MEHMET.FK6XVN0811TKYO3NFJK2XVQX6NS) violated - parent key not found
如何在添加新条目时填充这两个表?
提前致谢!