使用hibernate和struts2插入多个表,使用一对一映射

时间:2013-12-18 05:26:35

标签: hibernate jsp struts2

我有5张桌子:

  • StudentPersonnelDetails
  • StudentContactDetails
  • StudentParentDetails
  • StudentQualificationDetails
  • StudentOtherDetails

此处StudentPersonnelDetails是以studentId为主键的父表,它是所有其他剩余表中的外键。

我使用的是Hibernate 4.2.1和Struts 2.1。

我在JSP中有一个表单,控件将所有学生的详细信息插入到相应的表中。

现在我的问题是我得到NullPointerException以下堆栈跟踪:

java.lang.NullPointerException
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:648)
    at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4527)
    at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:99)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:114)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
    at org.hibernate.internal.SessionImpl.fireSaveOrUpdate(SessionImpl.java:735)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:727)
    at org.hibernate.internal.SessionImpl.saveOrUpdate(SessionImpl.java:723)
    at com.dao.StudentDAO.saveOtherDetails(StudentDAO.java:133)
    at com.actions.StudentRegisterAction.otherDetails(StudentRegisterAction.java:125)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
    at com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
    at org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
    at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Dec 18, 2013 10:28:28 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: 'StudentOtherAction' in namespace: '/jsp'. Form action defaulting to 'action' attribute's literal value.
Dec 18, 2013 10:28:28 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: 'StudentOtherAction' in namespace: '/jsp'. Form action defaulting to 'action' attribute's literal value.
Dec 18, 2013 10:28:28 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: '' in namespace: '/jsp'. Form action defaulting to 'action' attribute's literal value.
Dec 18, 2013 10:28:28 AM com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
WARNING: No configuration found for the specified action: '' in namespace: '/jsp'. Form action defaulting to 'action' attribute's literal value.

所以我没有得到我错的地方。以下是相关的类和文件供您参考。

1)student.hbm.xml。

