Hibernate:字段列表中的未知列

时间:2016-06-07 11:36:58

标签: java mysql sql hibernate

我正在尝试像Hibernate User Guide (2.7.2)那样执行单向@OneTaMany关系,但是当我尝试将以下对象保存在MariaDB数据库中时:

Filter filter = new Filter("TLS_A320");
filter.addConstraint(new Constraint(ConstraintType.DEPARTURE, "TLS"));
filter.addConstraint(new Constraint(ConstraintType.AIRCRAFT, "A320"));
session.save(filter);

我遇到了这个例外:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Filter_idFilter' in 'field list'

以下是两个班级:

Filter.java

public class Filter {

    @Id
    @Column(name = "idFilter")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Constraint> constraintList;

    //more code
};

Constraint.java

public class Constraint {

    @Id
    @Column(name = "idConstraint")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(name = "type")
    @Enumerated(EnumType.ORDINAL)
    private ConstraintType constraintType;

    @Column(name = "value")
    private String value;

    //more code
}

以下是表格定义:

CREATE TABLE `Constraint` (
    idConstraint INT NOT NULL AUTO_INCREMENT,
    type INT NOT NULL,
    value VARCHAR(10) NOT NULL,

    PRIMARY KEY (idConstraint)
);

CREATE TABLE Filter (
    idFilter INT NOT NULL AUTO_INCREMENT,
    name VARCHAR(50) UNIQUE NOT NULL,

    PRIMARY KEY (idFilter)
);

CREATE TABLE Filter_Constraint (
    idFilter INT UNIQUE NOT NULL,
    idConstraint INT NOT NULL,

    CONSTRAINT fk_Filter_Constraint_Filter FOREIGN KEY (idFilter) REFERENCES Filter(idFilter),
    CONSTRAINT fk_Filter_Constraint_Constraint FOREIGN KEY (idConstraint) REFERENCES `Constraint`(idConstraint)
);

在我看来,Filter和Constraint插入很好,插入Filter_Constraint表时会发生异常:

DEBUG org.hibernate.SQL - insert into Filter (name) values (?)
DEBUG org.hibernate.id.IdentifierGeneratorHelper - Natively generated identity: 4
DEBUG org.hibernate.SQL - insert into `Constraint` (type, value) values (?, ?)
DEBUG org.hibernate.id.IdentifierGeneratorHelper - Natively generated identity: 5
DEBUG org.hibernate.SQL - insert into `Constraint` (type, value) values (?, ?)
DEBUG org.hibernate.id.IdentifierGeneratorHelper - Natively generated identity: 6
DEBUG org.hibernate.SQL - insert into `Filter_Constraint` (Filter_idFilter, `constraintList_idConstraint`) values (?, ?)
DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - could not execute statement [n/a]
DEBUG com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'Filter_idFilter' in 'field list'

我对Hibernate很陌生,我无法弄清楚我做错了什么。

1 个答案:

答案 0 :(得分:2)

您没有在constraintList上定义任何连接列或反向连接列,因此Hibernate会自动生成最可能导致名称为Filter_idFilter的列名。

实体名称将成为名称的一部分,因为Constraints 可以具有相同名称的ID字段,即idFilter。当然知道并非如此,但Hibernate没有(或者至少列名生成代码不是那么复杂)因此它必须假设可能就是这样。

连接表名称也是如此,因此您可能希望@JoinTable使用constraintList上的连接列和反连接列的定义。但是,由于关系为OneToMany,为什么不向Constraint添加反向引用,将过滤器的ID添加到表Constraint并删除完全连接表?