Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate: why field.camelcase?

Can someone tell me why in NHibernate mapping we can set access="field.camelcase", since we have access="field" and access="property"?

EDIT: my question is "why we can do this", not "what does it mean". I think this can be source of error for developper.

like image 983
Francois Avatar asked Feb 20 '23 22:02

Francois


2 Answers

I guess you wonder what use field.camelcase have when we can do the same with just field? That's true, but that would give (NH) properties unintuive names when eg writing queries or reference the property from other mappings.

Let's say you have something you want to map using the field, eg

private string _name;
public string Name { get { return _name; } }

You sure can map the field using "field" but then you would have to write "_name" when eg writing HQL queries.

select a from Foo a where a._name = ...

If you instead using field.camelcase the data, the same query would look like

select a from Foo a where a.Name...

EDIT I now saw you wrote "field.camelcase" but my answer is about "field.camelcase-underscore". The principles are the same and I guess you get the point ;)

like image 161
Roger Avatar answered Mar 24 '23 21:03

Roger


the portion after the '.' is the so called naming strategy, that you should specify when the name you write in the hbm differ from the backing field. In the case of field.camelcase you are allowed to write CustomerName in the hbm, and NHibernate would look for a field with name customerName in the class. The reason for that is NHibernate not forcing you to choose a name convention to be compliant, NH will works with almost any naming convention.

like image 38
Felice Pollano Avatar answered Mar 24 '23 21:03

Felice Pollano