0

在尝试使用 Springs 默认存储库持久化对象时,我无法弄清楚为什么某些东西不起作用。我使用休眠作为我的持久层和 Spring MVC 框架。我使用 JPA 而不是休眠映射文件。

我看到的错误如下:

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

我正在尝试使用 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;
    }
}

我在 question.type_id 列到 type.id 列上正确设置了外键约束。我可以让我的对象都在代码中正确表示,但是当我尝试使以下存储库持久保存对象时,我得到了上述错误。

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

所以要清楚。我可以创建一个新的问题对象并使用存储库从数据库中检索类型对象。我可以将 questions.type 设置为这个新类型对象,但是每当我尝试使用上面的存储库保留包含该类型的问题时,我都会收到该错误。我在调试器中检查了类型对象,它似乎没有任何问题。任何线索或建议都是有帮助的。这看起来像是某种转义字符序列,但我不太确定。

4

1 回答 1

1

所以我真的很傻。我缺少正确的注释,它没有取出类型 ID 来插入,而是尝试以某种方式插入这个 java 对象

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

上面的代码解决了这个问题

于 2012-10-19T09:27:37.017 回答