Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting a default value

 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just want the value of defaultShipmentMethodCodeId to = 1 for new records created from the Customer.java class.
What do I do? I found the section of code where there are getters(?) and setters(?)

 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can just initialize your member variable in your Entity class. For example,
 
Dan Acuff
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you much!
 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also do the following:

@Column(columnDefinition = "VARCHAR(12) DEFAULT 'NEW'", name = "status", nullable = false)
 
Mark E Hansen
Ranch Hand
Posts: 650
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benjamin muktesh wrote:You can also do the following:

@Column(columnDefinition = "VARCHAR(12) DEFAULT 'NEW'", name = "status", nullable = false)

In that case, aren't you configuring the default on the database column, not on the class itself. This assumes the database supports a default, right? Also the value wouldn't be assigned until after the new record was saved in the database.

Of course, unless the OP didn't want this, that would be a better approach, as it prevents the default from getting missed even when the record is created through other means.

 
benjamin muktesh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark E Hansen wrote:
benjamin muktesh wrote:You can also do the following:

@Column(columnDefinition = "VARCHAR(12) DEFAULT 'NEW'", name = "status", nullable = false)

In that case, aren't you configuring the default on the database column, not on the class itself. This assumes the database supports a default, right? Also the value wouldn't be assigned until after the new record was saved in the database.



Yes sir it will be applied on the table and the DDL would look something like this

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic