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


i trying create project embedded h2 db, , using spring framework hibernate. database created in initialize time if not exist. development platform intellij.

problem when run application

@autowired private ipersonservice personservice; // comes null? 

here classes , 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 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 , here 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 ipersonservice interface , here 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 main class

public class mainapp {      private static applicationcontext applicationcontext;             public static void main(string[] args) {          applicationcontext = new classpathxmlapplicationcontext("hibernate-config.xml");      forexample = 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);     } } 

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 not recognise new operator in run time..


Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -