栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

事务注释在Spring Boot中不起作用

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

事务注释在Spring Boot中不起作用

首先,您使用的是Spring Boot,然后使用Spring Boot,并让它自动为您配置东西。它将配置数据源,实体管理工厂,事务管理器等。

接下来,使用错误的事务管理器,使用JPA,因此应使用,

JpaTransactionManager
而不是
HibernateTransactionManager
已经为您配置的,您可以简单地为此删除Bean定义。

其次,您

hibernate.current_session_context_class
搞砸了正确的TX集成,请删除它。

使用自动配置

考虑到所有这些因素后,基本上可以将您的

Application
课程简化为以下内容。

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})@EntityScan("com.buhryn.interviewer.models")public class Application {    public static void main(String[] args) {        System.out.println("--------------------------- Start Application ---------------------------");        ApplicationContext ctx = SpringApplication.run(Application.class, args);    }    @Bean    public SessionFactory sessionFactory(EntityManagerFactory emf) {        if (emf.unwrap(SessionFactory.class) == null) { throw new NullPointerException("factory is not a hibernate factory");        }        return emf.unwrap(SessionFactory.class);    }}

接着添加

application.properties
src/main/resources
含有以下内容。

# DataSource configurationspring.datasource.driver-class-name=org.postgresql.Driverspring.datasource.username=postgresspring.datasource.password=postgresspring.datasource.url=jdbc:postgresql://localhost:5432/interviewer# General JPA propertiesspring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialectspring.jpa.show-sql=false# Hibernate Specific propertiesspring.jpa.properties.hibernate.format_sql=falsespring.jpa.hibernate.ddl-auto=create

这将正确配置数据源和JPA。

使用JPA代替普通的Hibernate

另一个技巧不是使用普通的hibernateAPI,而是简单地使用JPA,这样您就可以删除Bean

SessionFactory
。只需将dao更改为使用
EntityManager
而不是即可
SessionFactory

@Repositorypublic class CandidateDao implements ICandidateDao{    @PersistenceContext    private EntityManager em;    @Override    @Transactional    public CandidateModel create(CandidateDto candidate) {        CandidateModel candidateModel = new CandidateModel(candidate.getFirstName(), candidate.getLastName(), candidate.getEmail(), candidate.getPhone());        return em.persist(candidateModel);    }    @Override    public CandidateModel show(Long id) {        return new CandidateModel(     "new",     "new",     "new",     "new");    }    @Override    public CandidateModel update(Long id, CandidateDto candidate) {        return new CandidateModel(     "updated",     candidate.getLastName(),     candidate.getEmail(),     candidate.getPhone());    }    @Override    public void delete(Long id) {    }}

添加Spring Data JPA

而且,如果您真的想受益,请将Spring Data JPA添加到组合中,并完全删除DAO,仅保留一个接口。您现在拥有的将被移到服务类(它属于IMHO)。

整个仓库

public interface ICandidateDao extends JpaRepository<CandidateModel, Long> {}

修改后的服务(现在也应该是事务性的,并且所有业务逻辑都在该服务中)。

@Service@Transactionalpublic class CandidateService implements ICandidateService{    @Autowired    ICandidateDao candidateDao;    @Override    public CandidateModel create(CandidateDto candidate) {        CandidateModel candidateModel = new CandidateModel(candidate.getFirstName(), candidate.getLastName(), candidate.getEmail(), candidate.getPhone());        return candidateDao.save(candidate);    }    @Override    public CandidateModel show(Long id) {        return candidateDao.findOne(id);    }    @Override    public CandidateModel update(Long id, CandidateDto candidate) {        CandidateModel cm = candidateDao.findOne(id);        // Update values.        return candidateDao.save(cm);    }    @Override    public void delete(Long id) {        candidateDao.delete(id);    }}

现在,您还可以删除bean定义,以

SessionFactory
将您
Application
main
方法简化为一个方法。

@SpringBootApplication(exclude = {ErrorMvcAutoConfiguration.class})@EntityScan("com.buhryn.interviewer.models")public class Application {    public static void main(String[] args) {        System.out.println("--------------------------- Start Application ---------------------------");        ApplicationContext ctx = SpringApplication.run(Application.class, args);    }}

因此,我强烈建议您使用该框架,而不是尝试围绕该框架工作。这样可以真正简化开发人员的工作。

依存关系

最后一点,我建议从

spring-data-jpa
依赖项中删除依赖项,而改用启动程序。对于AspectJ,使用AOP启动器也是如此。另外,杰克逊1也不再受支持,因此添加该依赖项不会添加任何内容

dependencies {    compile("org.springframework.boot:spring-boot-starter-web")    compile("org.springframework.boot:spring-boot-starter-actuator")    compile("org.springframework.boot:spring-boot-starter-data-jpa")    compile("org.springframework.boot:spring-boot-starter-aop")    compile("com.google.pre.gson:gson:2.3.1")    compile("org.hibernate:hibernate-entitymanager:4.3.10.Final")    compile("postgresql:postgresql:9.1-901-1.jdbc4")    testCompile("org.springframework.boot:spring-boot-starter-test")}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/484106.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号