【问题标题】:wrong query generate by hibernate休眠生成的错误查询
【发布时间】: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_idcreation_datecomment_countdtype 字段,但我没有'不知道为什么休眠将它们添加到查询中。

有没有办法改变hibernate创建的查询或以任何其他方式解决这个问题,我如何控制或管理hibernate创建的查询???

我还应该提到我使用邮递员发送数据并检查我的代码。

【问题讨论】:

  • 我在您的 Content 实体中看到了 @Column(name = "content_id"),请检查您的 @Column 映射是否具有与您的表对应的名称。
  • 对不起,该字段在数据库中,我在不存在的字段之间错误地提到了它。

标签: java mysql spring hibernate


【解决方案1】:

经过大量的搜索和工作,我发现休眠表列的命名约定是以下划线分隔单词的方式,这就是我在休眠生成的查询中看到这些列的原因。因此,如果您的类中有一个变量,例如 creationDate hibernate 尝试转换为 creation_date 所以当我在此方法中更改所有列的名称时,问题就解决了。另外,dtype列是一种特殊的列,当许多类使用同一个表插入数据时,它会由hibernate创建,这是因为为了区分哪个类在表中插入记录,而hibernate提供它的值与那个名称类。

【讨论】:

    【解决方案2】:

    但是您的 Content 实体中确实有 content_id 和 creation_date ,问题实体从该实体扩展

    【讨论】:

    • 我现在有creationDate 但没有creation_date 查询错误说我没有正确的creation_date 字段,因为在数据库中我有creationDate 字段,它与created_date 不同。
    • 好的。您应该尝试删除该表以便再次生成它。
    猜你喜欢
    • 2016-08-17
    • 1970-01-01
    • 2016-06-21
    • 2011-08-10
    • 2019-11-15
    • 2016-07-12
    • 2016-05-20
    • 1970-01-01
    • 2017-12-06
    相关资源
    最近更新 更多