gpt4 book ai didi

java - @Transactional 不在独立应用程序中启动事务

转载 作者:行者123 更新时间:2023-12-02 13:26:00 26 4
gpt4 key购买 nike

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

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 类时。发生以下执行:

Exception in thread "main" org.hibernate.HibernateException: persist is not valid without active transaction

这是我的 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>

我在这里做错了什么?

这是完整的堆栈跟踪:

Apr 12, 2017 8:16:35 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@fa5d3: startup date [Wed Apr 12 20:16:35 IST 2017]; root of context hierarchy Apr 12, 2017 8:16:36 PM org.springframework.jdbc.datasource.DriverManagerDataSource setDriverClassName INFO: Loaded JDBC driver: org.hsqldb.jdbcDriver Apr 12, 2017 8:16:36 PM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {5.2.9.Final} Apr 12, 2017 8:16:36 PM org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found Apr 12, 2017 8:16:37 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} Exception in thread "main" org.hibernate.HibernateException: persist is not valid without active transaction at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:351) at com.sun.proxy.$Proxy27.persist(Unknown Source) 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() at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656) at com.orbit.services.UserService$$EnhancerBySpringCGLIB$$15a51388.save() at com.orbit.main.LaunchApp.main(LaunchApp.java:16)

最佳答案

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

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

关于java - @Transactional 不在独立应用程序中启动事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43370337/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com