'how to send LocalDateTime.now() in java to the database (without sending a date from the frontend)?
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)
…
Solution 1:[1]
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
Solution 2:[2]
Wrong data types
You are incorrectly mixing the wrong data types.
See the MySQL 8 documentation.
See also Comments on the Question by Ole V.V.
Moment
The TIMESTAMP type in MySQL 8 is akin to the SQL standard type TIMESTAMP WITH TIME ZONE. Use this type to record a moment, a specific point on the timeline. This type represents date with time-of-day as seen “in UTC”, an offset from UTC of zero hours-minutes-seconds. The matching type in Java is OffsetDateTime, as specified by the JDBC spec.
Not a moment
The DATETIME type in MySQL 8 is akin to the SQL standard type TIMESTAMP WITHOUT TIME ZONE. This type represents a date with time of day, but lacks the context of a time zone or offset from UTC. This type cannot represent a moment. The matching Java type is LocalDateTime.
So your use of Java LocalDateTime with MySQL TIMESTAMP is a mismatch. Indeed, I cannot imagine a scenario where calling LocalDateTime.now is the right thing to do.
You have not explained your business domain. So I cannot advise you on which type is appropriate. But I can tell you to not mix the wrong types together.
Avoid legacy date-time types
Never use the classes java.sql.Timestamp, Calendar, or either Date. These are terribly flawed in design.
Those classes are now legacy, years ago supplanted by the modern java.time classes defined in JSR 310.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
