Ok,
Spring boot app:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Domain:
@Table(name = "aaa")
@Entity
@Setter
@Getter
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "event_seq")
@SequenceGenerator(name = "event_seq", sequenceName = "aaa_seq", allocationSize = 1)
private long id;
@Column(name = "startdate")
private ZonedDateTime startDate;
}
repo
public interface EventRepository extends JpaRepository<Event, Long> {
}
converter
@Converter(autoApply = true)
public class ZonedDateTimeConverter implements AttributeConverter<ZonedDateTime, Date> {
@Override
public Date convertToDatabaseColumn(ZonedDateTime dateTime) {
return dateTime != null ? Date.from(dateTime.toInstant()) : null;
}
@Override
public ZonedDateTime convertToEntityAttribute(Date date) {
if (date != null) {
Instant instant = Instant.ofEpochMilli(date.getTime());
return ZonedDateTime.ofInstant(instant, ZoneOffset.UTC);
}
return null;
}
}
Controller
@RestController
public class DefaultController {
@Autowired
private EventRepository eventRepository;
@GetMapping("/create")
public void createEvent() {
Event event = new Event();
event.setId(0);
event.setStartDate(ZonedDateTime.now());
eventRepository.save(event);
}
}