我正在尝试通过Hibernate建立数据库连接(我是这项技术的初学者)。但是,当我创建ProspectStatusDao类的实例时遇到下一个异常,实现的所有详细信息(XML的配置文件和Java类)都在下面。非常感谢您的帮助:

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>


-----------------------休眠Util类---------------------- -------------------------------------------------- -----

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类------------------------- -------------------------------------------------- -------------

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;
}


}

最佳答案

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, ...

与其他列类似。

10-07 22:35