PackageCom.ehcache;Importjava.io.Serializable; Public classUserImplementsserializable{Private intID; PrivateString name; PublicUser () {} Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } @Override PublicString toString () {return"User [id=" + ID + ", name=" + name + "]"; } }
<?XML version= "1.0"?><!DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/ Dtd/hibernate-mapping-3.0.dtd "><hibernate-mapping Package= "Com.ehcache"> <!--Name property: Which Class Table property: Which table, if not, the default table name is the simple name of the class - <classname= "User"Table= "Tt_user" > <Cacheusage= "Read-write" /> <IDname= "id"> <Generatorclass= "Native" /> </ID> < Propertyname= "Name"Not-null= "true" /> </class></hibernate-mapping>
@Test publicvoid test2 () { = sessionfactory.opensession (); Session.begintransaction (); = Session.load (User. Class, 1);
//This Side note the session closes after the print U1 will error no session //Hibernate.initialize (U1); Session.gettransaction (). commit (); Session.close (); System.out.println (U1); // return C1; }
Lazy initialization results for hibernate