简体   繁体   English

SQL错误:1366不正确的整数值Spring MVC休眠

[英]SQL Error: 1366 Incorrect integer value Spring MVC Hibernate

I am having some trouble figuring out why something is not working when trying to persist an object using Springs default Repositories. 我在弄清楚为什么在尝试使用Springs默认存储库持久化对象时某些东西无法正常工作时遇到了一些麻烦。 I am using hibernate as my persistence layer and Spring MVC framework. 我正在使用休眠作为我的持久层和Spring MVC框架。 I use JPA instead of hibernate mapping files. 我使用JPA而不是休眠映射文件。

The error I am seeing is the following: 我看到的错误如下:

WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 1366, SQLState: HY000
ERROR: org.hibernate.util.JDBCExceptionReporter - Incorrect integer value: '\xAC\xED\x00\x05sr\x00!com.kwhours.butternut.domain.Type\xCF;\xBF\xEF\x8A\x82\x87\xF1\x02\x00\x0DZ\x00\x06isLeafI\x00\x05levelL\' for column 'type_id' at row 1

I am trying to persist a domain object with JPA annotations the relevant part of that code is: 我试图用JPA注释持久化一个域对象,该代码的相关部分是:

@Entity
@Table(name = "question")
public class Question implements java.io.Serializable {
    private Type type;

    @Column(name = "type_id")
    public Type getType() {
        return this.type;
    }

    public void setType(Type type) {
        this.type = type;
    }
}

I have a foreign key constraint properly set up on the question.type_id column to type.id column. 我在question.type_id列上将外键约束正确设置为type.id列。 I am able to have my objects all properly represented in code but when I try to make the following repository persist the object, I get the afore mentioned error. 我能够用代码正确地表示所有对象,但是当我尝试使以下存储库保留该对象时,出现上述错误。

public interface QuestionRepository extends CrudRepository<Question, Long> {
    public List<Question> findByDatasetInstanceId(Long datasetInstanceId);
}

So to be clear. 所以要明确。 I can make a new question object and retrieve a type object from the db using a repository. 我可以创建一个新的问题对象,并使用存储库从数据库中检索类型对象。 I can set questions.type to this new type object but I get that error whenever I try to persist the question which contains that type using the repository above. 我可以将questions.type设置为这个新类型的对象,但是每当我尝试使用上面的存储库保留包含该类型的问题时,都会收到该错误。 I checked out the type object in the debugger and there seems to be nothing amiss with it. 我在调试器中签出了类型对象,似乎没有任何不妥之处。 Any clues or suggestions are helpful. 任何线索或建议都是有帮助的。 That looks like some kind of escape character sequence but I am not really sure. 这看起来像某种转义字符序列,但我不确定。

So I was really silly about this. 所以我真的很傻。 I am missing the proper annotations and it wasn't taking the type id out to insert but rather tryign to somehow insert this java object 我缺少适当的注释,并且没有取出类型ID插入,而是尝试以某种方式插入此java对象

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "type_id")
public Type getType() {
    return this.type;
}

that code above solved the issue 上面的代码解决了这个问题

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

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