Solved

Hibernate fectch error

Posted on 2014-12-27
1
105 Views
Last Modified: 2014-12-30
Hello there,

I am trying to fetch data from the MSSQL 2008 database using hibernate. But for some reason I get this error

data:"[Ljava.lang.Object; cannot be cast to com.erp.shared.entities.custom.CusNeighbourGroupInfo",

here is my hibernate code.

public DSResponse fetch(DSRequest dsRequest) throws Exception
    {
        logger.info("> fetch");
        DSResponse dsResponse = new DSResponse();
        Session hibernateSession = HibernateUtills.getInstance().getHibernateSession();

        String select = "SELECT\r\n" + 
                "    dbo.province.provincename as province,\r\n" + 
                "    dbo.province.capital,\r\n" + 
                "    dbo.province.code,\r\n" + 
                "    dbo.province.telcode,\r\n" + 
                "    dbo.province.taxcode,\r\n" + 
                "    dbo.county.countyname as county,\r\n" + 
                "    dbo.district.districtname as district,\r\n" + 
                "    dbo.zone.alternateName as zone,\r\n" + 
                "    dbo.neighbourhood.alternateName as neigbhour,\r\n" + 
                "    dbo.city.cityname as city,\r\n" + 
                "    dbo.city.taxcode,\r\n" + 
                "    dbo.city.fdocode,\r\n" + 
                "    count(dbo.customer.customeraltname) as countCustomer\r\n" + 
                "FROM\r\n" + 
                "    dbo.county\r\n" + 
                "INNER JOIN\r\n" + 
                "    dbo.province\r\n" + 
                "ON\r\n" + 
                "    (\r\n" + 
                "        dbo.county.provinceID = dbo.province.id)\r\n" + 
                "INNER JOIN\r\n" + 
                "    dbo.district\r\n" + 
                "ON\r\n" + 
                "    (\r\n" + 
                "        dbo.county.id = dbo.district.countyID)\r\n" + 
                "INNER JOIN\r\n" + 
                "    dbo.city\r\n" + 
                "ON\r\n" + 
                "    (\r\n" + 
                "        dbo.district.id = dbo.city.districtID)\r\n" + 
                "INNER JOIN\r\n" + 
                "    dbo.zone\r\n" + 
                "ON\r\n" + 
                "    (\r\n" + 
                "        dbo.city.id = dbo.zone.cityId)\r\n" + 
                "INNER JOIN\r\n" + 
                "    dbo.neighbourhood\r\n" + 
                "ON\r\n" + 
                "    (\r\n" + 
                "        dbo.zone.id = dbo.neighbourhood.zoneId)\r\n" + 
                "INNER JOIN\r\n" + 
                "    dbo.customer\r\n" + 
                "ON\r\n" + 
                "    (\r\n" + 
                "        dbo.neighbourhood.id = dbo.customer.neighbourhoodId)\r\n" + 
                "        Group By dbo.province.provincename,\r\n" + 
                "    dbo.province.capital,\r\n" + 
                "    dbo.province.code,\r\n" + 
                "    dbo.province.telcode,\r\n" + 
                "    dbo.province.taxcode,\r\n" + 
                "    dbo.county.countyname,\r\n" + 
                "    dbo.district.districtname,\r\n" + 
                "    dbo.zone.alternateName,\r\n" + 
                "    dbo.neighbourhood.alternateName,\r\n" + 
                "    dbo.city.cityname,\r\n" + 
                "    dbo.city.taxcode,\r\n" + 
                "    dbo.city.fdocode\r\n" + 
                "";

        Query query = hibernateSession.createSQLQuery(select);

        List<CusNeighbourGroupInfo> order = query.list();

        for (int i = 0; i < order.size(); i++)
        {
            if (order.get(i).getProvince() != null)
            {
                DebugTools.printj(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>                                    "+order.get(i).getProvince());
                order.get(i).getCounty();
                order.get(i).getDistrict();
                order.get(i).getCity();
                order.get(i).getZone();
                order.get(i).getNeighbour();
            }
        }

        Integer rowCount = order.size();

        dsResponse.setStartRow(0);
        dsResponse.setEndRow(rowCount);
        dsResponse.setTotalRows(rowCount);
        dsResponse.setData(order);
        HibernateUtills.getInstance().closeSession();
        DebugTools.printj(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>     FETCHEDDDDDDDD                               ");
        logger.info("< fetch");
        return dsResponse;
    }

Open in new window


and my POJO class

public class CusNeighbourGroupInfo
{
    private String province;
    private String county;
    private String district;
    private String city;
    private String zone;
    private String neighbour;
    
    public CusNeighbourGroupInfo()
    {

    }

    public CusNeighbourGroupInfo(String province, String county, String district, String city, String zone,
            String neighbour)
    {
        super();
        this.province = province;
        this.county = county;
        this.district = district;
        this.city = city;
        this.zone = zone;
        this.neighbour = neighbour;
    }

    public String getProvince()
    {
        return province;
    }

    public void setProvince(String province)
    {
        this.province = province;
    }

    public String getCounty()
    {
        return county;
    }

    public void setCounty(String county)
    {
        this.county = county;
    }

    public String getDistrict()
    {
        return district;
    }

    public void setDistrict(String district)
    {
        this.district = district;
    }

    public String getCity()
    {
        return city;
    }

    public void setCity(String city)
    {
        this.city = city;
    }

    public String getZone()
    {
        return zone;
    }

    public void setZone(String zone)
    {
        this.zone = zone;
    }

    public String getNeighbour()
    {
        return neighbour;
    }

    public void setNeighbour(String neighbour)
    {
        this.neighbour = neighbour;
    }

    @Override
    public String toString()
    {
        return "CusNeighbourCubeInfo [province=" + province + ", county=" + county + ", district=" + district +
                ", city=" + city + ", zone=" + zone + ", neighbour=" + neighbour + "]";
    }   
}

Open in new window


please help

cheers
Zolf
0
Comment
Question by:zolf
1 Comment
 
LVL 20

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
ID: 40521466
Your query returns multiple fields than the your POJO has. I would suggested keep the fields same as your POJO or create another POJO by accomodating all the fields.


Hope this helps.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
Core Java. What output will be and why ? 6 58
groovy example issue 10 70
maven project error 5 24
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now