I have a MySQL table like this:
ID_EMAIL bigint(20)
ID_NEWSLETTER bigint(20)
SUBSCRIPTION_DATE timestamp NULLABLE DEFAULT CURRENT_TIMESTAMP
and a JPA class like this:
@Temporal(TemporalType.TIMESTAMP)
@Column(name="SUBSCRIPTION_DATE", updatable = false)
private Date subscriptionDate;
@ManyToOne
@JoinColumn(name="ID_EMAIL",insertable = false, updatable = false)
private AnagraficaIscritti anagraficaIscritti;
//bi-directional many-to-one association to Newsletter
@ManyToOne
@JoinColumn(name="ID_NEWSLETTER", insertable = false, updatable = false)
private Newsletter newsletter;
When I insert an object in other tables the field SUBSCRIPTION_DATE is always updated . This is wrong because I'd like to store only "creation_date" so I'd like to prevent to being updated every time.
So, for example, if I am inserting record 51 all the previous column SUBSCRIPTION_DATE record are updated !!!
| 48 | 34 | 2017-10-11 12:09:42 |
| 49 | 34 | 2017-10-11 12:09:42 |
| 50 | 34 | 2017-10-11 12:09:42 |
| 51 | 34 | 2017-10-11 12:09:42 |
+----------+---------------+---------------------+
Any hints?
the code of the update that could cause the problem should be this:
for (Newsletter item : newsletterItem ) {
boolean isIscritto = false;
for (AnagraficaIscritti iscItem : item.getAnagraficaIscrittis() ) {
if (iscItem.getIdEmail() == iscritto.getIdEmail())
isIscritto = true;
}
if (!isIscritto) {
item.getAnagraficaIscrittis().add(iscritto);
em.persist(item);
}
}
I think that when I persist Newsletter object all the column SUBSCRIPTION_DATE are updated at the same value for the same newsletter
I also tried to modify the SQL declaration in
SUBSCRIPTION_DATE timestamp NULLABLE DEFAULT NULL
when it updates the SUBSCRIPTION_DATE updates all the rows to NULL.
Thanks