Spring Consolidation Hibernate encounters Beancreationexception__hibernate

Source: Internet
Author: User

Beginner spring, when consolidating hibernate in spring, you encounter the following exceptions:

Org.springframework.beans.factory.BeanCreationException:Error creating Bean with Name ' Bookdao ' defined in class path re SOURCE [Applicationcontext.xml]: Initialization of Bean failed; Nested exception is java.lang.IllegalArgumentException:sessionFactory or hibernatetemplate is required

I have defined a Bookdao class in the program, inherits the Hibernatedaosupport, uses the hibernatetemplate to carry on the deletion to the database data additions and deletions to check the operation.

public class Bookdao extends Hibernatedaosupport ... {

public void Addbook (book book) ... {
Gethibernatetemplate (). Save (book);
Gethibernatetemplate (). Flush ();
}

public void Delbook (book book) ... {
Gethibernatetemplate (). Delete (book);
Gethibernatetemplate (). Flush ();
}

public void Update (book book) ... {
Gethibernatetemplate (). Update (book);
Gethibernatetemplate (). Flush ();
}

Public book GetBook (Long oid) ... {
Return (book) gethibernatetemplate (). Get (Book.class, OID);

}

}

In addition, I define a class booksrv, which relies on Bookdao, and injects Bookdao objects using the dependency injection of the setter injection.

public class booksrv ... {

Private Bookdao Bdao=null;

public void Setbdao (Bookdao bdao) ... {
This.bdao = Bdao;
}

public void Add (book book) ... {
Bdao.addbook (book);
}

public void del (book book) ... {
Bdao.delbook (book);
}

public void Update (book book) ... {
Bdao.update (book);
}
Public book get (Long oid) ... {
Return Bdao.getbook (OID);
}
}

My original error profile is as follows (omit other parts):

< Bean id = "Bookdao" class = DAO. Bookdao ">
</Bean >

< Bean id = "BOOKSRV" class = srv. Booksrv ">
< property name = ' Bdao ' ref = ' Bookdao ' ></property >
</Bean >

Thought for a long time, also can not find unusual reason. And by that sentence sessionfactory or hibernatetemplate is required, has been in DataSource, Sessionfactory, Hibernatetemplate's configuration is desperately chasing the wrong place.

It's time to find out where the problem lies: Beancreationexception:error creating Bean with Name ' Bookdao ' ...

Because Bookdao inherits Hibernatedaosupport and uses Hibernatedaosupport's gethibernatetemplate () to get a Hibernatetemplate object, Then call the object's Save (), flush (), and other methods. Therefore, Bookdao relies on hibernatetemplate.

Then, adding Bookdao to Hibernatetemplate in the configuration file solves the problem.

The correct configuration file is:

< Bean id = "Bookdao" class = DAO. Bookdao ">
< property name = ' hibernatetemplate ' ref = ' hibernatetemplate ' ></property >
</Bean >

< Bean id = "BOOKSRV" class = srv. Booksrv ">
< property name = ' Bdao ' ref = ' Bookdao ' ></property >
</Bean >


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.