@Transactional未在独立应用程序

时间:2017-04-12 13:01:58

标签: java hibernate spring-jdbc spring-transactions spring-java-config

我创建了以下示例。但我没有得到预期的结果。 这是我的代码:

1.application.properties

################### JDBC Configuration ##########################
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:file:db/hsqldb;shutdown=true
jdbc.username=root
jdbc.password=


################### Hibernate Configuration ##########################
hibernate.dialect=org.hibernate.dialect.HSQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
hibernate.generate_statistics=true

2。 DatabaseConfig.java

/**
 * 
 * load Hibernate Session factory to get session ,load Hibernate Transaction
 * Manager to deal with transaction
 * 
 */

@Configuration
@EnableTransactionManagement
public class DatabaseConfig {

    @Value("${jdbc.driverClassName}")
    private String driverClass;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Value("${hibernate.dialect}")
    private String dialect;
    @Value("${hibernate.show_sql}")
    private String showSQL;
    @Value("${hibernate.hbm2ddl.auto}")
    private String hbm2ddl;

    @Bean
    public Properties hibernateProperties() {

        Properties hp = new Properties();
        hp.put("hibernate.dialect", dialect);
        hp.put("hibernate.show_sql", showSQL);
        hp.put("hibernate.hbm2ddl.auto", hbm2ddl);
        hp.put("hibernate.current_session_context_class", "thread");
        return hp;

    }

    @Bean
    public DataSource driverManagerDataSource() {
        DriverManagerDataSource ds = new DriverManagerDataSource();
        ds.setDriverClassName(driverClass);
        ds.setUrl(url);
        ds.setUsername(username);
        ds.setPassword(password);
        return ds;
    }

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
        sessionFactoryBean.setDataSource(driverManagerDataSource());
        sessionFactoryBean.setHibernateProperties(hibernateProperties());
        sessionFactoryBean.setPackagesToScan(new String[] { "com.orbit.hibernate.entities" });

        return sessionFactoryBean;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
        HibernateTransactionManager tm = new HibernateTransactionManager(sessionFactory);
        return tm;

    }

}

第3。 AppConfig.java

/**
 * 
 * This class has all the beans required at application level
 * 
 */
@Configuration
@Import(DatabaseConfig.class)
@ComponentScan("com.orbit")
public class AppConfig {

    @Bean
    public static PropertyPlaceholderConfigurer propertyPlaceHolderConfigurer() {
        PropertyPlaceholderConfigurer pphc = new PropertyPlaceholderConfigurer();
        pphc.setLocation(new ClassPathResource("application.properties"));
        pphc.setIgnoreUnresolvablePlaceholders(true);
        return pphc;
    }
}

4.User.java

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    private String name;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

5。 UserRepository.java

@Repository
public class UserRepository {

    @Autowired
    private SessionFactory sessionFactory;

    public void save() {
        User u = new User();
        String random = UUID.randomUUID().toString();
        u.setName("mks" + random);
        Session session = sessionFactory.getCurrentSession();
        session.persist(u);

    }

    @SuppressWarnings("unchecked")
    public List<User> getAll() {
        Session session = sessionFactory.getCurrentSession();
        Query q = session.createQuery("FROM User");
        List<User> users = q.getResultList();
        return users;
    }
}

6。 UserService.java

@Service
@Transactional
public class UserService {

    @Autowired
    private UserRepository userRepo;

    public List<User> getAllUser() {
        return userRepo.getAll();
    }

    public void save() {
        userRepo.save();
    }
}

7。 AppLaunch.java

public class LaunchApp {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
        UserService userService = ctx.getBean(UserService.class);
        userService.save();
        userService.save();
        System.out.println("Printing all user");
        List<User> user = userService.getAllUser();
        user.forEach(System.out::println);
        ctx.close();
    }
}

当我运行AppLaunch.java类时。发生以下执行:

  

线程中的异常&#34; main&#34; org.hibernate.HibernateException:没有活动事务,persist无效

这是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.orbit</groupId>
    <artifactId>spring.hibernate.java.config</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring.hibernate.java.config</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.2.9.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>2.3.4</version>
        </dependency>
    </dependencies>
</project>

我在做什么,这里?

这是完整的堆栈跟踪:

  

2017年4月12日下午8:16​​:35   org.springframework.context.annotation.AnnotationConfigApplicationContext   prepareRefresh INFO:刷新   org.springframework.context.annotation.AnnotationConfigApplicationContext@fa5d3:   启动日期[Wed Apr 12 20:16:35 IST 2017];上下文层次结构的根   2017年4月12日下午8:16​​:36   org.springframework.jdbc.datasource.DriverManagerDataSource   setDriverClassName INFO:已加载的JDBC驱动程序:org.hsqldb.jdbcDriver Apr   12,2017 8:16:36 PM org.hibernate.Version logVersion信息:HHH000412:   Hibernate Core {5.2.9.Final} 2017年4月12日下午8:16​​:36   org.hibernate.cfg.Environment信息:HHH000206:   hibernate.properties未找到2017年4月12日下午8:16​​:37   org.hibernate.annotations.common.reflection.java.JavaReflectionManager    信息:HCANN000001:Hibernate Commons注释   {5.0.1.Final}线程&#34; main&#34;中的异常   org.hibernate.HibernateException:如果没有活动,则persist无效   交易在   org.hibernate.context.internal.ThreadLocalSessionContext $ TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:351)     在com.sun.proxy。$ Proxy27.persist(未知来源)at   com.orbit.repositories.UserRepository.save(UserRepository.java:26)at   com.orbit.services.UserService.save(UserService.java:25)at   com.orbit.services.UserService $$ FastClassBySpringCGLIB $$ 6492a26b.invoke()     在   org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)     在   org.springframework.aop.framework.CglibAopProxy $ CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     在   org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)     在   org.springframework.aop.framework.CglibAopProxy $ DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656)     在   com.orbit.services.UserService $$ EnhancerBySpringCGLIB $$ 15a51388.save()     在com.orbit.main.LaunchApp.main(LaunchApp.java:16)

2 个答案:

答案 0 :(得分:1)

尝试删除此属性并再次运行。

hp.put("hibernate.current_session_context_class", "thread");

答案 1 :(得分:0)

您需要在spring transaction document https://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#testing-tx

中指定事务管理器

另请注意编辑save方法并添加beginTransaction,如下所示:

    public void save() {
        User u = new User();
        String random = UUID.randomUUID().toString();
        u.setName("mks" + random);
        Session session = getSessionFactory().getCurrentSession();
        Transaction trans = session.beginTransaction();
        session.persist(u);
        trans.commit();

    }