0

I have an application (Spring 4 MVC+Hibernate 4+MySQL+Maven integration example using annotations) , integrating Spring with Hibernate using annotation based configuration . I have this table

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;

and this domain class:

@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;
    }
}

and this in the controller

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

but I got this error:

    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)

and when I set deviceEvent.setReceived(new Date()) : I got this error:

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

1 Answer 1

2

You are attempting to store Long into a TIMESTAMP column. Use Date:

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

deviceEvent.setReceived(new Date());
deviceEventService.save(deviceEvent); 
Sign up to request clarification or add additional context in comments.

2 Comments

The method setReceived(Long) in the type DeviceEvent is not applicable for the arguments (Date)
Don't set Long, set Date: deviceEvent.setReceived(new Date()).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.