2

I have a problem while I'm trying to pull data from my DB requesting to be in a specific object type. I've created query, which is fetching objects of Java Object type not the type that I need. Here is my DAO class:

import com.jackowiak.Domain.TurbinesData;
import com.jackowiak.Model.TurbineDataCSVReader;
import com.jackowiak.Utils.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import java.util.List;

public class TurbinesDaoBean {
    private static final Logger LOG = LoggerFactory.getLogger(TurbinesDaoBean.class);

    public List<TurbinesData> getTurbineDataFromDB(String turbineName) {

        LOG.info("Initializating DB connection to get turbine data");
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();

        Query query = session.createQuery("select windSpeed, turbinePower from TurbinesData where turbineName = :turbineName");
        query.setParameter("turbineName", turbineName);

        session.getTransaction().commit();
        List<TurbinesData> results = query.list();

        LOG.debug("Data for turbine " + turbineName + " collected successfully");
        return results;


    }
}

And here is my Entity class:

    @Entity
    @Table(name = "TurbinesData")
    public class TurbinesData {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "ID", unique = true, nullable = false)
        protected long id;

        @Column(nullable = false, length = 50, name = "Nazwa_turbiny")
        protected String turbineName;

        @Column(nullable = false, length = 20, name = "V_wiatru")
        protected Double windSpeed;

        @Column(nullable = false, length = 20, name = "Moc_turbiny")
        protected Double turbinePower;

        public TurbinesData() {
        }

        public TurbinesData(Double windSpeed, Double turbinePower) {
            this.windSpeed = windSpeed;
            this.turbinePower = turbinePower;
        }

        public TurbinesData(String turbineName, Double windSpeed, Double turbinePower) {
            this.turbineName = turbineName;
            this.windSpeed = windSpeed;
            this.turbinePower = turbinePower;
        } 
// getters and setters
} 

I would like to receive list of TurbinesData objects after executing query

2
  • Please check this question: stackoverflow.com/questions/10829197/… Commented Aug 24, 2018 at 18:01
  • When I'm using TypedQuery it throws Cannot create TypedQuery for query with more than one return using requested result type [com.jackowiak.Domain.TurbinesData] exception Commented Aug 24, 2018 at 18:07

2 Answers 2

1

Change the jpql to:

"FROM TurbinesData td WHERE td.turbineName = :turbineName"

And then use TypedQuery

EDIT: According to your comment you want to retrieve only two fields. You need to do:

"SELECT NEW package.to.TurbinesData(td.windSpeed, td.turbinePower) FROM TurbinesData td WHERE td.turbineName = :turbineName"

Note:

  1. Need to have proper constructor defined.
  2. Need to use fully qualified name
Sign up to request clarification or add additional context in comments.

3 Comments

I can agree with this solution, I've achieved this earlier, but in this case I'm retrieving all data from DB and I think it's not the most efficient way to do it. What I mean is shouldn't I pull only necessary data in this case objects with 2 fields rather than with 4 fields?
@Jabbasnik Understood. I can't tell how much performance difference will be there in this case, but that can be achieved. I have updated my answer.
It's working. I didn't know I can call constructors from hql. Thank You!
0

You can typecast List<Object> to List<custom class>

Like.

return (List<T>) query.list();

Hope that helps.

2 Comments

Unfortunately it's not working - still getting Object[] list
I realised issue in your code. For mapped entity in hibernate, we go with hql queries than complete query, which return results in desired 'List<CustomObj>' Example: 'String hql = "from Product where description like :keyword"; Query query = session.createQuery(hql); query.setParameter("keyword", "P1"); List<Product> listProducts = query ' Try this way.

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.