在我当前的更新函数的保存部分(我尝试了flush:true和insert:true)每次尝试都会执行插入。
我要更新的列是主键,但在mysql数据库中,我在所有外键使用中都有ON UPDATE CASCADE。
我还有什么需要在grails或其他地方注意的吗?
更新: 我有以下表格:
CREATE TABLE IF NOT EXISTS `Station` (
`Name` CHAR(50) NOT NULL ,
`Location_Designation` CHAR(50) NOT NULL ,
PRIMARY KEY (`Name`, `Location_Designation`) ,
INDEX `fk_Station_Location1_idx` (`Location_Designation` ASC) ,
CONSTRAINT `fk_Station_Location_LocatedIn`
FOREIGN KEY (`Location_Designation` )
REFERENCES `Location` (`Designation` )
ON DELETE NO ACTION
ON UPDATE CASCADE)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `Location` (
`Designation` CHAR(50) NOT NULL ,
PRIMARY KEY (`Designation`) )
ENGINE = InnoDB;
我的Grails:
stationInstance.properties = params
//Station = "S1", Location="test"
if (!stationInstance.save(flush: true)) { // }
更新2: 这是我的hibernate在update-command中所做的事情:
Hibernate: select station_.name, station_.Location_Designation as Location2_2_ from station station_ where station_.name=?
Hibernate: insert into station (Location_Designation, name) values (?, ?)
答案 0 :(得分:1)
如果您要更新主键,则可能无法在此处进行保存。您可能需要将HQL与executeUpdate一起使用,如下所示:
Station.executeUpdate("update Station s set s.name=? where s.name=?", name2, name1)