0

How to set a default value of an Id in JPA to 38? I know that I can set a single id length by using @Column(length=38), but how to change it globally?

If this is not possible with JPA, maybe it is with Spring Data or Hibernate?

Greg Zuber
  • 1,229
  • 1
  • 12
  • 22
  • You might use a common baseclass like AbstractPersistenObject, where you define the id field. – Uwe Allner May 05 '17 at 09:04
  • [this](http://stackoverflow.com/questions/197045/setting-default-values-for-columns-in-jpa) is most probably what you want – XtremeBaumer May 05 '17 at 09:05
  • default "length" ? perhaps that depends on what TYPE it is. Since `@Column` "length" only applies if it is a String (see JPA javadocs), then you are using something invalid there – Neil Stockton May 05 '17 at 09:33

1 Answers1

0

I think you should add a abstract parent class for your entities, here is my AbstractEntity:

@MappedSuperclass
public class AbstractEntity {

  @Id
  @GeneratedValue
  @Column(length=38)
  private Long id;

  @Column(name = "created_at")
  private Date createdTime;

  @Column(name = "updated_at")
  private Date updatedTime;

  @PrePersist
  public void init() {
      if(createdTime == null) {
          createdTime = new Date();
      }
      updatedTime = new Date();
  }

  public Long getId() {
      return id;
  }

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

  public Date getCreatedTime() {
      return createdTime;
  }

  public void setCreatedTime(Date createdTime) {
      this.createdTime = createdTime;
  }

  public Date getUpdatedTime() {
      return updatedTime;
  }

  public void setUpdatedTime(Date updatedTime) {
      this.updatedTime = updatedTime;
  }


}
xierui
  • 1,047
  • 1
  • 9
  • 22