1

There is a table

CREATE TABLE `test` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) NOT NULL,
 `lastmodifiedTimestamp` DATETIME ON UPDATE CURRENT_TIMESTAMP,
  `creationTimestamp` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`),
 UNIQUE KEY `Unique` (`name`)
)

and a Entity class which i use for Json conversion as well as database storage. The issue here is that when i try to create a Test hibernate gives an exception com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'creationTimestamp' cannot be null However i can run an sql query without passing Timestamp fields.How can i make hibernate avoid sending these timestamp fields in the insert query?

I cannot mark them as transient because i need it the timestamp fields during de-serialization.When i do a get for Test object.

It anyway gives Caused by: javax.persistence.EntityNotFoundException: No row with the given identifier exists perhaps because of both @Column and @Transient on the fields

Below is the entity class

@Entity
@Table(name = "test")
public class Test implements Serializable {
 private long id;
 @JsonSerialize(using = DateTimeSerializer.class)
  private DateTime creationTimestamp; //joda datetime used in response body
  @JsonIgnore                       //used in http header
  private DateTime lastModofiedTimestamp;

@Id
  @Column(name = "id")
  public long getId() {
    return id;
  }

  public void setId(long id) {
    this.id = id;
  }

  @Column(name = "creationTimestamp")
  @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
  public DateTime getCreationTimestamp() {
    return creationTimestamp;
  }

  public void setCreationTimestamp(DateTime creationTimestamp) {
    this.creationTimestamp = creationTimestamp;
  }
@Column(name = "lastmodifiedTimestamp")
  @Type(type = "org.joda.time.contrib.hibernate.PersistentDateTime")
  public DateTime getLastModofiedTimestamp() {
    return lastModofiedTimestamp;
  }

  public void setLastModofiedTimestamp(DateTime lastModofiedTimestamp)   {
    this.lastModofiedTimestamp = lastModofiedTimestamp;
  }
}

If i pass a proper value of creationTimestamp from the UI which populates the field on json de-serialization is used by hibernate to pass to the insert query.It works in this case and creates the row in database But i want this value not to be sent from UI and ignored if sent.This is for the create call But during get call I want the timestamp values to be populated in the object

rakesh99
  • 1,234
  • 19
  • 34
  • This is yet another example of why mixing Entities and DTOs is not a good idea. See this [question and answer](http://stackoverflow.com/questions/31165016/dto-and-entity-in-one-object/31171996#31171996) – DuncanKinnear Jul 06 '15 at 19:59
  • http://stackoverflow.com/questions/1440952/why-are-data-transfer-objects-an-anti-pattern here i read just the opposite and how will having a separate DTO solve this issue as i need to send the field in get calls but need persist auto generated values during put(not the sent one)?I feel using @Formula is not that complicated compared to that – rakesh99 Jul 06 '15 at 20:25
  • The 'problem' you have encountered will be the first of many, many issues you will face if you do not split your Entities and DTOs. Trust me, we got to over 300 Entitiy classes before we realised that we had to split the functionality and it was a painful exercise. – DuncanKinnear Jul 06 '15 at 20:31
  • Oh, and the question you referenced was from 2009! Long before the invention of many of the Hibernate and JSON specific annotations used today. – DuncanKinnear Jul 06 '15 at 20:52
  • 1
    Have you looked at controlling the serialization of the object yourself rather than using the default java serialization? Sending on serialization/deserialization isn't a JPA/Hibernate issue. If you want JPA to not insert/update a field, mark it as updatable=false, insertable=false, making it read-only. – Chris Jul 07 '15 at 13:24
  • yes @Column(name = "..", insertable = false, updatable = false) works and appears more logical than @Formula – rakesh99 Jul 07 '15 at 18:28

1 Answers1

1

You can use @Formula instead of the @Column to annotate the creationTimestamp field but use the same creationTimestamp in the formula. See for example

StanislavL
  • 56,971
  • 9
  • 68
  • 98