简体   繁体   English

hibernate 生成的错误查询

[英]wrong query generate by hibernate

I am new to hibernate and Data JPA.我是 hibernate 和数据 JPA 的新手。 I try to do an insert into my table but the hibernate query has some columns in it that not exist in my table so it will throw an error.我尝试在我的表中插入,但 hibernate 查询中有一些列在我的表中不存在,因此它会引发错误。 Actually at first when I run my code the hibernate add these extra columns to my table and then I change spring.jpa.hibernate.ddl-auto value to none in application.properties , but now when I delete those extra columns from my table and try to insert a new record I see those columns are in insert method. Actually at first when I run my code the hibernate add these extra columns to my table and then I change spring.jpa.hibernate.ddl-auto value to none in application.properties , but now when I delete those extra columns from my table and尝试插入一条新记录,我看到这些列在插入方法中。

My Entity classes我的实体类

@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;
    }
}

my question class extends the content我的问题 class 扩展了内容

@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 class服务 class

@Service
public class QuestionService {

    @Autowired

    QuestionRepository questionRepository;
    public QuestionService(QuestionRepository questionRepository) {
        this.questionRepository = questionRepository;
    }

    public Question postQuestion(Question question){
        return questionRepository.save(question);
    }
}

Controller Controller

@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);
    }
}

Repository存储库

import com.easy4lazy.proj.model.Question;
import org.springframework.data.repository.CrudRepository;

public interface QuestionRepository extends CrudRepository<Question, Integer> {
}

Error code错误代码

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

I don't have content_id , creation_date , comment_count and dtype fields in my table and i don't know why hibernate add them into the query.我的表中没有content_idcreation_datecomment_countdtype字段,我不知道为什么 hibernate 将它们添加到查询中。

is there any way to change the query that hibernate created or fix this problem in any other way, how can I control or manage queries create by hibernate???有什么方法可以更改 hibernate 创建的查询或以任何其他方式解决此问题,我如何控制或管理 hibernate 创建的查询?

I also should mention that I use the postman to send data and check my code.我还应该提到我使用 postman 发送数据并检查我的代码。

But you do have content_id and creation_date in your Content entity which the Question entity extends from但是您的 Content 实体中确实有 content_id 和 creation_date ,而 Question 实体从该实体扩展而来

After lots of searching and working, I found that hibernate naming convention for table columns is in a way that it separate words by an underscore and that was the reason that I saw those columns inside the query generated by hibernate.经过大量搜索和工作,我发现表列的 hibernate 命名约定是用下划线分隔单词,这就是我在 hibernate 生成的查询中看到这些列的原因。 So if you have a variable inside your class like creationDate hibernate try to converted to creation_date so when I change all my column's name in this method problem solved.因此,如果您的 class 中有一个变量,例如creationDate hibernate ,请尝试转换为creation_date ,这样当我在此方法中更改所有列的名称时,问题就解决了。 Also, the dtype column is a special kind of column that will create by hibernate when many classes use the same table to insert data, it is because to distinguish which class insert the record inside the table and hibernate provide its value with the name of that class.另外,dtype列是hibernate创建的一种特殊列,当多个类使用同一张表插入数据时,这是因为要区分哪个class在表中插入记录,ZCB1F008EEBF5012C4EF9A2C36E574的值提供其值class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM