I have created following example . But I am not getting the expected result. Here is my code :
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();
}
}
when I run AppLaunch.java class . following execption occurs :
Exception in thread "main" org.hibernate.HibernateException: persist is not valid without active transaction
here is my 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>
What I am not doing right, here ?
Here is complete stack trace :
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)
try to remove this property and run again.
hp.put("hibernate.current_session_context_class", "thread");