0

I am trying to fetch specific fields from my entities. I need the result in my entity structure.

Following are my entities:

Country

public class CountryModel {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "CmtID")
    private int id;
    @Column(name = "CmtName")
    private String name;
    @JoinColumn(name="CmtStateID")
    @OneToMany(targetEntity=StateModel.class,fetch=FetchType.EAGER)
    private List<StateModel> state;
}

State

public class StateModel {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "SmtID")
    private int id;
    @Column(name = "SmtName")
    private String name;
}

Following is the HQL query am executing:

Query query = session.createQuery("select c.name, s.name from CountryModel c join c.state s where c.id=2");
CountryModel stateModel = (CountryModel) query.uniqueResult();

But am getting the following error:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.muziris.entity.CountryModel

Thanks for helping.

Expected result:

Country :
        name : india
        state : 
              name : kerala
              name : goa
        name : Pak
        state :
              name : karachi

2 Answers 2

0

Since your classes are mapped you can try:

 Query query = session.createQuery("from CountryModel c where c.id=2");
 CountryModel countryModel = (CountryModel) query.uniqueResult();

Let's make use of the mapping and HQL.

From there you can use a DTO to have only the data that you need

public CountryDTO transform(CountryModel cm){
String countryName = cm.getName();
List<String> stateNames = cm.getState().stream.map(StateModel::getName)
                                              .collect(Collectors.toList());
return new CountryDTO(countryName, stateNames);
}

CountryDTO is the result that you need.

Sign up to request clarification or add additional context in comments.

5 Comments

no. i only need to get country name and state name. i do not need all properties
still it will fetch all data from DB bro. i cannot allow that to happen.
why not? Why would you map your classes and use HQL then?
@tdavid Looks like EAGER loading is used. All related entities can be fetched if you will fetch entirely CountryModel.
Because in projects same entity is used in different places. in all places you would not want entire data. If u do that you are giving unwanted load to DB
0

Hibernate returns List<Object[]> when you use the projections. List<Object[]> is a list of specified projection columns.

Some links

https://stackoverflow.com/a/36883968/3405171

How to transform a flat result set using Hibernate

1 Comment

will it work on non sql query. I cannot use native query

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.