1

在我当前的更新函数的保存部分(我尝试了flush:true和insert:true)每次我尝试都会执行一个插入。

我要更新的列是主键,但在 mysql-database 中,我在所有外键使用中都有 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;

我的圣杯:

stationInstance.properties = params
//Station = "S1", Location="test"

if (!stationInstance.save(flush: true)) { // }

更新2:这是我的休眠在更新命令中所做的:

Hibernate: select station_.name, station_.Location_Designation as Location2_2_ from station station_ where station_.name=?
Hibernate: insert into station (Location_Designation, name) values (?, ?)
4

1 回答 1

1

如果您要更新主键,您可能无法在此处保存。您可能需要像这样将 HQL 与 executeUpdate 一起使用:

Station.executeUpdate("update Station s set s.name=? where s.name=?", name2, name1)
于 2013-09-12T16:43:03.823 回答