Better handling of implicit column naming with @ElementCollection of @Embeddables

Description

DefaultComponentSafeNamingStrategy is incompatible with @ElementCollection of @Embeddable objects.

Given the following classes

@Entity public class Foo { @ElementCollection private List<Bar> bars; ... } @Embeddable public class Bar { private String name; ... }

and DefaultComponentSafeNamingStrategy as a naming strategy, Hibernate generates the schema with the following incorrect table:

create table Foo_bars (Foo_id bigint not null, bars_collection&&element_name varchar(255))

.collection&&element. is an internal placeholder that should be removed before use of property name. Other naming strategies effectively remove it by using the part of property name after the last ., but DefaultComponentSafeNamingStrategy doesn't, it just replaces . with _.

Here is a workaround:

public class FixedDefaultComponentSafeNamingStrategy extends DefaultComponentSafeNamingStrategy { @Override public String propertyToColumnName(String propertyName) { return super.propertyToColumnName(propertyName.replace(".collection&&element.", ".")); } }

Activity

Hendy IrawanMarch 29, 2016 at 4:52 PM

Can this fix be backported to 4.3, please?

Steve EbersoleMarch 11, 2015 at 12:25 AM

FYI, this work has all been pushed to master..

Emmanuel BernardFebruary 16, 2015 at 7:47 AM

, I think we do play with "collection&&element" in OGM, do you think the improvement Steve made for 5.0 will impact us? If yes, we should open a JIRA to anticipate that and see if we need some metadata from elsewhere instead of this ad-hoc placeholder.

Steve EbersoleFebruary 10, 2015 at 9:18 PM

This is fixed locally as per my last comment. This work has not yet been pushed to master.

Steve EbersoleFebruary 10, 2015 at 9:05 PM

I have this fixed at least for the test I added previously. Initially, I was getting a column named items_collection&&element_name because org.hibernate.cfg.Ejb3Column#redefineColumnName was passed "items.collection&&element.name" as the propertyName. So in org.hibernate.cfg.Ejb3Column#redefineColumnName I added the following:

if ( propertyName.contains( ".collection&&element." ) ) { propertyName = propertyName.replace( "collection&&element.", "" ); }

This reduces the propertyName to the more reasonable "items.name". The component-based naming strategy now produces items_name as the column name; the standard naming strategy produces name.

Fixed

Details

Assignee

Reporter

Components

Fix versions

Affects versions

Priority

Created March 11, 2011 at 5:55 PM
Updated June 16, 2016 at 5:12 PM
Resolved February 10, 2015 at 9:18 PM

Flag notifications