myeclipse で hibernate 3.1 の整数化を使用して spring 3 を使用しています。問題は、「学生」インスタンスがエラーまたは例外なくデータベースに保存、更新、または削除されないことですが、findbyid などの他の機能やすべてが正しく機能することです。
これが私のものapplicationcontext.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.1.xsd">
<bean id="hibernateSession"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="file:src/hibernate.cfg.xml">
</property>
</bean>
<bean id="StudentDAO"
class="com.myeclipse.hibernatespring.StudentDAO">
<property name="sessionFactory">
<ref bean="hibernateSession" />
</property>
</bean>
<bean id="persistenceLayer"
class="com.myeclipse.hibernatespring.PersistenceLayer"
abstract="false" lazy-init="default" autowire="default"
p:studentDAO-ref="StudentDAO">
</bean></beans>
私persistenceLayer
のクラス:
package com.myeclipse.hibernatespring;
public class PersistenceLayer {
private StudentDAO studentDAO;
public StudentDAO getStudentDAO() {
return studentDAO;
}
public void setStudentDAO(StudentDAO studentDAO) {
this.studentDAO = studentDAO;
}
public void addStudent(Student st){
studentDAO.save(st);
}
public Student findStudentById(Integer id){
return studentDAO.findById(id);
}
}
私buisnesslogic
のクラス:
package com.myeclipse.hibernatespring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BuisnessLogic {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Student stu=new Student(4,"gaurav",67.7f);
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
PersistenceLayer pl=(PersistenceLayer) ctx.getBean("persistenceLayer");
pl.addStudent(stu);
Student st=pl.findStudentById(2);
System.out.print(st.getName());
}
}
私のhibernate.cfg:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/school
</property>
<property name="connection.username">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="myeclipse.connection.profile">
MySQL Connector/J
</property>
<mapping
resource="com/myeclipse/hibernatespring/Student.hbm.xml" />
</session-factory>
</hibernate-configuration>