<class name="com.beans.student.StudentPersonnelDetails" table="SchoolStudentPersonnelDetails">

    <id name="sId" column="StudentId">
        <generator class="increment">

        </generator>

    </id>
    <property name="course" column="Course"></property>
    <property name="division" column="Division"></property>
    <property name="birthDate" column="BirthDate" type="java.util.Date"></property>
    <property name="admissionDate" column="AdmissionDate" type="java.util.Date"></property>
    <property name="fName" column="FirstName"></property>
    <property name="middleName" column="MiddleName"></property>
    <property name="lName" column="LastName"></property>
    <property name="gender" column="Gender"></property>
    <property name="nationality" column="Nationality"></property>
    <property name="bloodGroup" column="BloodGroup"></property>
    <property name="caste" column="Caste"></property>
    <property name="religion" column="Religion"></property>
    <property name="feeStructureFor" column="FeeStructureFor"></property>
    <property name="feeConcession" column="FeesConcession" type="java.lang.Boolean"></property>

    <one-to-one name="StudentContactDetails" class="com.beans.student.StudentContactDetails"  cascade="delete"></one-to-one>
     </class>

      <class name="com.beans.student.StudentContactDetails" table="SchoolStudentContactDetails">

        <id name="sId" column="StudentId">
            <generator class="foreign">
            <param name="property">StudentPersonnelDetails</param></generator>
        </id>

        <property name="residentialAddress" column="ResidentialAddress" ></property>
        <property name="permanentAddress" column="PermanentAddress"></property>
        <property name="resPerSame" column="ResPerAddressSame" type="java.lang.Boolean"></property>
        <property name="country" column="Country"></property>
        <property name="state" column="State"></property>
        <property name="city" column="City"></property>

        <one-to-one name="StudentPersonnelDetails" class="com.beans.student.StudentPersonnelDetails" constrained="true"></one-to-one>
     </class>

       <class name="com.beans.student.StudentParentDetails" table="SchoolStudentParentDetails">

     <id name="sId" column="StudentId">

        <generator class="foreign">
        <param name="property">StudentPersonnelDetails</param>
        </generator>

     </id>

     <property name="fatherName" column="FatherName"></property>
     <property name="motherName" column="MotherName"></property>
     <property name="fatherQualification" column="FatherQualification"></property>
     <property name="motherQualification" column="MotherQualification"></property>
     <property name="fatherOccupation" column="FatherOccupation"></property>
     <property name="motherOccupation" column="MotherOccupation"></property>
     <property name="fatherEmail" column="FatherEmail"></property>
     <property name="motherEmail" column="MotherEmail"></property>
     <property name="fatherMobileNo" column="FatherMobile"></property>
     <property name="motherMobileNo" column="MotherMobile"></property>

     <one-to-one name="StudentPersonnelDetails" class="com.beans.student.StudentPersonnelDetails" constrained="true"></one-to-one>

     </class>


     <class name="com.beans.student.StudentQualificationDetails" table="SchoolQualificationDetails">

     <id name="sId" column="StudentId">

     <generator class="foreign">
     <param name="property">StudentPersonnelDetails</param>
     </generator>

     </id>

     <property name="previousSchool" column="PreviousSchool"></property>
     <property name="schoolState" column="PreviousSchoolState"></property>
     <property name="schoolCity" column="PreviousSchoolCity"></property>
     <property name="percentage" column="PreviousClassPercentage" type="java.lang.Double"></property>

     <one-to-one name="StudentPersonnelDetails" class="com.beans.student.StudentPersonnelDetails" constrained="true"></one-to-one>

     </class>

     <class name="com.beans.student.StudentOtherDetails" table="SchoolStudentOtherDetails">

        <id name="sId" column="StudentId">

        <generator class="foreign">
        <param name="property"></param>
        </generator>     

        </id>

     <property name="hostelFacility" column="HostelFacility" type="java.lang.Boolean"></property>
     <property name="busFacility" column="BusFacility" type="java.lang.Boolean"></property>
     <property name="nearbyArea" column="NearByArea"></property>

     <one-to-one name="StudentPersonnelDetails" class="com.beans.student.StudentPersonnelDetails" constrained="true"></one-to-one>

     </class> 

2)StudentRegisterAction.java

public class StudentRegisterAction extends ActionSupport{

    private static final long serialVersionUID = 1L;
    StudentPersonnelDetails sp= new StudentPersonnelDetails();
    StudentContactDetails sc= new StudentContactDetails();
    StudentParentDetails sper= new StudentParentDetails();
    StudentQualificationDetails sq= new StudentQualificationDetails();
    StudentOtherDetails so= new StudentOtherDetails();

    StudentDAO sd=new StudentDAO();

    /*@Override
    public String execute() throws Exception {

        sc.setStudentPersonnelDetails(sp);
        sper.setStudentPersonnelDetails(sp);
        sq.setStudentPersonnelDetails(sp);
        so.setStudentPersonnelDetails(sp);
        boolean res=sd.registerStudent(sp, sc, sper, sq, so);
        if(res == true){

            System.out.println("Student Registered Successfully...!");
        }else{

            System.out.println("error in Registration...! Try Again!");
        }



    return SUCCESS;
    }*/



    public String personnelDetails() throws Exception{

        boolean res=sd.savePersonnelDetails(sp);
        if(res == true)
            return SUCCESS;
        else
            return ERROR;
    }

    public StudentPersonnelDetails getSp() {
        return sp;
    }

    public void setSp(StudentPersonnelDetails sp) {
        this.sp = sp;
    }

    public StudentContactDetails getSc() {
        return sc;
    }

    public void setSc(StudentContactDetails sc) {
        this.sc = sc;
    }

    public StudentParentDetails getSper() {
        return sper;
    }

    public void setSper(StudentParentDetails sper) {
        this.sper = sper;
    }

    public StudentQualificationDetails getSq() {
        return sq;
    }

