Hibernate 5.X version exception in thread "main" Org.hibernate.MappingException:Unknown ENTITY:CN.XXX.P

Source: Internet
Author: User

Recently started using the latest version of Hibernate5, all configurations feel right but still no results.

Use the following:

public static void Main (string[] args) throws Exception {configuration cfg = new Configuration (). Configure (); Serviceregistry serviceregistry = new Standardserviceregistrybuilder (). Applysettings (Cfg.getproperties ()). build (); Sessionfactory SF = cfg.buildsessionfactory (serviceregistry); Session session = Sf.opensession (); Transaction Transaction = Session.begintransaction (); User user = new user (), user.setname ("xxx"), User.setpassword ("pswdddd"); Session.save (user); Transaction.commit (); Session.close (); Sf.close ();}

Running results always error:

Exception in thread "main" Org.hibernate.MappingException:Unknown Entity:cn.xxx.pojo.Userat Org.hibernate.internal.SessionFactoryImpl.getEntityPersister (sessionfactoryimpl.java:776) at Org.hibernate.internal.SessionImpl.getEntityPersister (sessionimpl.java:1533) at Org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId (abstractsaveeventlistener.java:104) At Org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId ( defaultsaveorupdateeventlistener.java:192) at Org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId ( DEFAULTSAVEEVENTLISTENER.JAVA:38) at Org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient ( defaultsaveorupdateeventlistener.java:177) at Org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate (defaultsaveeventlistener.java:32) at Org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate ( defaultsaveorupdateeventlistener.java:73) at Org.hibErnate.internal.SessionImpl.fireSave (sessionimpl.java:682) at Org.hibernate.internal.SessionImpl.save ( sessionimpl.java:674) at Org.hibernate.internal.SessionImpl.save (sessionimpl.java:669) at Cn.xxx.test.Test.main ( TEST.JAVA:24)
After looking at the official documentation, Hibernate 5 enables configuration differently from the original, and should be correct as follows:

It was copied directly from the official document.

public static void SetUp () throws Exception {//A sessionfactory are set up once for an application! Standardserviceregistry Registry = new Standardserviceregistrybuilder (). Configure ()//Configures settings from Hibernate.cfg.xml.build (); try {sessionfactory sf = new Metadatasources (registry). Buildmetadata (). Buildsessionfactory (); Session session = Sf.opensession (); Transaction Transaction = Session.begintransaction (); User user = new User ("Smile Bear"), User.setpassword ("0719"), Session.save (user), Transaction.commit (); user.setname; Session.close (); Sf.close ();} catch (Exception e) {Standardserviceregistrybuilder.destroy (registry); E.printstacktrace ();}}

Finally the operation succeeded:



Hibernate 5.X version exception in thread "main" Org.hibernate.MappingException:Unknown ENTITY:CN.XXX.P

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.