com.mysql.jdbc.MysqlDataTruncation:数据截断:日期时间值不正确

时间:2015-12-16 18:38:08

标签: mysql hibernate spring-mvc datetime jpa

我有一个应用程序(使用注释的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;
    }
}

这在控制器中

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) –

1 个答案:

答案 0 :(得分:1)

您正试图将Long存储到TIMESTAMP列中。使用Date

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

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