简体   繁体   中英

In Hibernate how to ignore a property only during serialization and not in deserialization

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM