Hibernate entity-name

In a complex application a persistent entity is typically used by multiple use cases. These use cases put different requirements on mapping such a persistent entity. For example, in a simple case only basic attributes must be mapped whereas in a more complex situation for the same entity a larger set of attributes (+ relationships) must be mapped.
When mapping a persistent entity, tradeoffs must be made regarding performance and the required code to implement the solution.
For the above scenario a developer would probably just map all of the persistent attributes (regardless whether they are required in all of the use casesor not) and eventually map relationships with some lazy loading enabled.
Dealing with it this way mostly works even in larger projects but can totally fail in others (especially when mappings are created by more than one developer and no design rules are in place).

Hibernate provides a mechanism to deal with this kind of troubles very well. One can actually provide multiple mappings for a single persistent class through the mapping attribute ‘entity-name’.
For example I had a situation where an entity had a primary key but also an alternate key which was used heavily in other entities for referencing this one.

So I just created 2 different mappings for this one class, mapping different attributes as id.

	
		

	

	

		
	

When referring to a specific mapping the ‘entity-name’ attribute is used

  

When using the Hibernate API, look out for overloaded methods that allow passing in a entity-name!

4 Comments

  • deksa - 2010/10/19

    Hi Martin,
    Can you provide example class and database design so we can understand what exactly is going on. I don’t quite understand what PC1 and PC2 refers to.
    I am interested in this because entity-name is possibly a solution to my problem described here: https://forum.hibernate.org/viewtopic.php?f=1&t=1007627&p=2437133#p2437133
    Kind Regards,
    Deksa
    P.S.: If you know a solution to the problem, it would be great if you could provide one.

    Reply
    • Martin Ahrer - 2010/10/19

      I’m sorry that I just can’t provide a working example but that is not required if you have a basic understanding of Hibernate. The entity-name by default is the simple class name. So if you had a class with FQN package.PersistentClass then the entity name of the mapped persistent entity would be PersistentClass. You can tweak that name (that must be unique across all the mappings in your persistence context).
      So the entity name is a kind of logical name for your class mapping. That name can be used in mapping declarations to refer to some persistent class but there are also plenty of hibernate APIs that allow you to e.g. perform a query against a mapped entity for which you pass in the entity name (just look at Session.get or Session.createCriteria).

      Hope that helps to understand the idea of entity name.

      Reply
  • balint - 2011/06/30

    Hi,
    I am just in need of this kind of solution. Works perfectly until I use somewhere the mapping…that is a bit screwed, as AFAIK can be used only with Classes in the meta-value tag and no entity-names…
    Can you think about a workaround? It’s a big legacy code, I can’t change the table structure, I must use — and I need the duplicated mapping of the classes…
    Thanks in forward!
    Kind Regards,
    Balint

    Reply

Leave a Reply

green red blue grey