Я новичок в спящем режиме и данных JPA. Я пытаюсь выполнить вставку в мою таблицу, но в запросе гибернации есть столбцы, которых нет в моей таблице, поэтому он выдаст ошибку. На самом деле сначала, когда я запускаю свой код, hibernate добавляет эти дополнительные столбцы в мою таблицу, а затем я изменяю значение spring.jpa.hibernate.ddl-auto на none в приложении. .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 создал или исправить эту проблему любым другим способом, как я могу контролировать или управлять запросами, созданными с помощью Hibernate ?? ?
Я также должен упомянуть, что я использую почтальона для отправки данных ипроверьте мой код.