我试图在spring框架中使用hibernate。 以下是我的文件:
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;
}
}
驱动程序类:
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文件:
<?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文件
<?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>
没有编译问题。但是期望id为1的员工对象。 以下是日志。
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
答案 0 :(得分:1)
我认为,您不使用任何交易的主要问题。要使用Hibernate读取数据,您还需要进行事务处理。因此,您需要使用事务管理器扩展p:nth-child(3)
{
margin: 0 50px;
}
配置。您可以使用SpringStarter.xml
注释或通过@Transactional
中的方法掩码向方法添加事务。
您的代码中存在一些问题
SpringStarter.xml
应该是
org.springframework.orm.hibernate3.HibernateTemplate
org.springframework.orm.hibernate4.HibernateTemplate
应该是
public List<Employee> getEmployees()
{
List<Employee> list = new ArrayList<Employee>();
list = template.loadAll( Employee.class );
return list;
}
和
public List<Employee> getEmployees()
{
return template.loadAll( Employee.class );
}
应该是
HibernateTemplate template;