Help

One of the new features in 4.1 will be the addition of an actual API for loading entities by natural id. Yes previous versions had the ability to do natural id loading leveraging criteria queries, but that approach was very limiting. The new API will allow caching at both the Session and SessionFactory level in addition to providing a consistent API. The new approach has been made available for identifier based loading as well, again for consistency.

New methods have been added to the Session contract as a starting point (see source or javadoc for additional discussion):

public IdentifierLoadAccess byId(String entityName);
public IdentifierLoadAccess byId(Class entityClass);

public NaturalIdLoadAccess byNaturalId(String entityName);
public NaturalIdLoadAccess byNaturalId(Class entityClass);

public SimpleNaturalIdLoadAccess bySimpleNaturalId(String entityName);
public SimpleNaturalIdLoadAccess bySimpleNaturalId(Class entityClass);

All of the load access delegates have methods for getting an entity reference (getReference) and loading an entity (load). The distinction is similar to the older get and load methods on Session; getReference on load access delegates equate to Session.load and load on load access delegates equate to Session.get

public interface IdentifierLoadAccess {
    public IdentifierLoadAccess with(LockOptions lockOptions);
    public Object getReference(Serializable id);
    public Object load(Serializable id);
}

public interface NaturalIdLoadAccess {
    public NaturalIdLoadAccess with(LockOptions lockOptions);
    public NaturalIdLoadAccess using(String attributeName, Object value);
    public Object getReference();
    public Object load();
}

public interface SimpleNaturalIdLoadAccess {
    public SimpleNaturalIdLoadAccess with(LockOptions lockOptions);
    public Object getReference(Object naturalIdValue);
    public Object load(Object naturalIdValue);
}

So let's say we have an entity defining a natural id:

@Entity
@Table(name="T_USER")
public class User {
    @Id
    private Long id;
    @NaturalId
    private String username;
    ...
}

we can load instances of that class by natural id as follows:

session.byNaturalId( User.class ).using( "username", "steve" ).load();

That actually makes sure we get back an initialized instance, just like Session.get does. If we just want a possibly uninitialized reference we would use this instead:

session.byNaturalId( User.class ).using( "username", "steve" ).getReference();

Since the natural id here is simple (just a single attribute) we can make this even easier:

session.bySimpleNaturalId( User.class ).load( "steve" );
14 comments:
 
25. Jan 2012, 09:19 CET | Link
Adrien OpenWide

If I remember correctly, Hibernate 4 need at least Java 1.5. Why is this new API not using generics ? I understant the byId(String entityName) can't know the real type, it could still return a IdentifierLoadAccess<Object>

ReplyQuote
 
26. Jan 2012, 14:02 CET | Link
John

I thought too that the hibernate 4.0 api would have evolved to use more generics but methods like get/load/merge haven't been parameterized.

 
27. Jan 2012, 13:19 CET | Link

The issue is that Hibernate has a feature that causes problems with parameterizing those sigs, @Proxy. Specifically if you specify @Proxy.proxyClass to name an interface Hibernate uses that interface to define proxies used for lazy loading, not the class. The proxies are only castable to the interface (hierarchy), not the class (hierarchy). There is a discussion on the dev mailing list if you care to join in.

 
21. Mar 2012, 13:48 CET | Link
Jeremy Stone

How do I arrange that the new byNaturalId loading be L2 cached? (With old criteria approach, setCacheable/setCacheRegion could be used.)

Also is the new approach intelligent enough to know not to perform an (expensive) dirty check/session flush unnecessarily?

 
29. Mar 2012, 14:59 CET | Link
Guenther Demetz

Hi Jeremy,

 
29. Mar 2012, 15:01 CET | Link
Guenther Demetz

Hi Jeremy,

How do I arrange that the new byNaturalId loading be L2 cached?

Use @NaturalIdCache annotation

Also is the new approach intelligent enough to know not to perform an (expensive) dirty check/session flush unnecessarily?

Currently it is not very intelligent in that sense, for this purpose I opened Hibernate JIRA issue Please take a look at it and, if you like this as enhancement, please vote for it.

 
24. Oct 2012, 10:09 CET | Link
Jeremy Stone

Thanks Guenther (although rather belatedly). The JIRA issue addresses my concerns exactly.

 
23. May 2014, 11:34 CET | Link

Can I know the difference between Session.load and Session.byId.load method?

 
20. Jun 2014, 09:14 CET | Link
The get() method is special because the identifier uniquely identifies a single instance of a class.
If load() can’t find the object in the cache or database, an exception is thrown. The load() method never returns null. The get() method returns null if the object can’t be found.
 
09. Jul 2014, 14:31 CET | Link

I searched for this code on Google for long hours. I could not find it at all. The codes they have given in the Google codes are different and it is not working for me. But this one did. Thank you for that. Thanks for your explanation on loading natural id.

 
20. Jul 2014, 06:57 CET | Link
senlldy | ddeeqz(AT)163.com

Cynthia Steffe at Mercedes-Benz 2014 bag 2010 is at high-end boutiques and the online stores Neiman Marcus, Bloomingdales and Saks Fifth Way. These are high-end clothes that fake gucci belts any woman can feel happy in. Cynthia Steffe can be found at less pricey prices at online discount slots.

Storage space: Your bag should plenty of storage space to carry all the primary items like cellular phones, keys, credit cards, numerous others. for your simplicity louis vuitton replica of use. If you definitely student then select a bag which help you carry books, notes, et cetera. You can try it out for school bags that have two straps which aid you to balance the weight.

But you get the shoes to order the purse to almost free. Advertising even listen up to exactly what the shoes would cost in the city. An extremely also far more replica prada convenient to order the handbags and shoes on the Internet, anyone will not be disturbed by anyone here, either business clients nor vendors. In authentic replica handbags shops, there as well many all-matched replica chanel brand bags like Gucci, Fendi, Burberry, Coach, Lv handbags. Its color and design may match every outfits and you can get it with a competitive price, but it has a quality so that it's difficult to distinguish it starting from an authentic any. If you are a beautiful lady who loves fashion, discover take actions right now to yourself!

 
22. Jul 2014, 07:07 CET | Link

That was really a hard task to find this code by searching in Google or whatever. Thanks for saving the time of us and sharing this article. Regards,

Pure Moroccan Argan Oil For Hair and Skin
 
30. Jul 2014, 08:59 CET | Link

Capita di essere una riproduzione meraviglioso stupefacente osservare con eccezionale replica orologio 7750 cronografo mobilità intelligente con regolatore funzionale così come lame incise. A parte le ore odinary, momenti, sub-secondi opera, che possiede giorno mostra più la funzione di cronografo. L'dare un'occhiata a occasione è spesso ogni PVD spruzzato più migliorato di platino rivestito in acciaio 316L.

 
13. Aug 2014, 15:21 CET | Link

Consistent API is the feature I've been looking for, since ages and now I could get it. But, I don't have the proper programming function, so I could get it from this post. I'm working on multiple APIs for my top 10 essay writing services project and I'm glad to get it on track. Thanks for the share.

Post Comment