Current location - Quotes Website - Signature design - Spring’s transaction integration with hibernate keeps getting errors
Spring’s transaction integration with hibernate keeps getting errors

SaUserDAO userdao=(SaUserDAO)context.getBean("userDAOProxy");

context.getBean("userDAOProxy"); means taking out id=userDAOProxy from the spring environment context Object. Then force convert this object to SaUserDAO.

You change it to

SaUserDAO userdao=(SaUserDAO)context.getBean("SaUserDAO");

Is it okay if you force-convert the userDAOProxy object into SaUserDAO?

The key is to understand what spring does

Has your configuration file sessionFactory been configured?

SPRING is an IOC container. To put it simply, we usually NEW an object.

With SPRING, after configuring the object in SPRING, we can take out those objects from the SPRING container. Use it directly. It is also called injection.

For example: when you are in class A and want to call a method of class B, you usually create a new instance of B.

Use SPRING If so, just add an attribute B b to A; and then configure the attribute b of A in the configuration file.

< /p>

The b here is the value of id=b above

About SPRING's proxy< /p>

AOP proxy

Spring uses J2SE dynamic proxies (dynamic proxies) as the AOP proxy by default. This way any interface (or set of interfaces) can be proxied.

Spring can also use CGLIB proxies. CGLIB proxies are necessary when proxy classes rather than proxy interfaces are required. If a business object does not implement an interface, CGLIB will be used by default.

To put it simply, if your class does not implement the interface, it must be forced to use CGLIB for proxy. If the class implements the interface, it will use JDK’s dynamic proxy mechanism by default to implement AOP.

Force to use CGLIB to generate Proxy.

1 Add it to the spring configuration file

2 Add CGLIB Library

spring/lib/cglib/*.jar

Spring’s transaction management is also AOP

I recommend you a good video

/topics/93279/

I copied a configuration file that I used when studying

Integration of Spring and hibernate

To control transactions, just You have to get hibernate's session. How to obtain hibernate's session: sessionfactory.sessionfactory creates configuration, and the configuration is obtained from the configuration file. Spring integrates Hibernate mainly to manage hibernate's Session, including the creation, submission, and closing of Session. the entire life cycle. The session here can be regarded as a connection.

Declarative transaction configuration

1 Configure session factory

2 Configure transaction manager

< p>3 Configure the propagation characteristics of transactions

4 Configure which methods use transactions

Configure sessionFactory and let spring to create Session.

classpath:hibernate.cfg.xmlhibernate configuration file

class=" org.springframework.orm.hibernate3.LocalSessionFactoryBean"//Only suitable for xml method

class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean//Compatible annotations

Take the sessionFactory and put it in the transaction manager. Create a transaction manager with the id transactionManager, which matches a session factory, This sessionFactory refers to the ID of the session factory.

id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

An advice (notification) is created here to set up transactions for the transaction manager. This means applying transactions to methods starting with save, del, and update.

< /p>

The function of configuration is to apply the advice we created above to specific classes.

The following code means to apply the allManagerMethod() method to all methods of all classes under package com.spring.

All methods of all classes under the com.spring package

advisor can be understood as an aspect, and an aspect generally has pointcut and advice.

This is the most important thing for you to learn It’s easy to understand the principles of SPRING

IOC, AOP and other concepts and principles