The nested field does not work after upgrading to Hibernate 5.0.6. Default in Spring Download 1.3.1.RELEASE

The presence of a nested class MultiLanguageText:

@Embeddable public class MultiLanguageText { @Field private String textDe; @Field private String textFr; @Field private String textIt; //... } 

And another class that uses this class twice:

 @Entity(name = "T_AnotherClass") public class AnotherClass{ @Id @GeneratedValue private long key; @Embedded private MultiLanguageText name; @Embedded private MultiLanguageText description; //... } 

The fields were perfectly translated into "name_textDe", "description_textDe", "name_textFr", etc. with spring version 1.2.7.RELEASE.

However, to store LocalDate, I wanted to switch to Hibernate 5. I followed the process described here: https://github.com/spring-projects/spring-boot/issues/2763#issuecomment-154419889

The process worked fine, but the translation of the embedded fields stopped working *. I tried different implicit_naming_strategy and physical_naming_strategy but didn't work.

If I annotate the fields as follows, it works, but the process is somewhat cumbersome:

 @Embedded @AttributeOverrides({ @AttributeOverride(name = "textDe", column = @Column(name = "name_textDe", length = MultiLanguageText.MAX_TEXT_LENGTH)), @AttributeOverride(name = "textFr", column = @Column(name = "name_textFr", length = MultiLanguageText.MAX_TEXT_LENGTH)), @AttributeOverride(name = "textIt", column = @Column(name = "name_textIt", length = MultiLanguageText.MAX_TEXT_LENGTH)), }) private MultiLanguageText name; 

* Doesn’t work means that I get an exception something along the lines (since now the field is displayed without the field prefix and therefore the field exists twice):

 Caused by: org.hibernate.MappingException: Repeated column in mapping for entity: AnotherClass column: textDe (should be mapped with insert="false" update="false") at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:764) at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:782) at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:778) at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:804) at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:539) at org.hibernate.mapping.RootClass.validate(RootClass.java:265) at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ... 48 more 
+5
source share
2 answers

To fix this in Spring Boot 1.4, add this to your application.yaml :

 spring.jpa.hibernate.naming.implicit-strategy: org.hibernate.boot.model.naming.ImplicitNamingStrategyComponentPathImpl 

From javadocs :

An ImplicitNamingStrategy implementation that uses the full compound path extracted from AttributePath, as opposed to only the terminal property part. Basically, the senior port is the DefaultComponentSafeNamingStrategy Class, which implements the more supported NamingStrategy contract.

+9
source

Hibernate's official documentation states:

If you want to have the same type of inline object twice in the same object, the default column name will not work, since multiple inline objects will have the same set of columns. In simple JPA, you need to override at least one set of columns. Hibernation, however, allows you to improve the default naming mechanism through the NamingStrategy interface. You can write a strategy that prevents matching names in such a situation. DefaultComponentSafeNamingStrategy is an example of this.

Therefore, if you want to have full JPA compliance, stick to your current implementation.

+1
source

Source: https://habr.com/ru/post/1239807/


All Articles