尝试获取 FactorySession 时获取 failed.org.hibernate.MappingException

Getting failed.org.hibernate.MappingException when trying to get a FactorySession

我正在尝试通过 Hibernate 建立数据库连接(我是这项技术的初学者)。尽管如此,当我创建 class ProspectStatusDao 的实例时,我遇到了下一个异常,实现的所有细节(XML 的配置文件和 java class es)在下面。非常感谢您的帮助:

public static void main(String[] args) throws MalformedURLException {

    ProspectStatusDao p = new ProspectStatusDao();
    p.getProspectStatus(26);

}


Exception: Initial SessionFactory creation     failed.org.hibernate.MappingException: could not determine type nullException in thread "main" java.lang.NullPointerException
at com.posadas.referidos.dao.ProspectStatusDao.getProspectStatus(ProspectStatusDao.java:50)

at demo.spring.client.Client.main(Client.java:30)

------------------------hibernate.cfg.xml---------------- ----------------------

<hibernate-configuration>
<session-factory>
    <!-- SQL Dialect -->
    <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
    <!-- Database connection settings -->
    <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
    <property name="hibernate.connection.url"></property>
    <property name="hibernate.connection.username"></property>
    <property name="hibernate.connection.password"></property>
    <!-- JDBC connection pool -->
    <property name="hibernate.connection.pool_size">200</property>
    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.format_sql">true</property>
    <!-- Bind the getCurrentSession() method to the thread. -->
    <property name="current_session_context_class">thread</property>
        <mapping resource="com/posadas/referidos/model/queries/queryProspect.hbm.xml" />
</session-factory>
</hibernate-configuration>

------------------------hbm.xml---------------- ------------------

<sql-query name="queryProspectStatus">
    <return-scalar column="ReferredID" type="java.lang.Integer" />
    <return-scalar column="FirstName" type="java.lang.String" />
    <return-scalar column="LastName" type="java.lang.String" />
    <return-scalar column="StatusID" type="java.lang.Integer" />
    <return-scalar column="Concept" type="java.lang.String" />

<![CDATA[
    SELECT dbo.Referred.ReferredID, dbo.t_Prospect.FirstName, dbo.t_Prospect.LastName, 
    dbo.Referred.StatusID, dbo.Referred_Status.Concept
    FROM dbo.Referred INNER JOIN dbo.t_Prospect ON dbo.Referred.prospectID = dbo.t_Prospect.ProspectID
    INNER JOIN dbo.Referred_Status ON dbo.Referred_Status.StatusID = dbo.Referred.StatusID
    WHERE dbo.Referred.ReferredID = :referredId
]]>

</sql-query>

------------------------Hibernate Util class---------------- ---------------------------------------------- --------------

import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


public class HibernateUtil {

private static SessionFactory sessionFactory;

private static SessionFactory buildSessionFactory() {

    try {

        // Create the SessionFactory from hibernate.cfg.xml
        Configuration configuration = new Configuration().configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().
                applySettings(configuration.getProperties()).build();
        return configuration.buildSessionFactory(serviceRegistry);

    } catch(Throwable ex) {

        System.out.println("Initial SessionFactory creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static SessionFactory getSessionFactory() {

    if (null == sessionFactory)
        sessionFactory = buildSessionFactory();

    return sessionFactory;
}

}

------------------------DAO class---------------- ---------------------------------------------- ----------------------

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.posadas.referidos.to.ProspectStatusTO;
import com.posadas.referidos.util.HibernateUtil;
/**
  * @author r.gonzalez.arellano
  *
 */
public class ProspectStatusDao {

private Session sessionSQL;

private void openConnectionSQL(){
    sessionSQL = HibernateUtil.getSessionFactory().getCurrentSession();
}

//public List<ProspectStatusTO> getProspectStatus(ProspectStatusTO objTO){
public void getProspectStatus(int referredId){

    //List<ProspectStatusTO> lista = new ArrayList<ProspectStatusTO>();

    try{
        openConnectionSQL();
        Query queryProspectStatus = sessionSQL.getNamedQuery("queryProspectStatus");
        queryProspectStatus.setParameter("referredId", referredId);

        List resultado = queryProspectStatus.list();
        ProspectStatusTO prospectStatusTo = null;

        for(Object object:resultado){
            Object[] obj =(Object[]) object;
            prospectStatusTo = new ProspectStatusTO();
            prospectStatusTo.setReferredId(obj[0]==null?0:Integer.parseInt(obj[0].toString()));
            prospectStatusTo.setLastName(obj[1]==null?"":obj[1].toString());
            prospectStatusTo.setFirstName(obj[2]==null?"":obj[2].toString());
            prospectStatusTo.setStatusId(obj[3]==null?0:Integer.parseInt(obj[3].toString()));
            prospectStatusTo.setStatusConcept(obj[4]==null?"":obj[4].toString());
        }

        System.out.println(prospectStatusTo);

    }catch(Exception e){
    }finally{
        sessionSQL.close();
    }

    //return lista;
}

}

您跳过了空 catch 块中的真正异常。在 openConnectionSQL();.

中可能是个例外

sessionSQL 为空,无法关闭。添加e.printStackTrace()看看是什么问题

}catch(Exception e){
    e.printStackTrace();
}finally{
    sessionSQL.close();
}

Exception: Initial SessionFactory creation failed.org.hibernate.MappingException: could not determine type null

这意味着您在 queryProspect.hbm.xml.

中有错误

问题可能是您没有使用别名。

如果你使用这个

<return-scalar column="ReferredID" type="java.lang.Integer" />

您需要在 SQL

中有一个别名 ReferredID

SELECT dbo.Referred.ReferredID AS ReferredID, ...

与其他专栏类似。