Hibernate Association

时间:2012-01-07 12:54:53

标签: hibernate

我正在使用hibernate。我有两张桌子:工作和公司。

Company.hbm.xml是:

<class name="com.jobs.admin.data.Company" table="S_COMPANY">
    <id name="companyKid" column="COMPANY_KID" type="integer">
        <generator class="sequence">
            <param name="sequence">company_sequence</param>
        </generator>
    </id>
    <property name="companyName" column="COMPANY_NAME" length="200" type="string" unique="true"/>
    <property name="companyDesc" column="COMPANY_DESC" type="string" length="1000" unique="true"  />
    <property name="companySName" column="COMPANY_SNAME" type="string" length="100"/>
    <property name="companyMail" column="COMPANY_MAIL" type="string" length="200"/>
    <property name="companyPhone" column="COMPANY_PHONE" type="string" length="200"/>
</class>

我的Job.hbm.xml是:

<?xml version="1.0"?>
<!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.jobs.admin.data.Job" table="S_JOBS">
        <id name="jobKid" column="JOB_KID" type="integer">
            <generator class="sequence">
                <param name="sequence">job_sequence</param>
            </generator>
        </id>
        <property name="jobTitle" column="JOB_TITLE" type="string"/>
        <property name="jobDesc" column="JOB_DESC" type="string"></property>
<!--        <property name="jobCompany" column="JOB_COMPANY" type="integer"></property>-->
        <property name="jobCity" column="JOB_CITY" type="integer"></property>
        <property name="jobExp" column="JOB_EXP" type="integer"></property>
        <property name="jobCat" column="JOB_CAT" type="integer"></property>
        <property name="jobStartDate" column="JOB_STARTDATE" type="string"></property>
        <property name="jobEndDate" column="JOB_ENDDATE" type="string"></property>
        <many-to-one name="jobCompany" class="com.jobs.admin.data.Company" column="JOB_COMPANY" property-ref="companyKid" ></many-to-one>

    </class>

</hibernate-mapping>

这是我加入这些表的方法代码

 List<com.jobs.admin.data.JobCat> jobCat= null;
        db_session= HibernateSessionFactory.getSessionFactory().openSession();
        transaction=null;
        try{
            transaction = db_session.beginTransaction();
            jobCat = db_session.createQuery("from Job as job inner join job.jobCompany as company").list();
            transaction.commit();
        }
        catch (HibernateException e) {
            transaction.rollback();
            e.printStackTrace();
            } finally {
            db_session.close();
            }
        return jobCat;

当我跑步时,它给了我以下例外:

java.lang.NullPointerException
    at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1662)
    at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:641)
    at org.hibernate.type.EntityType.resolve(EntityType.java:415)
    at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139)
    at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
    at org.hibernate.loader.Loader.doQuery(Loader.java:752)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
    at org.hibernate.loader.Loader.doList(Loader.java:2232)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
    at org.hibernate.loader.Loader.list(Loader.java:2124)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
    at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
    at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
    at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
    at com.jobs.admin.service.JobImpl.getJobs(JobImpl.java:91)
    at com.jobs.admin.controller.JobController.showViewJobs(JobController.java:64)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:167)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:414)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

请建议我,我的错误是什么?

1 个答案:

答案 0 :(得分:1)

可能这是因为https://hibernate.onjira.com/browse/HHH-6813。我不知道是否在最新的4.1版本中修复了它。