    public void setSq(StudentQualificationDetails sq) {
        this.sq = sq;
    }

    public StudentOtherDetails getSo() {
        return so;
    }

    public void setSo(StudentOtherDetails so) {
        this.so = so;
    }

    public String contactDetails() throws Exception{

        boolean res=sd.saveContactDetails(sc);
        if(res == true)
            return SUCCESS;
        else
            return ERROR;

    }

    public String parentDetails() throws Exception{

        boolean res=sd.saveParentDetails(sper);
        if(res == true)
            return SUCCESS;
        else
            return ERROR;

    }

    public String qualificationDetails() throws Exception{

        boolean res=sd.saveQualificationDetails(sq);
        if(res == true)
            return SUCCESS;
        else
            return ERROR;
    }

    public String otherDetails() throws Exception{

        boolean res=sd.saveOtherDetails(so);
        if(res == true)
            return SUCCESS;
        else
            return ERROR;
    }

}

3)StudentDAO.java

package com.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.beans.student.StudentContactDetails;
import com.beans.student.StudentOtherDetails;
import com.beans.student.StudentParentDetails;
import com.beans.student.StudentPersonnelDetails;
import com.beans.student.StudentQualificationDetails;
import com.dbutil.HibernateUtil;

public class StudentDAO {



    /*public boolean registerStudent(StudentPersonnelDetails sp,StudentContactDetails sc,StudentParentDetails sparent,StudentQualificationDetails sq,StudentOtherDetails so){

        Session session=null;
        Transaction tr=null;
        System.out.println("in registerStudent.....");
        try{
            session=HibernateUtil.getSession();
            tr=session.beginTransaction();

            session.saveOrUpdate(sp);
            session.saveOrUpdate(sc);
            session.saveOrUpdate(sparent);
            session.saveOrUpdate(sq);
            session.saveOrUpdate(so);

            tr.commit();
            return true;
        }catch(Exception e){
            e.printStackTrace();

            tr.rollback();
        }
        return false;
    }*/


    public boolean savePersonnelDetails(StudentPersonnelDetails sp){

        Session session=null;
        Transaction tr=null;

        try{
            session=HibernateUtil.getSession();
            tr=session.beginTransaction();

            session.saveOrUpdate(sp);
            tr.commit();

        }catch(Exception e){
            e.printStackTrace();
            tr.rollback();
        }


        return false;
    }


    public boolean saveContactDetails(StudentContactDetails sc){

        Session session=null;
        Transaction tr=null;

        try{
            session=HibernateUtil.getSession();
            tr=session.beginTransaction();

            session.saveOrUpdate(sc);
            tr.commit();

        }catch(Exception e){
            e.printStackTrace();
            tr.rollback();
        }

        return false;
    }

    public boolean saveParentDetails(StudentParentDetails sparent){

        Session session=null;
        Transaction tr=null;

        try{
            session=HibernateUtil.getSession();
            tr=session.beginTransaction();

            session.saveOrUpdate(sparent);
            tr.commit();

        }catch(Exception e){
            e.printStackTrace();
            tr.rollback();
        }

        return false;
    }

    public boolean saveQualificationDetails(StudentQualificationDetails sq){

        Session session=null;
        Transaction tr=null;

        try{
            session=HibernateUtil.getSession();
            tr=session.beginTransaction();

            session.saveOrUpdate(sq);
            tr.commit();

        }catch(Exception e){
            e.printStackTrace();
            tr.rollback();
        }

        return false;       
    }

    public boolean saveOtherDetails(StudentOtherDetails so){
        Session session=null;
        Transaction tr=null;

        try{
            session=HibernateUtil.getSession();
            tr=session.beginTransaction();

            session.saveOrUpdate(so);
            tr.commit();

        }catch(Exception e){
            e.printStackTrace();
            tr.rollback();
        }

        return false;
    }
}

所有细节都是Bean Classes。

0 个答案:

没有答案