java - Spring-Hibernate integration : Unable to persist -
i'm trying use hibernate in spring framework. following files :
employee.java :
package com.springstarter; public class employee { private int id; private string name; private float salary; public employee() { } public employee( int id, string name, float salary ) { super(); this.id = id; this.name = name; this.salary = salary; } public int getid() { return id; } public void setid( int id ) { this.id = id; } public string getname() { return name; } public void setname( string name ) { this.name = name; } public float getsalary() { return salary; } public void setsalary( float salary ) { this.salary = salary; } } employeedao.java package com.springstarter; import java.util.arraylist; import java.util.list; import org.springframework.orm.hibernate3.hibernatetemplate; public class employeedao { hibernatetemplate template; public void settemplate( hibernatetemplate template ) { this.template = template; } public void saveemployee( employee e ) { template.save( e ); } public void updateemployee( employee e ) { template.update( e ); } public void deleteemployee( employee e ) { template.delete( e ); } public employee getbyid( int id ) { employee e = ( employee ) template.get( employee.class, id ); return e; } public list<employee> getemployees() { list<employee> list = new arraylist<employee>(); list = template.loadall( employee.class ); return list; } }
driver class :
package com.springstarter; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class springormapp { public static void main( string[] args ) { applicationcontext ctx = new classpathxmlapplicationcontext( "springstarter.xml" ); employeedao dao = ( employeedao ) ctx.getbean( "edao" ); system.out.println( ( ( employee ) dao.getbyid( 1 ) ).getname() ); } }
beans xml file :
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> <property name="driverclassname" value="oracle.jdbc.oracledriver" /> <property name="url" value="jdbc:oracle:thin:@[10.113.49.82]:1521:roc12c" /> <property name="username" value="milli_sec" /> <property name="password" value="milli_sec" /> </bean> <bean id="mysessionfactory" class="org.springframework.orm.hibernate4.localsessionfactorybean"> <property name="datasource" ref="datasource"></property> <property name="mappingresources"> <list> <value>employee.hbm.xml</value> </list> </property> <property name="hibernateproperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.oracle9dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean> <bean id="template" class="org.springframework.orm.hibernate3.hibernatetemplate"> <property name="sessionfactory" ref="mysessionfactory"></property> </bean> <bean id="edao" class="com.springstarter.employeedao"> <property name="template" ref="template"></property> </bean> </beans>
employee.hbm.xml file
<?xml version='1.0' encoding='utf-8'?> <!doctype hibernate-mapping public "-//hibernate/hibernate mapping dtd 3.0//en" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.springstarter.employee" table="employee"> <id name="id"> <generator class="assigned"></generator> </id> <property name="name"></property> <property name="salary"></property> </class> </hibernate-mapping>
no compilation issues.but expecting employee object of id 1. following logs.
info: hhh000397: using astquerytranslatorfactory jan 28, 2016 4:28:15 pm org.hibernate.tool.hbm2ddl.schemaupdate execute info: hhh000228: running hbm2ddl schema update jan 28, 2016 4:28:15 pm org.hibernate.tool.hbm2ddl.schemaupdate execute info: hhh000102: fetching database metadata jan 28, 2016 4:28:16 pm org.hibernate.tool.hbm2ddl.schemaupdate execute info: hhh000396: updating schema jan 28, 2016 4:28:46 pm org.hibernate.tool.hbm2ddl.tablemetadata <init> info: hhh000261: table found: milli_sec.employee jan 28, 2016 4:28:46 pm org.hibernate.tool.hbm2ddl.tablemetadata <init> info: hhh000037: columns: [id, name, salary] jan 28, 2016 4:28:46 pm org.hibernate.tool.hbm2ddl.tablemetadata <init> info: hhh000108: foreign keys: [] jan 28, 2016 4:28:46 pm org.hibernate.tool.hbm2ddl.tablemetadata <init> info: hhh000126: indexes: [employee_pk] jan 28, 2016 4:28:46 pm org.hibernate.tool.hbm2ddl.schemaupdate execute info: hhh000232: schema update complete
i think, main issue don't use transactions. reading data hibernate need transactions too. need extend springstarter.xml
configuration transaction manager. can use @transactional
annotation or add transactions methods methods masks in springstarter.xml
.
and there problems in code
org.springframework.orm.hibernate3.hibernatetemplate
should be
org.springframework.orm.hibernate4.hibernatetemplate
public list<employee> getemployees() { list<employee> list = new arraylist<employee>(); list = template.loadall( employee.class ); return list; }
should
public list<employee> getemployees() { return template.loadall( employee.class ); }
and
hibernatetemplate template;
should be
private hibernatetemplate template;
Comments
Post a Comment