问题
I can't make MySQL to update a TIMESTAMP field marked as CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP when the update query is made by Hibernate.
According to this answer, and this comment, MySQL should handle tables with more than one column defaulting to CURRENT_TIMESTAMP
. My problem is that, when updating the entities with Hibernate, that seems to work for one table, but not for another in the same schema. Also, it works fine for all tables when I run the updates directly against the DB (without Hibernate).
CREATE TABLE `homes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userId_fk` int(11) NOT NULL,
`type_fk` varchar(45) NOT NULL,
`summary` varchar(255) DEFAULT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_homes_users` (`userId_fk`),
KEY `fk_homes_homeTypes_idx` (`type_fk`),
CONSTRAINT `fk_homes_homeTypes` FOREIGN KEY (`type_fk`) REFERENCES `homeTypes` (`type`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_homes_users` FOREIGN KEY (`userId_fk`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
CREATE TABLE `addresses` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`street` varchar(45) DEFAULT NULL,
`city` varchar(45) NOT NULL,
`province` varchar(45) DEFAULT NULL,
`postcode` varchar(45) NOT NULL,
`country` varchar(45) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `fk_addresses_countries_idx` (`country`),
CONSTRAINT `fk_addresses_countries` FOREIGN KEY (`country`) REFERENCES `countries` (`iso3166_alpha3`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=25 DEFAULT CHARSET=utf8;
The second table, addresses, has its updated column correctly changed with each update. The first one doesn't. I'm using MySQL 5.7 and Hibernate 4.3.11.
My entities:
@Entity
@Table(name = "homes")
public class Home {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
@Column(name = "userId_fk", nullable = false)
private Integer userId;
@OneToOne(cascade = ALL)
@JoinTable(name = "homes_addresses", joinColumns = @JoinColumn(name = "homeId_fk"),
inverseJoinColumns = @JoinColumn(name = "addressId_fk"))
private Address address;
@Enumerated(EnumType.STRING)
@Column(name = "type_fk") // TODO nullable = false ?
private HomeType type;
private String summary;
private LocalDateTime created;
private LocalDateTime updated;
}
@Entity
@Table(name = "addresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String street;
@NotNull
private String city;
private String province;
@NotNull
private String postcode;
@NotNull
private String country;
private LocalDateTime created;
private LocalDateTime updated;
}
An example of query that does NOT trigger the ON UPDATE:
Hibernate:
/* update
xxx.xxx.Home */ update
homes
set
created=?,
summary=?,
type_fk=?,
updated=?,
userId_fk=?,
videoLink=?
where
id=?
来源:https://stackoverflow.com/questions/35196165/mysqls-on-update-now-working-for-one-table-and-not-for-another-when-updated-b