0

Eclipse / Java

I have a method to perform database query.

myBean :

List<Composicao> itemProdutoLista = compPrecoServico.buscar(Composicao.nomeQuery);

The nomeQuery is :

@NamedQuery(name = "nomeQuery", query= " SELECT composicao.valortotal,composicao.id FROM Composicao composicao

query - javax.persistence.query

public List<T> buscar(String nomeQuery) {
    Query query = entityManager.createNamedQuery(nomeQuery);
    return query.getResultList();
}

My return List buscar get me that : An Object elementData

enter image description here

How can I get an Entity Composicao elementdata in my return buscar method ? instead of object elementData

4
  • @JB Nized thats it what I want regarding that last post about How to read Java ArrayList sub-items Commented Feb 14, 2014 at 12:35
  • I don't quite understand - List<Composicao> itemProdutoLista contains objects of type Composicao so which part of your code is incorrect? Commented Feb 14, 2014 at 12:40
  • see my printscreen. It gets me an Object elementData and I need an entity elementData Commented Feb 14, 2014 at 12:41
  • So when you iterate over itemProdutoLista you get Object? But your list is already correctly typed, so when the type would not match, you would get a ClassCastException most likely. Commented Feb 14, 2014 at 12:44

2 Answers 2

3

I gave you the query to use in your previous question:

SELECT composicao FROM Composicao composicao

Don't you understand that selecting two fields of an entity, as you're doing in the query

SELECT composicao.valortotal,composicao.id FROM Composicao composicao
                    ^-- first field      ^-- second field    

doesn't return Composicao objects, but simply the two fields you selected: valortotal and id?

You're explicitely asking JPA to return only valortotal and id. Why would it return all the other fields and create a Composicao object? You want all the fields, and not just valortotal and id. So the query is (I'll repeat it a third time just in case):

SELECT composicao FROM Composicao composicao
Sign up to request clarification or add additional context in comments.

13 Comments

we are back to that old question. I need to retrieve itemProdutoLista.get(0).get(0) for valorTotal and itemProdutoLista.get(0).get(1) for id. I know thats not exists get(x).get(y) but is what I need to @JB Nizet
Composicao firstComposicao = itemProdutoLista.get(0); int valortotal = firstComposicao.getValortotal(); Long id = firstComposicao.getId();. If you get back a List<Composicao>, every element of the list is a Composicao. Isn't that obvious?
I did it already but I got this : java.math.BigDecimal cannot be cast to br.com.getsprint.gestao360.entidade.Composicao @JB Nizet
You're talking about an exception caused by code you're not showing. And this exception is thrown because you're trying to cast a BigDecimal to ComposicaoPreco. BigDecimal is not a ComposicaoPreco, so that doesn't make any sense. That's like doing Banana b = (Banana) truck; A truck is not a banana. And casting it to Banana won't change anything to what a truck is: it's a truck and will always be a truck. So of course, you get a ClassCastException.
I got exception in this line you posted : Composicao firstComposicao = itemProdutoLista.get(0); theres no code to show. I pasted all my code in this post. and itemProdutoLista is printed in this post either
|
0

Assuming you are talking about something like JPA.

Put a constructor on your entity (besides the empty/default constructor) that accepts every one of your query arguments.

@Entity
public class Jedi
   @Id private int id;
   private String name;

   //required by the standard
   public Jedi(){}

   public Jedi(int id, String name) {
     this.id = id;
     this.name = name;
   }

   //...
 }

Then your query

select NEW org.starwars.Jedi(j.id,j.name) from Jedi j where name.endsWith('Sky Walker')

Comments

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.