Hibernate Errors for beginners

Source: Internet
Author: User

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 ".

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.