简体   繁体   中英

wrong query generate by hibernate

I am new to hibernate and Data 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. 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.

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

@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

@Service
public class QuestionService {

    @Autowired

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

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

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.

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???

I also should mention that I use the postman to send data and check my code.

But you do have content_id and creation_date in your Content entity which the Question entity extends from

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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