This is the Date value that client is sending
createdOn : "2017-09-14T13:23:44"
In Controller client value is mapping to the Dto, AttachmentsDto
public class AttachmentsDto implements Serializable {
#JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss", timezone="Asia/Muscat")
private Date createdOn;
}
Below is the Attachment Entity
public class Attachments implements Serializable {
private static final long serialVersionUID = 1L;
#Temporal(TemporalType.TIMESTAMP)
#Column(name="CREATED_ON", insertable= true, updatable=false)
private Date createdOn;
}
Service have written code to save the entire dto.
mapped dto to entity and called repo save here.
attachmentsRepo.save(mapper.map(AttachmentsDto, Attachments.class)), AttachmentsDto.class);
Here its saving only date there is no timestamp.
Db createdOn = 9/14/2017
How can I store date and timestamp ?
Related
I have an Spring Boot REST API that update a database table. So I have an Entity that contains INSTANT fields.
#ApiModel(description = "Table.")
#Entity
#Table(name = "T_VIR")
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Virement implements Serializable {
private static final long serialVersionUID = 1L;
#Id
#Column(name = "vir_id", nullable = false)
#GeneratedValue(generator = "sequenceVir")
#GenericGenerator(name = "sequenceVir", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
#org.hibernate.annotations.Parameter(name = "sequence_name", value = "S_VIR")
})
private Long id;
#NotNull
#Column(name = "vir_date", nullable = false)
private Instant date;
So I have a DTO that corresponding to my entity with INSTANT date
public class VirDTO implements Serializable {
private Long id;
private Instant date;
When I generate swagger Api Docs, VirDTO date's fieds are typed with "Date"
public class VirDTO implements Serializable {
private static final long serialVersionUID = 1L;
#JsonProperty("date")
private Date date = null;
#JsonProperty("id")
private Long id = null;
So, in my code, when I use virDTO and send it to my API, value is altered (years is on 5 digits, month and days are not the same)
Data send to Rest API :
{
date: Sun Jun 12 17:57:15 CEST 2022
id: 122
}
Result of data at the begin of API :
id=122,date=+54416-06-05T18:15:43Z
Do you know why and how we can correct this ?
I know there has been multiple questions on bidirectional relations using spring jpa in the past but my case is a little bit different because i am using 3 entities with 2 relationships to implement a medical system
I have 3 entities : doctor/patient/appointment
here is the code for the 3 entities
please note all setters , getters and constructors implemented but ommited here for clarity
Patient class
#Entity
public class resPatient {
#Id
#GeneratedValue( strategy= GenerationType.IDENTITY )
private long code;
private String name;
private String gender;
private String email;
private String mobile;
private int age;
private String notes;
#OneToMany(mappedBy = "patient")
List<resPackageMembership> memberships;
#OneToMany(mappedBy = "patient")
List<resAppointment> appointments;
#OneToMany(fetch = FetchType.LAZY,mappedBy = "patient")
List<resMedImage> medImages;
Doctor class
#Entity
public class resDoctor {
#Id
#GeneratedValue( strategy= GenerationType.IDENTITY )
private long code;
private String name;
private String mobile;
private String email;
private String gender;
private int age;
private String speciality;
#OneToMany(mappedBy = "doctor")
List<resAppointment> appointments;
Appointment class
#Entity
public class resAppointment {
#Id
#GeneratedValue( strategy= GenerationType.IDENTITY )
private long code;
private String speciality;
#Basic
#Temporal(TemporalType.TIMESTAMP)
private Date dateCreated;
#Basic
#Temporal(TemporalType.TIMESTAMP)
private Date dateToVisit;
private String status;
private String notes;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "doctorCode")
private resDoctor doctor;
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "patientCode")
private resPatient patient;
the way my medical system should work is that when i get a patient using my restful controller i want all the patient data including his appointments but this leads to an infinite loop as the appointment has the doctor which also has appointments and so on.
i cannot user #JSONIGNORE as there are 2 relationships i want to get the patient with his appointments which should have the doctor without the appointments array and should not have any patient data as i already am in the patient object
As a general best-practice, it's recommended to separate the entities from the data transfer objects used for the rest controllers. With DTO's in place, you have more control on which data to include and serialize within them to avoid the circlular references.
If you like check out https://bootify.io, it generates the DTOs from your database schema, but the custom endpoint you still need to define/build.
I develop an annotation processor called beanknife recently, it support generate DTO from any class. You need config by annotation. But you don't need change the original class. This library support configuring on a separate class. Of course you can choose which property you want and which you not need. And you can add new property by the static method in the config class. For your question:
// this will generate a DTO class named "resPatientView".
// You can change this name using genName attribute.
#ViewOf(value=resPatient.class, includePattern = ".*")
public class PatientViewConfigure {
// here tell the processor to automatically convert the property appointments from List<resAppointment> to List<resAppointmentWithoutPatient>.
// resAppointmentWithoutPatient is the generated class configured at the following.
// Note, although at this moment it not exists and your idea think it is an error.
// this code really can be compiled, and after compiled, all will ok.
#OverrideViewProperty("appointments")
private List<resAppointmentWithoutPatient> appointments;
}
// here generated a class named resAppointmentWithoutPatient whick has all properties of resAppointment except patient
#ViewOf(value=resAppointment.class, genName="resAppointmentWithoutPatient", includePattern = ".*", excludes={"patient"})
public class AppointmentWithoutPatientViewConfigure {
// the doctor property will be converted to its dto version which defined by the configure class DoctorWithoutAppointmentsViewConfigure.
#OverrideViewProperty("doctor")
private resDoctorWithoutAppointments doctor;
}
// here we generate a class which has all properties of resDoctor except appointments
#ViewOf(value=resDoctor.class, genName="resDoctorWithoutAppointments", includePattern = ".*", excludes={"appointments"})
public class DoctorWithoutAppointmentsViewConfigure {}
// in you rest controller. return the dto instead of the entities.
resPatient patient = ...
resPatientView dto = resPatientView.read(patient);
List<resPatient> patients = ...
List<resPatientView> dto = resPatientView.read(patients);
At the end, the class resPatientView will has the same shap with resPatient except its appointments not having patient property and its doctor property is replaced with a version without appointments property.
Here are more examples.
The version 1.10 is ready. Will fix some bug and support the configure bean to be managed by spring.
I use GSON to change the object to json. Also, set and use the code below. As a result, the desired timestamp was obtained.
#Bean
public Gson gson() {
return new GsonBuilder()
.setExclusionStrategies(new AnnotationBasedExclusionStrategy())
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
}
However, there is a mark like AM and PM behind the time.
I hope 13:00:00 to 13:00:00 Not 1:00:00 PM
What should I do to get the results I want? Can existing timestamp results be kept together?
ToString Method shows desired result...
#Entity
#Table(name="tb_work_system")
public class WorkSystem {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(nullable=false)
private String work_system_type;
#Column(nullable=false)
private Time work_start_time;
#Column(nullable=false)
private Time work_end_time;
#Column(nullable=false)
private Time rest_start_time;
#Column(nullable=false)
private Time rest_end_time;
#Column(nullable=false,columnDefinition="BOOLEAN DEFAULT false")
private Boolean deleted=false;
#CreationTimestamp
private Timestamp created_at;
#UpdateTimestamp
private Timestamp updated_at;
}
//ToString Method
WorkSystem [id=1, work_system_type=탄력 근무제, work_start_time=09:00:00, work_end_time=18:00:00, rest_start_time=12:00:00, rest_end_time=13:00:00, deleted=false, created_at=2020-01-31 12:18:13.595343, updated_at=2020-01-31 12:18:13.595343]
//ToJson(Gson)
{"id":1,"work_system_type":"탄력 근무제","work_start_time":"09:00:00 오전","work_end_time":"06:00:00 오후","rest_start_time":"12:00:00 오후","rest_end_time":"01:00:00 오후","deleted":false,"created_at":"2020-01-31 12:18:13","updated_at":"2020-01-31 12:18:13"}
When I save entity in my repository personRepository.save(person) this returned object has a date in the format like this: Wed Dec 21 13:38:00 CET 2016. How I can change it to format like: 2016-12-21 13:38:00.732? Where this conversion is done, how this can be changed? In my database date saves in this format: 2016-12-21 13:38:00.732.
#Entity
public class Person {
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
#Temporal(TemporalType.TIMESTAMP)
#Column(name = "update_date")
private java.util.Date updateDate;
public interface PersonRepository extends CrudRepository<Person, Long> {}
You should use "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime". First of all, you change your person entity
#Entity
public class Person {
#Column(name = "update_date")
#Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime")
private LocalDateTime updateDate;
}
Then, update datetime in somewhere like PersonalImpl
import org.joda.time.LocalDateTime;
Class PersonalImpl {
void updatePerson(...){
Person person = new Person();
//do update person data
person.setUpdateDate(LocalDateTime.now());
}
}
PostgreSQL 9.5 show db
2014-09-10 07:59:14.822
#Entity
#Table(name="transactions")
#NamedQuery(name="Transaction.findAll", query="SELECT t FROM Transaction t")
public class Transaction implements Serializable{
private static final long serialVersionUID = 1L;
#Id
#GeneratedValue(strategy=GenerationType.IDENTITY)
#Column(name="id", nullable=false, unique=true, length=11)
private int id;
#CreationTimestamp
#Column(name="created_at")
private Date createdAt;
}
I have above entity using with Hibernate. I have a requirement where I need to update createdAt field but when I try following it didn't work. The createdAt is from the date of creations, which make sense.
Transaction newTransaction = new Transaction();
newTransaction.setCreatedAT(new Date);
sessionFactory.openSession().save(newTransaction);
Is there a way to keep #CreationTimestamp annotation and be able to modify or set another date when needed ?
**I am using Hibernate 4 with Spring. Any solution or another suggestion will be highly appreciated **
Why would you want to set it manually? Much cleaner solution would be to have 2 methods in your entity class. Both annotations are from the persistence package.
private Date creationTime;
private Date modificationTime;
#PrePersist
public void prePersist() {
Date now = Date();
this.creationTime = now;
this.modificationTime = now;
}
#PreUpdate
public void preUpdate() {
this.modificationTime = Date();
}
Or preferably use Java 8/Jodatime classes, in which case you need set #Type for fields manually as Hibernate doesn't support them out of the box as far as I know.