gpt4 book ai didi

mysql - com.mysql.jdbc.MysqlDataTruncation : Data truncation: Incorrect datetime value

转载 作者:行者123 更新时间:2023-11-29 11:49:40 26 4
gpt4 key购买 nike

我有一个应用程序(使用注释的 Spring 4 MVC+Hibernate 4+MySQL+Maven 集成示例),使用基于注释的配置将 Spring 与 Hibernate 集成。我有这张表

CREATE TABLE `t_device_event` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`device_id` int(11) unsigned NOT NULL,
`device_event_message` varchar(100) DEFAULT NULL,
`device_event_received` TIMESTAMP ,
`device_event_coordinates` point DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `device_id` (`device_id`),
CONSTRAINT `t_device_event_ibfk_1` FOREIGN KEY (`device_id`) REFERENCES `t_device` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这个域类:

@Entity
@Table(name="t_device_event")
public class DeviceEvent {

public class Coordinates {

private Double lat;

private Double lng;


public Coordinates(Double lat, Double lng) {
super();
this.lat = lat;
this.lng = lng;
}

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

public Double getLng() {
return lng;
}

public void setLng(Double lng) {
this.lng = lng;
}

}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@ManyToOne
@JoinColumn(name="device_id")
private Device device;


@Column(name = "device_event_received")
private Long received;


@Column(name = "device_event_message")
private String message;



//@Column(name = "device_event_coordinates")
//@Type(type = "org.hibernate.spatial.GeometryType")
@Transient
private Coordinates coordinates;


public Coordinates getCoordinates() {
return coordinates;
}

public void setCoordinates(Coordinates coordinates) {
this.coordinates = coordinates;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Device getDevice() {
return device;
}

public void setDevice(Device device) {
this.device = device;
}

public Long getReceived() {
return received;
}

public void setReceived(Long received) {
this.received = received;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public DeviceEvent(Device device) {
super();
this.device = device;
}
}

这在 Controller 中

deviceEvent.setReceived(new Date().getTime());
deviceEventService.save(deviceEvent);

但是我收到了这个错误:

    com.mysql.jdbc.MysqlDataTruncation: Data truncation: Incorrect datetime value: '1450290805238' for column 'device_event_received' at row 1
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4224)
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4158)
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2615)
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2776)
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2840)
com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2082)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2334)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2262)
com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2246)
org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96) org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)

当我设置 deviceEvent.setReceived(new Date()) 时: 我收到此错误:

the method setReceived(Long) in the type DeviceEvent is not applicable for the arguments (Date) –

最佳答案

您正在尝试将 Long 存储到 TIMESTAMP 列中。使用日期:

@Column(name = "device_event_received")
private Date received;
...

deviceEvent.setReceived(new Date());
deviceEventService.save(deviceEvent);

关于mysql - com.mysql.jdbc.MysqlDataTruncation : Data truncation: Incorrect datetime value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34319642/

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