休眠:字段列表中的未知列 [英] Hibernate : Unknown column in field list

查看:176
本文介绍了休眠:字段列表中的未知列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图按照但是当我尝试将以下对象保存在MariaDB数据库中时: p>

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

我得到这个例外:

<$ p $ com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:'字段列表'中的未知列'Filter_idFilter'

以下是两个类:

Filter.java

  public class Filter {

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

@Column(name =name)
私有字符串名称;

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

//更多代码
};

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;

//更多代码
}

这里是表定义:
$ b $ pre $ 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  - 插入到过滤器(名称)值(?)
DEBUG组织。 hibernate.id.IdentifierGeneratorHelper - 本地生成的标识:4
DEBUG org.hibernate.SQL - 插入到`Constraint`(类型,值)值(?,?)
DEBUG org.hibernate.id.IdentifierGeneratorHelper - 本地生成的标识:5
DEBUG org.hibernate.SQL - 插入到`Constraint`(类型,值)值(?,?)
DEBUG org.hibernate.id.IdentifierGeneratorHelper - 本地生成的标识: 6
DEBUG org.hibernate.SQL - 插入'Filter_Constraint`(Filter_idFilter,`constraintList_idConstraint`)值(?,?)
DEBUG org.hibernate.engine.jdbc.spi.SqlExceptionHelper - 无法执行语句[n / a]
DEBUG com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:未知列'Filter_idFilter'in'field list'

我是Hibernate的新手,我不能找出我做错了什么。

解决方案

您没有在 constraintList ,因此Hibernate会自动生成最可能导致名称 Filter_idFilter 的列名。

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



连接表名称也是如此,所以您可能想要使用 @JoinTable ,同时在 constraintList 上定义连接列和反向连接列。但是,由于关系是 OneToMany ,为什么不添加对 Constraint 的反向引用,添加过滤器的id到表约束并且完全摆脱连接表?


I'am trying to do an unidirectional @OneTaMany relationship like in the Hibernate User Guide (2.7.2) but when I try to save the following object in a MariaDB Database:

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

I got this exception :

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

Here are the 2 classes:

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
}

And here is the tables definition :

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)
);

It seems to me that that Filter and Constraint insertions are fine and the exception happens when inserting in the Filter_Constraint table :

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'

I'am pretty new to Hibernate and I just can't figure out what I did wrong.

解决方案

You didn't define any join column or inverse join column on constraintList, hence Hibernate will autogenerate column names which most probably results in the name Filter_idFilter.

The entity name will be part of the name since Constraints could have an id field with the same name, i.e. idFilter. Of course you know that isn't the case but Hibernate doesn't (or at least the column name generation code isn't that complex) and hence it has to assume that could be the case.

The same is true for join table names so you'll probably want to use @JoinTable along with the definition of join columns and inverse join columns on constraintList. However, since the relation is OneToMany anyways, why don't you add a back reference to Constraint, add the filter's id to table Constraint and get rid of the join table entirely?

这篇关于休眠:字段列表中的未知列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