Hello All,
Learning to use Hibernate, JPA stuff. Trying to use createNativeQuery to which is getting common column values from 2 database tables by using Oracle's UNION function. Eventually need to get the field values from the query output assigned to corresponding variables of the code. I could have created a view from similar query used here and create the entity class using Hibernate Code Generator. That would have been lot easier for me to do. But just wanted to find out how I can do the same using createNativeQuery.
Any help or hints would be greatly appreciated.
Here's snippets of the codes I have along with the error message being generated -
Code:
public class table1 extends java.io.Serializable {
import javax.persistence.Query;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
...
@Entity
@Table(name = "TABLE1", uniqueConstraints = {})
@SqlResultSetMapping(name="testing", entities =
@EntityResult(entityClass=table1.class, fields= {
@FieldResult(name="col1", column="col1"),
@FieldResult(name="col2", column="col2"),
@FieldResult(name="col3", column="col3"),
})
)
...
public class TestAction implements TestInterface {
public String col1;
...
@DataModel
private List<table1> queryresults;
...
// Here's sql statement for a Oracle 9i database
String sqlStmt = " select col1, col2, col3, col4 from (select col1, col2, col3, col4" +
"from table1" +
"union " +
"select col1, col2, col3, col4" +
"from table2) " +
"where col1 = :col1";
queryresults = em.createNativeQuery(sqlStmt, "testing").getResultList();
// Not sure this would be the riqht way to get individual field value from
// the query result output list.
col1 = queryresults.get(0).getCol1();
...
But getting org.hibernate.QueryException: Not all named parameters have been set: ... error