软件艺术思考者  
混沌,彷徨,立志,蓄势...
公告
日历
<2006年8月>
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

导航

随笔分类(86)

随笔档案(85)

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 

这几天一直没有更新我的博客是因为我遇到了一个棘手的问题,这是我编程以来困扰我时间最长的一个问题:从周一下午一直到现在。如果哪位高手有这方面的经验,请不吝指教:
这是个关于hibernate方面的问题:
我的数据库关联较多,hbm.xml是用myeclipse自动生成的.dao是spring的,也是myeclipse自动生成的.在用hibernate向数据库插入数据的时候,第一次执行还比较顺利,但把同样的数据再次插入的时候,代码就走不动了.(主键是自增长的,插入时不设值),但当我重启tomcat后,第一次执行插入又能插入了,但第二次执行的时候又走不动了...就这样周而复始.
我测试后发现经常被卡住的语句是类似下面的语句:
(某类)beanDAO.findByExample(某类实例).get(0);
请各位抽空看看.谢谢.

大家可以凭经验来判断一下这个问题.不用看代码.因为hbm.mxl 和dao都是myeclipse自动生成的,我没有改.
而程序中经常出错就这句.(某类)beanDAO.findByExample(某类实例).get(0);


现在我对这个问题总算有点思路了。但还没实现,请高手指教。

