5

I have a job that runs every midnight and I am using JPA to create tables. I want to assign a default value to a column (for all the rows existing in the current db) every time I start the job and then during the job I will assign that column a different value depending on some logic.

For example:

@Column(name = "delete_YN?")
private String deleteYN = "Y";
//getters setters here

I want this column to have a default value "Y" every time my job starts (each row inserted in the previous run should have the column value as "Y" at the start of the job) and the job assigns the value "N" to the column by some logic. In the end, I would be able to see which rows have the Y value and which ones have the N value to this column. Am I doing this correctly? Is this okay or should use the columnDefinition annotation? What exactly is the difference between this and the columnDefinition annotation?

Alok
  • 117
  • 1
  • 1
  • 6
  • you want the column name to have a "?" in it? – Neil Stockton Aug 13 '14 at 07:11
  • @NeilStockton no, I won't keep a "?" in the column name, but I think thats irrelevant to what my doubt is :) – Alok Aug 13 '14 at 17:33
  • @Alok Have you solved this issue? else put more information in order to help you better. – Xstian Aug 23 '14 at 00:07
  • It turns out that I have to explicitly mark all rows with the "Y" value for that column before proceeding to my other code. A simple update command will do that. The method I mentioned in my original question only assigns the "Y" value at the beginning of creating a new table. It will not alter the table with a new value to that column for all the existing rows. – Alok Aug 24 '14 at 15:35
  • have you solved this issue? if yes, how did you solve it? – Xstian Oct 13 '14 at 13:32

1 Answers1

3

@Column JavaDoc of columnDefinition

   /**
     * (Optional) The SQL fragment that is used when
     * generating the DDL for the column.
     * <p> Defaults to the generated SQL to create a
     * column of the inferred type.
     */

columnDefinition that you should use is (for Oracle)

@Column(name="delete_YN", columnDefinition="varchar2(1) DEFAULT 'Y'`")

But i think that your way is better because for the columnDefinition you're tying to db.

I suggest you also these links.... link1 and link2

I hope I've given you all the answers about your question.

Community
  • 1
  • 1
Xstian
  • 8,184
  • 10
  • 42
  • 72