Arithmetic operation on a date


Next post about very obvious thing, but I've lost about 30 minutes to find out how to do it. So this post is some kind of note, for my information.
The problem was, how to get yesterday date from datefield. I use very nice component DataPicker from Microba Controls library which deliver calendar. Ok but back to topic. How to get a day before some date selected on that component, or day after, or back to today's date. Example:



// -1 day
try {
Calendar c = Calendar.getInstance();
c.setTime(dateField.getDate()); // set calendar date from field
c.add(Calendar.DATE, -1); // substract one day
dateField.setDate(c.getTime());
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}
// today
dateField.setDate(new Date());
// + 1 day
try {
Calendar c = Calendar.getInstance();
c.setTime(dateField.getDate()); // set calendar date from field
c.add(Calendar.DATE, 1); // add one day
dateField.setDate(c.getTime());
} catch (PropertyVetoException ex) {
ex.printStackTrace();
}

JPA, Postgresql and auto increment id attribute


I'm still writing this stupid frontend for database. Really it's confusing when have to write piece of software only for student project. Never mind. I've left idea with web app written in jsp/jsf (god's know what else) and 've started a new project. It's also would be tv schedule, but now it would be a desktop app written using netbeans, swing, postgresql and jpa.
So, todays problem. How to insert new object into database? I've written the front controller for a model, with method addStacja that get a Stacjtv object as an argument.



public boolean addStacjatv(Stacjatv st) {
em = getEntityManager();
try {
em.getTransaction().begin();
em = getEntityManager();
try {
em.getTransaction().begin();
em.persist(st);
em.getTransaction().commit();
return true;
} finally {
em.close();
} em.persist(st);
em.getTransaction().commit();
return true;
} finally {
em.close();
}
}


simple code, nothing special. But every time, when I invoke it, I get exception:


Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "id" violates not-null constraint
Error Code: 0
Call: INSERT INTO stacjatv (id, url, opis, nazwa) VALUES (?, ?, ?, ?)
bind => [null, fff, fff, ff]

well, Postgresql make auto increment using sequence. JPA doesn't know about it. I've googled and found solution in JPA Toplink Documentation. We have to tell JPA that we want to use sequence to generate next id value of primary key, so to solve the problem, we have to make some modification in model class.
I had to change Stacjatv.java which was generated by Netbeans creator. New version looks like it:

...
@Id
// start modyfication
@GeneratedValue(generator="stacjatv_id_seq",strategy=GenerationType.SEQUENCE)
// end
@Column(name = "id", nullable = false)
private Integer id;
...