1

What I need to do is to run a piece of native sql query to select everything and get data out from a specific table.

My app is a spring hibernate based web app. Here is my code:

DAOserviceImpl:

@Service public class ItemServiceImpl implements ItemService { @PersistenceContext EntityManager em; @Transactional public void addItem(Item item) { em.persist(item); } @Transactional public List<Item> iosADVsearchResults(String itemCode) { //run native query with jpa List<Item> itemList = (List<Item>)em.createQuery("SELECT * FROM item itemcode='" + itemCode + "'") .getResultList(); return itemList; } } 

but what I eventually get is this error:

HTTP Status 500 - Request processing failed; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: unexpected token: = near line 1, column 35 [SELECT itemcode FROM item itemcode='ll3369']

I was following this tutorial: http://www.oracle.com/technetwork/articles/vasiliev-jpql-087123.html

Please help, any code example would be helpful.

2 Answers 2

2

If You are using JPA your query to get all records should be like this:

List<Item> itemList = (List<Item>) em.createQuery("SELECT i FROM item where itemcode=?1) .setParameter(1, itemcode) .getResultList(); 

this will return all the record which matches the parameter itemcode ..

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

Comments

1

em.createQuery() expects a query written in JPQL, not SQL (see this for some info). For SQL query, use em.createNativeQuery()

List<Item> itemList = (List<Item>)em.createNativeQuery("SELECT * FROM item WHERE itemcode='" + itemCode + "'", Item.class).getResultList(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.