Refer to 《hibernate_reference%3.3.1 to learn the first example. Some strange problems are encountered.
1. org. hibernate. hibernateexception: No currentsessioncontext configured!
Cause: Session session = hibernateutil. getsessionfactory (). getcurrentsession ();
This line of code has a problem. modify it
Session session = hibernateutil. getsessionfactory (). opensession ();
The actual reason is unknown. Why is the initial quantity of currentsession not defined? How to define it?
2. java. Lang. classnotfoundexception: org. slf4j. impl. staticloggerbinder
Add all the jar files and hibernate files under the hibernate-distribution-3.3.1.GA/lib/required directory to build path or report classnotfoundexception error !!!
Solution: also download hibernate-entitymanager-3.4.0.ga.zip to add the hibernate-entitymanager-3.4.0.GA under the slf4j-log4j12.jar/lib/test directory to build path.
Comment: Do you want this?
3. avax. Naming. noinitialcontextexception need to specify class name in environment or system property or as an applet parameter or in an application resource file java. Naming. Factory. Initial
Solution: Modify the hibernate. cfg. xml file and remove the red part.
<Session-factory name = "sessionfactory">
<Property name = "connection. driver_class">
Com. MySQL. JDBC. Driver
</Property>
<Property name = "hibernate. Connection. url">
JDBC: mysql: // localhost: 3306/test
</Property>
<Property name = "hibernate. Connection. username"> root </property>
<Property name = "hibernate. Connection. Password"> root </property>
<Property name = "hibernate. dialect">
Org. hibernate. dialect. mysqlinnodbdialect
</Property>
<! -- Echo all executed SQL to stdout -->
<Property name = "show_ SQL"> true </property>
<! -- Drop and re-create the database schema on startup -->
<Property name = "hbm2ddl. Auto"> Create </property>
<Mapping Resource = "events/event. HBM. xml"/>
</Session-factory>
Cause: With one more name attribute, Hibernate will try to register this sessionfacotry to the jndi. This error occurs.
4. When a new record is inserted to hibernate, the old record of MySQL will be overwritten.
Delete line code
<Property name = "hbm2ddl. Auto"> Create </property>
Hibernate. cfg. XML file. usually you only leave it turned on in continuous unit testing, but another run of hbm2ddl wocould drop everything you have stored-the create Configuration Setting actually translates into "Drop all tables from the schema, then re-create all tables, when the sessionfactory is build ".