0

I am new to hibernate and having difficulty in understanding what is the use of logical name in hibernate? I have read this http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#PhysicalNamingStrategy

I have failed to understand the difference between class attributes name, logical name and physical name. I understand that physical name is the name in db, attributes name are of course java class attributes name. But what is the role of logical name? And why there are 2 stages now called as ImplicitNamingStrategy and PhysicalNamingStrategy?

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Chirag
  • 618
  • 6
  • 21

2 Answers2

1

Logical name is defined in your java / hiberate configuration. Physical name is the name in your database. According to the naming strategy section in the official documentation : http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#naming] :

Part of the mapping of an object model to the relational database is mapping names from the object model to the corresponding database names. Hibernate looks at this as 2 stage process:

The first stage is determining a proper logical name from the domain model mapping. A logical name can be either explicitly specified by the user (using @Column or @Table e.g.) or it can be implicitly determined by Hiernate through an ImplicitNamingStrategy contract.

Second is the resolving of this logical name to a physical name which is defined by the PhysicalNamingStrategy contract.

Community
  • 1
  • 1
Imen Gharsalli
  • 367
  • 2
  • 13
  • If I have mentioned the link of the doc, that means I have read it. tell me why do you need logical name ? – Chirag Sep 13 '17 at 15:22
  • Sure. JPA defines no distinction between logical and physical name. However, you can override PhysicalNamingStrategyStandardImpl.toPhysicalTableName(Identifier name, JdbcEnvironment context) if you wish to define rules around the naming of database objects. This allows the implementatio of naming rules without having to hard code them. I hope I was helpful. – Imen Gharsalli Sep 13 '17 at 15:31
  • Noooooooooooooo – Chirag Sep 13 '17 at 15:33
  • Logical name is the one in you code/ hibernate configuration. You have the choice to either keep it as physical name or use a naming strategy to define rules for your physical name. Please be specific about what's not clear. – Imen Gharsalli Sep 13 '17 at 15:40
  • Imen, thanks for your time. But I wanted to know why do we need logical name? Carlitos answered it. – Chirag Sep 13 '17 at 15:47
1

The idea of having 2 phases is to separate concerns or responsability.... allowing you to be more flex ... example: suppose that you are in a massive project, and there 2 teams, one responsable for the database design (including naming), and the other for object modeling... the database team is always making changes in their naming standard (for whatever reason), however, they are responsable of implementing the physical name strategy and they put as contract that the logical name should be lower snake case (example: dummy_attribute)... if hibernate does not allow you to define a logical namimg strategy, then you will have to name your class attributes also in lower snake case... that's the reason of 2 phases

Carlitos Way
  • 3,279
  • 20
  • 30
  • Thanks Carlitos, this is what I needed. Another question, I want to know about the default behaviour of implicit name strategy. Is it to change the camel case attribute names to lower snake case ? The doc says that default implicit strategy is ImplicitNamingStrategyJpaCompliantImpl , but I want to know the behaviour of ImplicitNamingStrategyJpaCompliantImpl. – Chirag Sep 13 '17 at 15:45
  • I'm not really sure about this one, because I always name my entities and attributes explicitly (using either `@Table` or `@Column`, respectively) ... However, I suppose that ImplicitNamingStrategyJpaCompliantImpl use JAVA Class/attributes naming convention based on this SO [thread](https://stackoverflow.com/questions/37062675/hibernate-5-1-x-naming-strategy-backward-compatible-with-hibernate-4-x) ... you'll have to play with the provided configurations to be really sure... – Carlitos Way Sep 13 '17 at 16:50