5

I work on remote project and found interesting record in log:

2015-12-26 00:28:30,835 DEBUG org.hibernate.tool.hbm2ddl.SchemaUpdate
  Caller+0   at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:251)
  => alter table bail add column monthName tinyblob

with logging set to:

<logger level="trace" name="org.hibernate.tool.hbm2ddl"/>

when try to identify what happen to:

<prop key="hibernate.hbm2ddl.auto">update</prop>

on first run from backup.

When I have seen Bail.java source I am surprised:

String[] monthName = {"Января", "Февраля",
        "Марта", "Апреля", "Мая", "Июня", "Июля",
        "Августа", "Сентября", "Октября", "Ноября",
        "Декабря"
};

So this is constant field!

Is it right to store constants declaration in entity class in term of JPA / Hibernate?

How should I mark constant so it wouldn't be entity property?

I think that static keyword do the job and I think about refactoring code to:

public static final String[] monthName = 
    Collections.unmodifiableList(Arrays.asList(
        "Января", "Февраля", "Марта", "Апреля", "Мая", "Июня", "Июля",
        "Августа", "Сентября", "Октября", "Ноября", "Декабря"
));

Because production version deployed with hbm2ddl.auto=update I think I should warn DBA to remove unnecessary monthName column.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
gavenkoa
  • 45,285
  • 19
  • 251
  • 303
  • 1
    static? transient? @Transient. Ample ways. If something is a constant it should be static anyway ... but then that is nothing to do with JPA, and everything to do with Java –  Dec 26 '15 at 08:15

1 Answers1

7

All properties are persisted by default as if they were marked with the @Basic annotation.

To avoid a field from being persisted you have the following options:

  • you can mark it with @Transient
  • you can make it a static final field, but then you might want to move it to a constants class utilities anyway
  • the best approach is to use the Java 8 java.time.Month and then internationalize the month name so you can support multiple languages in the UI, while storing a universal name in the DB.
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • `final` keyword is natural for constants. Is it enough only `static` to avoid filed be persisted? I think so but afraid that there is something hidden... – gavenkoa Dec 28 '15 at 08:47
  • 2
    You can use static and then Hibernate will not persist those fields. – Vlad Mihalcea Dec 28 '15 at 09:03
  • Using Java 17 with Spring Boot JDBC I still get `org.postgresql.util.PSQLException: ERROR: column "column_name" of relation "table_name" does not exist` with a `static final` field. – Marco Lackovic Jan 30 '23 at 10:21
  • @MarcoLackovic This question is about JPA and Hibernate. If you are using Spring Boot JDBC, then this question and answer are not going to help you since you are using a different data access library. – Vlad Mihalcea Jan 30 '23 at 11:23
  • @VladMihalcea thanks, I will try to find a solution elsewhere then: for the moment I simply moved the constant to a service class. – Marco Lackovic Jan 30 '23 at 14:44