gpt4 book ai didi

java - Hibernate OneToMany/ManyToOne 导致 SQLGrammarException

转载 作者:行者123 更新时间:2023-11-29 06:27:06 25 4
gpt4 key购买 nike

我在两个表之间有一个 OneToMany/ManyToOne 关系。表 Partition 中有一个外键引用了 Eventid。当我调用 event.getPartitions() 时,它应该在一个 Event 中返回一个包含所有 Partitions 的列表,我得到以下异常:

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.

dialect 设置为 org.hibernate.dialect.MySQL5Dialectdriver classcom.mysql.jdbc。驱动程序

获取分区:

@XmlTransient
@OneToMany(mappedBy = "event")
public Collection<Partition> getPartitions()
{
return partitions;
}

获取事件:

@XmlTransient
@ManyToOne
@JoinColumn(name = "event_id", referencedColumnName = "id", nullable = false)
public Event getEvent()
{
return event;
}

事件:

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=?

最佳答案

分区reserved word在 MySQL 中,将表名更改为其他名称(实体名称可以保持不变,只需使用 @Table 注解来配置新的表名)。

关于java - Hibernate OneToMany/ManyToOne 导致 SQLGrammarException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30321026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com