0

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

pacionet
  • 183
  • 4
  • 15
  • 1
    can you share you code that updates the object? – SpringLearner Oct 11 '17 at 10:21
  • I think this @Temporal(TemporalType.TIMESTAMP) will always set the TimeStamp – Pwnstar Oct 11 '17 at 10:23
  • @Code.IT that simply tells it to store it as a TIMESTAMP. A field is only persisted as what is in the object being persisted/merged. –  Oct 11 '17 at 10:33
  • See https://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html Change the column type to datetime or disable auto-update for the timestamp column. – Alan Hay Oct 11 '17 at 11:17
  • Possible duplicate of [Should I use field 'datetime' or 'timestamp'?](https://stackoverflow.com/questions/409286/should-i-use-field-datetime-or-timestamp) – Alan Hay Oct 11 '17 at 11:19
  • I added the code that could cause the problem. The definition of the column in mysql should be correct, because NULLABLE DEFAULT CURRENT_TIMESTAMP store current timestamp ONLY when insert new row NOT ON UPDATE – pacionet Oct 11 '17 at 13:17
  • I also tried the annotation @CreationTimestamp but same result :( – pacionet Oct 11 '17 at 14:23

0 Answers0