10

I know there are plenty of these questions here on SO and also on the net, but all the answers suggest using columnDefinition which is database specific and hence not applicable for me because the system I'm working on needs to run on different databases.

I found this hibernate issue where someone requested this feature for annotations. The issue has been closed saying that another issue will cover that functionality. The second issue apparently added annotation @Generated and also some others, but I couldn't find any documentation on how to define the default column value with those new annotations.

So my question is: Does anyone know how can I define a default column value with annotations (and NOT using columnDefinition)?

Edit: to further clarify my problem: When I add a new not null column, I need Hibernate to update the existing schema (add the new column to the respective table). But since the column is non null, the database cannot create the column without specifying the default value (if there are already some rows in the table). So I need to instruct Hibernate to issue the following DDL statement: ALTER TABLE my_table ADD COLUMN new_column VARCHAR(3) DEFAULT 'def', but it has to be independent of the used database.

2 Answers 2

16

I don't think you need any documentation, the java docs are self explaining. If I understand you correctly you need a way to set a default value for a field. If yes please see the following code snippet.

@Entity @Table(name = "my_entity") public class SomeEntity extends BaseEntity { public static final class MyValueGenerator implements ValueGenerator<String> { @Override public String generateValue(Session session, Object owner) { return "This is my default name"; } } @Basic @Column(name = "name", insertable = true, updatable = true, nullable = false, length = 255) // This will add a DDL default @ColumnDefault("'This is my default name'") // This will add a runtime default. @GeneratorType(type = MyValueGenerator.class) private String name; // getters and setters } 
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks but this just seems to generate values on inserted entities. That's not what I need. I need to define the default column value when the column is added by the DDL statement (I edited my question to make it clearer).
@Jardo Have you tried the '@ColumnDefault' annotation ? I will update the my code to reflect the change.
@Bbl That's exactly what I needed thanks. I have just one remark: Hibernate puts the value from the annotation directly into the DDL statement so if you want to insert a string, you have to escape it with additional quotes or else you'll get an SQL error. Like this @ColumnDefault("'This is my default name'"). If you edit your answer to reflect this, it will be perfect :)
The @ColumnDefault annotation is used to specify the DEFAULT DDL value to apply when using the automated schema generator. (from docs.jboss.org/hibernate/orm/5.2/userguide/html_single/…)
How can i take default value from another table? I have to set default value from a table called country, where PK is country_id(which I want to set as default where countryname= "US")
7

Following code is working for me.

@ColumnDefault("'0.0'") @Column(name = "avgRating") private float avgRating; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.