posted on 2006-08-17 14:07 智者无疆 阅读(1423) 评论(8)  编辑  收藏 所属分类: about hibernate
评论:
  • # re: 最近遇到了hibernate的一个问题  智者无疆 Posted @ 2006-08-17 14:10
    注意出问题的这句是先从数据库里向外读一个数据,然后为插入其他表作准备。  回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  Robin's Java World Posted @ 2006-08-17 21:57
    你的DAO Bean是不是单例状态?如果是的话就改为非单态吧。  回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  Red Posted @ 2006-08-17 23:25
    由于 findByExample 操作,数据量过大有可能造成问题  回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  智者无疆 Posted @ 2006-08-18 09:17
    谢谢robin 和red的提示,我会把试验结果放在这儿。  回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  智者无疆 Posted @ 2006-08-18 09:43
    DAO改为非单态依然在第二次执行插入时不成功。依然是那个句子卡住了,下面我来想办法修理那个倒霉孩子.(某类)beanDAO.findByExample(某类实例).get(0);  回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  Binary Posted @ 2006-08-21 15:09
    事务关闭了吗?  回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  智者无疆 Posted @ 2006-08-22 15:55
    困扰了一个星期的错误,今天终于得以解决:
    1,我的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"">http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <servlet>
    <servlet-name>SpringContextServlet</servlet-name>
    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>



    <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>Fileupload</servlet-name>
    <servlet-class>com.real.zlj.common.Fileupload</servlet-class>
    </servlet>
    <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>DealExcel</servlet-name>
    <servlet-class>com.real.zlj.common.DealExcel</servlet-class>
    </servlet>
    <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>UploadExamPoins</servlet-name>
    <servlet-class>com.real.zlj.common.UploadExamPoins</servlet-class>
    </servlet>
    <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>DealExamPoins</servlet-name>
    <servlet-class>com.real.zlj.common.DealExamPoins</servlet-class>
    </servlet>



    <servlet-mapping>
    <servlet-name>Fileupload</servlet-name>
    <url-pattern>/fileupload</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>DealExcel</servlet-name>
    <url-pattern>/DealExcel</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>UploadExamPoins</servlet-name>
    <url-pattern>/UploadExamPoins</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-name>DealExamPoins</servlet-name>
    <url-pattern>/DealExamPoins</url-pattern>
    </servlet-mapping>




    </web-app>
    2。applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

    <beans>


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName">
    <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
    </property>
    <property name="url">
    <value>jdbc:microsoft:sqlserver://localhost:1433</value>
    </property>
    <property name="username">
    <value>sa</value>
    </property>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="dataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    </props>
    </property>




    <property name="mappingResources">
    <list>
    <value>com/real/zlj/hibernate/classes/UserInfo.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/StAttendance.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/FaAttendance.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/ExClass.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Syllabus.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Result.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Faculty.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Subject.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/ScGlory.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/ScTeacher.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/ScBulletin.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/ClassInfo.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Department.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Exam.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/School.hbm.xml</value>
    <value>com/real/zlj/hibernate/classes/Students.hbm.xml</value>
    </list>
    </property>
    </bean>

    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
    <bean id="myTransactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>
    <!--配置各个servicTarget-->
    <bean id="FacultyTarget" class="com.real.zlj.services.FacultyImp">
    <property name="facultyDao">
    <ref bean="FacultyDAO"/>
    </property>
    </bean>

    <bean id="ExClassTarget" class="com.real.zlj.services.ExClassImp">
    <property name="exClassDAO">
    <ref bean="ExClassDAO"/>
    </property>
    </bean>
    <bean id="ClassInfoTarget" class="com.real.zlj.services.ClassInfoImp">
    <property name="classDao">
    <ref bean="ClassInfoDAO"/>
    </property>
    </bean>
    <bean id="SchoolTarget" class="com.real.zlj.services.SchoolImp">
    <property name="schoolDao">
    <ref bean="SchoolDAO"/>
    </property>
    </bean>
    <bean id="StudentsTarget" class="com.real.zlj.services.StudentImp">
    <property name="studentDAO">
    <ref bean="StudentsDAO"/>
    </property>
    </bean>
    <bean id="ExamTarget" class="com.real.zlj.services.ExamImp">

    <property name="examDAO">
    <ref bean="ExamDAO"/>
    </property>
    </bean>
    <bean id="DepartTarget" class="com.real.zlj.services.DepartImp">
    <property name="departDAO">
    <ref local="DepartmentDAO"/>
    </property>
    </bean>

    <!--配置各个services ............... -->

    <bean id="FacultyService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="proxyInterfaces">
    <list>
    <value>com.real.zlj.services.IFaculty</value>
    </list>
    </property>
    <property name="target">
    <ref local="FacultyTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props >
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>


    <bean id="ExClassService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target">
    <ref local="ExClassTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="ClassInfoService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target">
    <ref local="ClassInfoTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="SchoolService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target">
    <ref local="SchoolTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="StudentsService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target">
    <ref local="StudentsTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    <bean id="ExamService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target">
    <ref local="ExamTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>

    <bean id="DepartService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="target">
    <ref local="DepartTarget"/>
    </property>
    <property name="transactionManager">
    <ref local="myTransactionManager"/>
    </property>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="save*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>
    <!--以下是各个DAO配置 -->
    <bean id="UserInfoDAO" class="com.real.zlj.hibernate.classes.UserInfoDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="StAttendanceDAO" class="com.real.zlj.hibernate.classes.StAttendanceDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="FaAttendanceDAO" class="com.real.zlj.hibernate.classes.FaAttendanceDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ExClassDAO" class="com.real.zlj.hibernate.classes.ExClassDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="SyllabusDAO" class="com.real.zlj.hibernate.classes.SyllabusDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ResultDAO" class="com.real.zlj.hibernate.classes.ResultDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="FacultyDAO" class="com.real.zlj.hibernate.classes.FacultyDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="SubjectDAO" class="com.real.zlj.hibernate.classes.SubjectDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ScGloryDAO" class="com.real.zlj.hibernate.classes.ScGloryDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ScTeacherDAO" class="com.real.zlj.hibernate.classes.ScTeacherDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ScBulletinDAO" class="com.real.zlj.hibernate.classes.ScBulletinDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ClassInfoDAO" class="com.real.zlj.hibernate.classes.ClassInfoDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="DepartmentDAO" class="com.real.zlj.hibernate.classes.DepartmentDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="ExamDAO" class="com.real.zlj.hibernate.classes.ExamDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="SchoolDAO" class="com.real.zlj.hibernate.classes.SchoolDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>
    <bean id="StudentsDAO" class="com.real.zlj.hibernate.classes.StudentsDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean>

    </beans>

    3.写service层的接口

    4。写service层的实现类。

    注意 (1)一定要继承它的接口。

    (2)写setXxDao()时,XxDao一定要和他的属性xxDao相对应。

    (3)findByExample(某类实例)特别好用,但findByName()经常查询失败。

    (4)别忘了老朋友:ServletContext sc = request.getSession().getServletContext();
    WebApplicationContext wac = WebApplicationContextUtils
    .getRequiredWebApplicationContext(sc);



    (5)poi的关键的几句:

    FileInputStream fis = new FileInputStream(fNew);
    POIFSFileSystem fs = new POIFSFileSystem(fis);
    HSSFWorkbook workbook = new HSSFWorkbook(fs);
    // 创建一个文件后,开始各个表单的循环
    int sheetNum = 2;
    out.print("此excel工作薄表单数为:"
    + workbook.getNumberOfSheets());
    out.print("<br/>");
    for (sheetNum = 1; sheetNum <= workbook.getNumberOfSheets(); sheetNum++) {
    HSSFSheet sheet = workbook.getSheet("Sheet" + sheetNum);
    out.println("<font color='red' >表单" + sheetNum+ " 开始处理...</font><br>");
    // 创建一个表单后,开始每行的循环*** 此处还要检查getLastRowNum()得到的意思
    for (int i = sheet.getFirstRowNum()+1; i <= sheet.getLastRowNum(); i++) {
    HSSFRow row = sheet.getRow(i);

    typeid = row.getCell((short) 0).getCellType();

      回复  更多评论   

  • # re: 最近遇到了hibernate的一个问题  xiaobai Posted @ 2006-08-31 15:52
    spring生成的dao的方法有时候不好用
    例如:用getById(id)不好用的时候,可以用getByExample试试。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 智者无疆 Powered by: 博客园 模板提供:沪江博客


   观音菩萨赞