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:

public class User {
    private Long id;
    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" );
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>

26. Jan 2012, 14:02 CET | Link

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)

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.

19. Sep 2014, 10:47 CET | Link

Many reasons exist why these big bags have grown in popularity recently. Small bags are limited in space these large replica handbags enable you to carry allot of stuff. Fashion statement ten these bags is made from obvious appeal. Think about the following factors when selecting a big bag. Costs are these bags can be astronomical so make sure you set an affordable you possibly can stick to. If you are away from a designer Louis Vuitton handbag would pay you to assign otherwise you can find them reasonable. As soon as you decide on a brand name or a style you want, it is best to do a little looking around to discover who supplies the best price, unique an outlet inside the mall in your area or a web retailer. Totes are far and away, aren't difficult to get, nevertheless, you should spend some time searching to locate precisely what is befitting you are interested in.

One bag called Hobo Bag Due to its distinctive form of a half moon. The name comes hobo bag hobo and migrant workers with used this kind of to a stick bag to transport your things when they traveled from a single location to another. Naturally, hobo bags today tend to be more elegant and so are stated in a variety of styles for everyone types of materials, even if the skin is just about the most popular choices. They come at the smallest prices entirely for the designer. A lot of these large louis vuitton replica handbags are very popular today, and you can see many celebrities putting them on. You will find imitation handbags that resemble those expensive designer bags. Replica chanel handbags won't be the same Forgery or false, the location where the seller is trying to trick you into thinking you receives the actual article. The replicas are produced with the same style as the primary goal as being the designer purse, but you are significantly less expensive, nor make false claims. The reduced valuation on these bags is due to the product quality and workmanship as well as the expense of materials.

20. Sep 2014, 20:21 CET | Link
James David | jamesdavid(AT)
21. Sep 2014, 18:55 CET | Link
Click HELP for text formatting instructions. Then edit this text and check the preview.I have found this article very exciting. Do you have any others on this topic? I am also sending it to my friend to enjoy your writing style. Thanks
23. Sep 2014, 13:16 CET | Link

Researching upon any topic thoroughly and then write it as readers can fully understand it is not possible until you have well researching and writing skills. Our Essay Planet writing service on this website are all in all because we are perfectly satisfying people and charging low price due to which people love to get our writing services.

24. Sep 2014, 07:06 CET | Link

thank for sharing..

26. Sep 2014, 08:45 CET | Link

Resources like the one you mentioned here will be very useful to me! I will post a link to this page on my blog My Link My Link

26. Sep 2014, 09:36 CET | Link

Your article had provided me with another point of view on this topic. I had absolutely no thought that things can easily work in this method as well. Thank you for sharing your opinion. obat gondok obat jantung koroner

29. Sep 2014, 09:50 CET | Link

The harmful group of 3 represents the three most harmful ingredients included in many fingernail enhance manufacturers. The three ingredients are toluene, dibutyl phthalate (DBT) and chemicals. official website

30. Sep 2014, 11:43 CET | Link

Research how the cup is designed before you purchase your display because outstanding insulating material cup is a vital feature of ms windows. important source

09. Oct 2014, 08:40 CET | Link
Agen 338a

It is essential for you to get the level difference considering that it will impact your victories and failures. Know the most ideal a chance to bet. Agen 338a

13. Oct 2014, 13:27 CET | Link
click to read

Apart from this Dayton is also indulged in quick delivering on services as they don't want to wait for a long time for repairing done. click to read

15. Oct 2014, 08:40 CET | Link

Click HELP for text formatting instructions. Then edit this text and check the preview.

15. Oct 2014, 08:45 CET | Link is a reliable custom paper writing agency offering a wide spectrum of efficient writing services.

04. Nov 2014, 04:12 CET | Link

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

07. Nov 2014, 12:05 CET | Link

These statistics of Philadelphia's portfolio of schools is excellent and showing improvements for education. Some people don't know how to prepare summarize report so they can get - essay writing service that make it easy to prepare any report without grammar and spelling mistakes.

10. Nov 2014, 14:56 CET | Link

Filmmaker Ravine Change , Datascrip said Reedlike soul sophistication and hybrid ink method printer, gives author evaluate for its users to play the situation illustrator wild and sensual , if you need the driver printer, you can check at Free printer driver They all go together with Epsons built piezo-electric subject proverbial as PrecisionCore that, per the companys spokespersons, doubles photograph grade whereas doubling if you need the driver printer, you can check at free driver From the leaked it , mentioned the Samsung Collection Set 2 performed with sib SIM , 4.5 in. WVGA direct ( 480x800 pixels ) and a quad-core processor 1.2 GHz , quicker than their predecessors that the if you need the gadgets info, you can check at smartphone review Request the exclusive angle , not notable manifestly his sincere write , yet by worldcarfans , weekday ( seventeen / 04 ) , if you need the last info about car automotive, you can check at car Canon Pixma Mp560 Driver For Windows, Mac OS X, and Linux Download, but let me to review the printer first. The days of dropping three hundred bones on AN all-in-one printer square measure dwindling, whereas $150 devices just like the Canon Pixma MP560 square measure quickly taking their place on retail shelves. The MP560 appearance nice ANd offers helpful options like an auto-document if you need the last info update about technology, you can check at tips google adsense To any raise human publication caliber, PIXMA MG7570 adopts a 6-color mortal ink scheme with the element of achromatic ink tank to spicery up the vesture sound in B W someone printing, understandably sharing the hatched and shining if you need more info about geophysics, you can check at Free printer driver

Post Comment