2015-05-19 63 views
1

Im在兩個表之間有一個OneToMany/ManyToOne關係。在表Partition中有一個外鍵Eventid。當我打電話event.getPartitions()應在一個Event返回包含所有Partitions名單,我得到以下異常:Hibernate OneToMany/ManyToOne結果在SQLGrammarException中

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.Driver

getPartitions:

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

getEvent:

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

下面的查詢得到exceuted,這也看行對我說:

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

回答

2

Partition是在MySQL reserved word,更改表名稱爲其他內容(實體名稱可以保持不變,只是用@Table註解來配置新表名)。

相關問題