Given this Entity I need to post the new Object of type Contracts. I http://www.springframework.org/tags/form tag form/sf. I also got jquery datapicker. The problem I have got is datapicker returns a String not Date object. How can it be parsed? The only solution I think will work is to get date from datepicker as @RequestParam in the @Controller class and parse it as a java.util.Date object.
@Entity
@Table(name = "contract")
public class Contracts {
@Id
@Column(name = "contract_id")
private int contractId;
@Column(name = "date_added")
private Date creationDate;
@Column(name = "date_start")
private Date startDate;
@Column(name = "date_end")
private Date finishDate;
@NotNull
@Column(name = "payment_amount")
private Integer paymentAmount;
@Column(name = "payment_type")
@Enumerated(EnumType.STRING)
private PaymentType paymentType;
private boolean valid;
@ManyToOne
@JoinColumn(name = "system_id")
private Systems system;
And this is 'POST' part of my Controller class.
@RequestMapping(value = "/createcontract", method = RequestMethod.POST)
public String createContract(@ModelAttribute("contract") @Valid final Contracts contract, BindingResult results,
@RequestParam("system-id") int systemId) {
if(results.hasErrors())
return "newcontract";
return "redirect:contracts";
Another question (because I tagged postgresql) is whether is 'all-right' to store java.util.Data object in postgresql as just date or maybe I should store it as 'timestamp with time zone'?
jQuery UI DatePicker gives Date object
The plugin allows you to obtain a Date object using getDate method.
Here is a Plunker with a working example.
Date type - JPA to PostgreSQL mapping
timestamp with time zonein PostgreSQLUsually is best to keep your dates as UTC in your database. This will give the most flexibility as you can represent the date in whichever zone you desire. This translates into using the
timestamp with time zoneas a type which will store as UTC. This means that your date time, before being stored, is added the zone offset in order to obtain the UTC.Is important that all your dates are expressed as UTC and not in a specific time zone. And when these are sent via the wire to your database the session needs to have the UTC time zone.
This means that the timezone needs to be UTC on JVM or JPA provider (eg: Hibernate).
As explained in this blog you can set UTC by:
JVM:
java -Duser.timezone=UTC -jar blabla.jarOr by
JVM:
TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));Or by
Hibernate: (application.properties)
spring.jpa.properties.hibernate.jdbc.time_zone = UTCCheck which version works for you.
DatefieldTherefore your field can be expressed as:
And you need to ensure that the
Dateobject represents a date time in UTC.You can store your Date object in PostgreSQL as:
timestampwith or without timezonetimewith or without timezonedateUsing the JPA annotation
@Temporalapplied on yourDatefield you can specify how the object is being mapped:@Temporal(TIMESTAMP)to timestamp@Temporal(DATE)todate@Temporal(TIME)totimeAlso specifying that your column is
timestamp with timezonecan be stated using annotation@Column(columnDefinition = "TIMESTAMP WITH TIMEZONE").