我'試圖做一個單向@OneTaMany關係像Hibernate User Guide (2.7.2)但是當我嘗試保存下列對象在數據庫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'
這裏有2類:
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表中:
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的新手,我無法弄清楚我做錯了什麼。
因此,用戶指南中給出的例子不起作用? – bl4ckout
@ bl4ckout這些例子很可能工作,但你沒有正確地適應它們。作爲一個例子,在Person_Phone表中有一列Person_id,因爲實體Person中的id列被定義爲id。 – Thomas
我認爲這是電話'Phone_id'也是我的錯。我在約束表和類中添加了一個引用,並且@JoinColumn和它現在都可以工作。非常感謝。 – bl4ckout