由Hibernate生成的表列顺序

时间:2016-03-06 04:50:29

标签: java mysql hibernate

我有一个由Hibernate从实体类生成的表,该表的当前顺序是:

describe Context;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id           | int(11)      | NO   | PRI | NULL    |       |
| context      | varchar(200) | NO   |     | NULL    |       |
| publish_time | datetime     | NO   |     | NULL    |       |
| URL          | varchar(400) | NO   |     | NULL    |       |
| term_id      | int(11)      | NO   | MUL | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

然后将此语句配置为将数据加载到数据库:

LOAD DATA LOCAL INFILE 'context.txt' INTO TABLE Context FIELDS TERMINATED\ BY '\t'  LINES TERMINATED BY '\n'

但是,数据文件的5列的顺序" context.txt"与表中的列顺序不同。因此,当我在应用程序中启动加载时收到此错误消息:

     20:46:00,207 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] 
    (ServerService Thread Pool -- 68) HHH000388:  Unsuccessful: LOAD DATA 
LOCAL INFILE '/myproject/src/main/resources/context.txt' INTO TABLE Context FIELDS TERMINATED BY '\t'  LINES TERMINATED BY '\n'
            20:46:00,207 ERROR [org.hibernate.tool.hbm2ddl.SchemaExport] (ServerService Thread Pool -- 68) Cannot add or update a child row: a foreign key constraint fails (`BombDS`.`Context`, CONSTRAINT `FK_afatp1vwi13k5r0hftr04vctl` FOREIGN KEY (`term_id`) REFERENCES `Term` (`id`))

如果我无法更改文件中的顺序,有没有办法控制Hibernate生成的列顺序?

1 个答案:

答案 0 :(得分:0)

在LOAD语句中指定文件中列的顺序:

LOAD DATA LOCAL INFILE 'C:/Users/blah/Desktop/something.csv' INTO TABLE
test.test_table
FIELDS TERMINATED BY ',' 
optionally ENCLOSED by '"' 
(id, name, count)