Java 休眠OneToMany/ManyTone会导致SQLGrammarException
我在两个表之间有一个Java 休眠OneToMany/ManyTone会导致SQLGrammarException,java,mysql,hibernate,Java,Mysql,Hibernate,我在两个表之间有一个OneToMany/manytone关系。表分区中有一个外键引用事件的id。调用event.getPartitions(),它将返回一个列表,其中包含一个事件中的所有分区,我得到以下异常: Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet. Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorE
OneToMany
/manytone
关系。表分区中有一个外键
引用事件的id
。调用event.getPartitions()
,它将返回一个列表,其中包含一个事件中的所有分区
,我得到以下异常:
Caused by: org.hibernate.exception.SQLGrammarException: could not extract ResultSet.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax.
方言
设置为org.hibernate.dialogue.mysql5dialogue
,驱动程序类
为com.mysql.jdbc.driver
getPartitions:
@XmlTransient
@OneToMany(mappedBy = "event")
public Collection<Partition> getPartitions()
{
return partitions;
}
活动:
DROP TABLE IF EXISTS `event`;
CREATE TABLE `event` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(1024) NOT NULL,
`description` varchar(1024) DEFAULT NULL,
`img` varchar(1024) DEFAULT NULL,
`date` date DEFAULT NULL,
`location` varchar(1024) DEFAULT NULL,
`information` varchar(1024) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
分区:
DROP TABLE IF EXISTS `partition`;
CREATE TABLE `partition` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`event_id` int(11) NOT NULL,
`title` varchar(1024) DEFAULT NULL,
`description` varchar(1024) DEFAULT NULL,
`active_timestamp` datetime DEFAULT NULL,
`position` int(11) NOT NULL,
PRIMARY KEY (`id`),
KEY `event_id` (`event_id`),
CONSTRAINT `event_partition` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;
以下查询被忽略,在我看来也是正常的:
10:44:04,708 INFO [stdout] (default task-4) Hibernate: select event0_.id as id1_1_0_, event0_.date as date2_1_0_, event0_.description as descript3_1_0_, event0_.img as img4_1_0_, event0_.information as informat5_1_0_, event0_.location as location6_1_0_, event0_.title as title7_1_0_ from Event event0_ where event0_.id=?
10:44:04,818 INFO [stdout] (default task-4) Hibernate: select partitions0_.event_id as event_id6_1_0_, partitions0_.id as id1_2_0_, partitions0_.id as id1_2_1_, partitions0_.active_timestamp as active_t2_2_1_, partitions0_.description as descript3_2_1_, partitions0_.event_id as event_id6_2_1_, partitions0_.position as position4_2_1_, partitions0_.title as title5_2_1_ from Partition partitions0_ where partitions0_.event_id=?
Partition
在MySQL中,请将表名更改为其他名称(实体名可以保持不变,只需使用@table
注释来配置新表名)
10:44:04,708 INFO [stdout] (default task-4) Hibernate: select event0_.id as id1_1_0_, event0_.date as date2_1_0_, event0_.description as descript3_1_0_, event0_.img as img4_1_0_, event0_.information as informat5_1_0_, event0_.location as location6_1_0_, event0_.title as title7_1_0_ from Event event0_ where event0_.id=?
10:44:04,818 INFO [stdout] (default task-4) Hibernate: select partitions0_.event_id as event_id6_1_0_, partitions0_.id as id1_2_0_, partitions0_.id as id1_2_1_, partitions0_.active_timestamp as active_t2_2_1_, partitions0_.description as descript3_2_1_, partitions0_.event_id as event_id6_2_1_, partitions0_.position as position4_2_1_, partitions0_.title as title5_2_1_ from Partition partitions0_ where partitions0_.event_id=?