enes enes - 7 days ago 4
Java Question

How to create emdedded H2 DB with spring(transactional) and hibernate in java desktop application?

I am trying to create a project with embedded h2 db, and using spring framework with hibernate. My database will be created in initialize time if not exist. My development platform is intellij.

Problem is that when i run the application

@Autowired
private IPersonService personService; // comes null?


here is my classes and config files.

myDB.sql:

CREATE TABLE IF NOT EXISTS personel(
id IDENTITY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age VARCHAR(100));


hibernate.properties:

db.driverClassName=org.h2.Driver
db.url=jdbc:h2:~/h2SpringProject/database/SpringSample;mv_store=false;mvcc=false
db.username=admin
db.password=


here is my hibernate-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">



<context:component-scan base-package="com.springapp"/>
<context:annotation-config/>
<context:property-placeholder location="hibernate.properties"/>
<tx:annotation-driven transaction-manager="transactionManager"/>

<bean id="dataSource"
class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="suppressClose" value="true"/>
</bean>


<jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
<jdbc:script location="myDb.sql"/>
</jdbc:initialize-database>


<bean id="hibernateCfgProperties"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.use_sql_comments">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">
org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
</prop>
</props>
</property>
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties" ref="hibernateCfgProperties"/>
<property name="packagesToScan" value="com.springapp.model"/>
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

</beans>


Person class

@Entity
@Table(name = "personel")
public class Personel {

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

@Column(name = "name")
private String name;

@Column(name = "age")
private String age;
.......


An IPersonDao interface and here is implemented class

@Component
public class PersonelDaoImpl implements IPersonelDao {

@Autowired
private SessionFactory sessionFactory;


public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}

@Override
public void savePersonel(Personel personel) {
getCurrentSession().saveOrUpdate(personel);
}

@Override
public void deletePersonel(long id) {
getCurrentSession().delete(id);
}

@Override
public List<Personel> getPersonels() {
return getCurrentSession().createQuery("from Personel").list();
}
}


There is an IPersonService interface and here is implemented class

@Service("PersonelService")
public class PersonelServiceImpl implements IPersonelService {

@Autowired
private IPersonelDao personelDao;

@Override
@Transactional
public void savePersonel(Personel personel) {
personelDao.savePersonel(personel);
}

@Override
@Transactional
public void deletePersonel(long id) {
personelDao.deletePersonel(id);
}

@Override
@Transactional
public List<Personel> getPersonels() {
return personelDao.getPersonels();
}
}


here is my main class

public class MainApp {

private static ApplicationContext applicationContext;

public static void main(String[] args) {

applicationContext = new ClassPathXmlApplicationContext("hibernate-config.xml");

ForExample a = new ForExample();
a.execute();
}
}


@Component
public class ForExample {

@Autowired
private IPersonelService personelService;

public void execute(){
Personel p = new Personel();
p.setName("thats Ok!");
p.setAge("614345");

personelService.savePersonel(p);
}
}

Answer
public class MainApp {


        public static ApplicationContext applicationContext;

        private static IPersonelService personelService;


        public static void main(String[] args) {

            applicationContext = new ClassPathXmlApplicationContext("hibernate-config.xml");
            personelService = applicationContext.getBean(IPersonelService.class);

            Personel p = new Personel();
            p.setName("thatsOK!");
            p.setAge("614345");

            personelService.savePersonel(p);

        }
    }

Because of spring does not recognise new operator in run time..