Annotated way to inject org.hibernate.SessionFactory into a custom DAO

Source: Internet
Author: User

When the custom DAO implements the Org.springframework.orm.hibernate3.support.HibernateDaoSupport, if DAO is to provide the bean in the form of annotations, The sessionfactory is injected in the form of annotations, and the associated set method in Hibernatedaosupport is final, that is, it cannot be overwritten:

Public final void Setsessionfactory (Sessionfactory sessionfactory) {
		if (this.hibernatetemplate = = NULL | | Sessionfactory! = This.hibernateTemplate.getSessionFactory ()) {
			this.hibernatetemplate = Createhibernatetemplate (sessionfactory);
		}
	}

Public final void Sethibernatetemplate (Hibernatetemplate hibernatetemplate) {
		this.hibernatetemplate = hibernatetemplate;
	}


Workaround:
@Transactional
@Repository ("Baselocationdao") public
class Baselocationdaoimpl extends Hibernatedaosupport Implements locationdao{
	private Logger Logger = Loggerfactory.getlogger (baselocationdaoimpl.class);
	
	@Resource public
	void Setmysessionfactory (Sessionfactory sessionfactory) {
		super.setsessionfactory ( sessionfactory);
	}
}



the Sessionfactory implementation class should also be configured as an annotated form:

	<bean id= "Sessionfactory" class= "Org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
		<property name= "DataSource" ref= "DataSource"/>
		<property name= "Packagestoscan" value= " Com.pbn.oss.location "></property>
		<property name=" Schemaupdate "value=" true "/>
		< Property name= "Hibernateproperties" >
			<props>
				<prop key= "Hibernate.format_sql" >true</ prop>
				<prop key= "Hibernate.show_sql" >true</prop>
				<prop key= "Hibernate.dialect" > org.hibernate.dialect.sqlserverdialect</prop>
				<prop key= "Hibernate.query.factory_class" > org.hibernate.hql.ast.astquerytranslatorfactory</prop>
			</props>
		</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.