Home > Blockchain >  how to send LocalDateTime.now() in java to the database (without sending a date from the frontend)?
how to send LocalDateTime.now() in java to the database (without sending a date from the frontend)?

Time:04-15

the table contains this:

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class SourceContentMappingPK implements Serializable {
    @Column(name = "hr_code")
    private String hrCode;

    @Column(name = "muse_id")
    private String museId;

    @Column(name = "source_type")
    private String sourceType;

    @Column(name = "cts")
    private LocalDateTime cts;
}

This is the model:

@Getter
@Setter
@NoArgsConstructor
public class SourceContentMappingDTO {
    private String hrCode;
    private String museId;
    private String sourceType;
    private String masterHotelId;
    private LocalDateTime cts;

    public SourceContentMappingDTO(String hrCode, String museId, String sourceType, String masterHotelId, LocalDateTime cts) {
        this.hrCode = hrCode;
        this.museId = museId;
        this.sourceType = sourceType;
        this.masterHotelId = masterHotelId;
        this.cts = cts;
    }
}

When I send a request through postman, the current date and time are written to the database. When I make the same request through the page, everything except the date is written to the database. It is necessary to enter the date without sending it from the frontend.

 @Override
    @Transactional
    public void updateImageSources(HotelMasterListSubItemDTO dto) {
        dto.setContentSources(Arrays.asList("IMAGES"));

        List<SourceContentMapping> sourceContentMappings = entityManager.createNativeQuery("select * from SOURCE_CONTENT_MAPPING where master_hotel_id = :masterHotelId", SourceContentMapping.class)
                .setParameter("masterHotelId", dto.getMasterHotelId())
                .setHint(QueryHints.READ_ONLY, true)
                .getResultList();

        for(String contentSourceType : dto.getContentSources()) {
            boolean contentSourceTypeExists = false;
            for(SourceContentMapping sourceContentMapping : sourceContentMappings) {
                if(contentSourceType.equals(sourceContentMapping.getId().getSourceType())) {
                    contentSourceTypeExists = true;

                    entityManager.createNativeQuery("update SOURCE_CONTENT_MAPPING set muse_id = :museId, hr_code = :hrCode, cts = :cts where master_hotel_id = :masterHotelId and source_type = :sourceType")
                            .setParameter("museId", dto.getMuseId())
                            .setParameter("hrCode", dto.getHrCode())
                            .setParameter("masterHotelId", dto.getMasterHotelId())
                            .setParameter("sourceType", contentSourceType)
                            .setParameter("cts", LocalDateTime.now())
                            .executeUpdate();
                }
            }

            if(!contentSourceTypeExists) {
                entityManager.createNativeQuery("insert into SOURCE_CONTENT_MAPPING (hr_code, muse_id, source_type, master_hotel_id, cts) values (?, ?, ?, ?, ?)")
                        .setParameter(1, dto.getHrCode())
                        .setParameter(2, dto.getMuseId())
                        .setParameter(3, contentSourceType)
                        .setParameter(4, dto.getMasterHotelId())
                        .setParameter(5, LocalDateTime.now())
                        .executeUpdate();
            }
        }
    }

I tried adding .setParameter ("cts", LocalDateTime.now ()) and .setParameter (5, LocalDateTime.now ()) but like I said only through postman it works but not through GUI. I need to make the parameter optional, nullable e.g.

Using MySQL. Table contains:

museId (varchar 50, notNull),
  hrCode (varchar 50, notNull),
  source_type (varchar 50, notNull),
  masterHotelId (varchar 100),
  cts (timestamp)
  …

CodePudding user response:

To get timestamp value and put it to sql you can simply use #Timestamp.valueOf() method and put your LocalDateTime to the method`s param. To get time you can use = new Timestamp(System.currentTimeMillis()) on backend side

CodePudding user response:

We can also use java.time.LocalDateTime to get an instance of LocalDate:

LocalDateTime localDateTime = LocalDateTime.now();
LocalDate localDate = localDateTime.toLocalDate();
  • Related