1

I have issues like if I modify or filter the query in DAO then the JSON result is not coming, otherwise it's working. But I need to filter the query. All details are:

Table details: Address(addressID, road, state, code)

Entity model (Address.java):

@Entity
public class Address(){
    @Id
    int addressID;
    String code;    

    //getter + setter methods   
}

I only need addressID and code, only these two fields. So, I mapped only these two and I don't need the other fields.

Action class (JSONData.java):

public class JSONData extends ActionSupport{

    private List<Address> address;
    //getter, setter

    public String jsonFormatDate() {

        AddressDAO dao = new AddressDAO();
        address = dao.listOfAllAddresses();

        return SUCCESS;
    }

}

DAO class (AddressDAO.java):

public class AddressDAO{

    public List<Address> listOfAllAddresses() { 
        .....
        List list = s.createQuery("from Address").list();
        .....
        return list;
    }

}

If I use the query "from Address" then the JSON output is successful and I may easily use that result to render the JSP page.

But if I modify the query and filter like "select a.addressID, a.code from Address a" then there is no JSON output. Moreover, if I also use the where clause, then no JSON output. If I run this form DAO layer, the query executes the proper output. But not from the JSON.

What things I need to include to get the JSON output by using some filtering rules.

Roman C
  • 49,761
  • 33
  • 66
  • 176
adarksun
  • 371
  • 1
  • 6
  • 20

1 Answers1

0

You can use where clause in the query to filter rows by the condition. For example

Query q = s.createQuery("from Address as a where a.code = ?");
q.setParameter(0, value);
List list = q.list();
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • I tried to use query like "select a.addressID from Address a". But no JSON output. But that same query is working if I tried using the console mode from the DAO class. – adarksun Jan 27 '14 at 15:05
  • Use the query in this answer. For JSON output you should return a `json` result. See [this](http://stackoverflow.com/a/21350079/573032) answer. – Roman C Jan 27 '14 at 15:20
  • I tried that answer. But I am still confused. In struts.xml, I configured like "". So, I am expecting the result through the field "address" into the Action class. But it gives only the primary key field's value into the JSON result. I am not getting the adressID values using the query "select a.addressID from Address a" So, where actually I need to change? – adarksun Jan 27 '14 at 21:08