【发布时间】:2019-10-10 23:31:16
【问题描述】:
我是休眠和数据 JPA 的新手。我尝试在我的表中进行插入,但休眠查询中有一些列在我的表中不存在,因此它会引发错误。实际上,起初当我运行我的代码时,休眠将这些额外的列添加到我的表中,然后我将 spring.jpa.hibernate.ddl-auto 值更改为 none 在 >application.properties,但是现在当我从表中删除这些额外的列并尝试插入新记录时,我看到这些列在插入方法中。
我的实体类
@Entity
public class Content {
@Id
@NotNull
@GeneratedValue
Integer id;
//this can be null if it is a question
@Column(name = "content_id")
Integer content_id;
@NotBlank @NotNull
@Column(name = "body")
String body;
@Column(name = "creationDate")
Timestamp creationDate;
@NotNull
@Column(name = "user_id")
Integer user_id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getContent_id() {
return content_id;
}
public void setContent_id(Integer content_id) {
this.content_id = content_id;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public Timestamp getCreationDate() {
return creationDate;
}
public void setCreationDate(Timestamp creationDate) {
this.creationDate = creationDate;
}
public int getUser_id() {
return user_id;
}
public void setUser_id(Integer user_id) {
this.user_id = user_id;
}
}
我的问题类扩展了内容
@Entity
public class Question extends Content {
@NotNull @NotBlank
@Column(name = "subject")
String subject;
@NotNull @NotBlank
@Column(name = "tags")
String tags;
@NotNull
@Column(name = "contentType")
final Integer contentType_id = 1;
@Column(name = "commentCount")
Integer commentCount;
public Question(@Valid @JsonProperty("subject") String subject,
@Valid @JsonProperty("tags") String tags,
@Valid @JsonProperty("body") String body) {
this.subject = subject;
this.tags = tags;
this.body = body;
}
public Integer getContentType_id() {
return contentType_id;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTags() {
return tags;
}
public void setTags(String tags) {
this.tags = tags;
}
public Integer getCommentCount() {
return commentCount;
}
public void setCommentCount(Integer commentCount) {
this.commentCount = commentCount;
}
}
服务类
@Service
public class QuestionService {
@Autowired
QuestionRepository questionRepository;
public QuestionService(QuestionRepository questionRepository) {
this.questionRepository = questionRepository;
}
public Question postQuestion(Question question){
return questionRepository.save(question);
}
}
控制器
@RequestMapping("easy4lazy/questions")
@RestController
public class QuestionController {
private final QuestionService questionService;
private final int contetnType = 1;
@Autowired
public QuestionController(QuestionService questionService) {
this.questionService = questionService;
}
@PostMapping(path = "/postQuestion" )
public Question postQuestion(@RequestBody Question q){
q.setContent_id(contetnType);
return questionService.postQuestion(q);
}
}
存储库
import com.easy4lazy.proj.model.Question;
import org.springframework.data.repository.CrudRepository;
public interface QuestionRepository extends CrudRepository<Question, Integer> {
}
错误代码
Hibernate: insert into content (body, content_id, creation_date, user_id, comment_count, content_type, subject, tags, dtype, id) values (?, ?, ?, ?, ?, ?, ?, ?,'Question', ?)
2019-10-10 18:11:36.513 WARN 11960 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 1054, SQLState: 42S22
2019-10-10 18:11:36.515 ERROR 11960 --- [nio-8080-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper : Unknown column 'creation_date' in 'field list'
2019-10-10 18:11:36.520 ERROR 11960 --- [nio-8080-exec-3] o.h.i.ExceptionMapperStandardImpl : HHH000346: Error during managed flush [org.hibernate.exception.SQLGrammarException: could not execute statement]
2019-10-10 18:11:36.547 ERROR 11960 --- [nio-8080-exec-3] 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.InvalidDataAccessResourceUsageException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause
我的表中没有 content_id、creation_date、comment_count 和 dtype 字段,但我没有'不知道为什么休眠将它们添加到查询中。
有没有办法改变hibernate创建的查询或以任何其他方式解决这个问题,我如何控制或管理hibernate创建的查询???
我还应该提到我使用邮递员发送数据并检查我的代码。
【问题讨论】:
-
我在您的 Content 实体中看到了
@Column(name = "content_id"),请检查您的 @Column 映射是否具有与您的表对应的名称。 -
对不起,该字段在数据库中,我在不存在的字段之间错误地提到了它。
标签: java mysql spring hibernate