Automatically prefix column names for @Embeddable classes

I am developing a project in which I save some POJOs by adding Hibernate annotations. One of the problems I ran into is that such code does not work, since Hibernate is trying to match the subfields in Time_T in one column (i.e. startTime.sec and stopTime.sec both try to match with colum sec , causing an error )

 @Entity public class ExampleClass { @Id long eventId; Time_T startTime; Time_T stopTime; } @Embeddable public class Time_T { int sec; int nsec; } 

Since there will be many such events in the system, it would be nice if it were possible to automatically add a prefix to the column name (for example, make the columns startTime_sec , startTime_nsec , stopTime_sec , stopTime_nsec ), without the need to apply overrides for each field. Does Hibernate have this feature or is there any other reasonable work?

+23
java orm hibernate
Jun 15 '10 at 15:34
source share
3 answers

Try setting the hibernate.ejb.naming_strategy property to org.hibernate.cfg.DefaultComponentSafeNamingStrategy

+20
Jun 15 '10 at 15:39
source share

Another way to solve the problem is to use @AttributeOverrides and @AttributeOverride annotations. In your example, the Time_T.sec property Time_T.sec displayed in the sec column. You can map ExampleClass as follows:

 @Entity public class ExampleClass { @Id long eventId; @AttributeOverrides( @AttributeOverride(name = "sec", column = @Column(name = "start_sec")) ) Time_T startTime; Time_T stopTime; } 

Displaying the result startTime.sec <=> start_sec and stopTime.sec <=> sec . Of course, you could use annotations to create a more meaningful name for the stopTipe.sec column.

+4
Jun 15 '10 at 16:26
source share

In my case with org.hibernate: hibernate-core: 5.0.12.Final and org.springframework.boot: spring-boot-starter-data-jpa: 1.5.2.RELEASE I had to do the following properties in the application.properties file:

 spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl 
+3
Apr 14 '17 at 13:52 on
source share



All Articles