Mapping RawSql Result to Object in Java Ebean - java

Let's say I have a class Article which is automatically mapped by Java Ebean as a database table.
For this table I wanted to retrieve entries via a RawSql query, because I find SQL more simple when it gets to complex queries with many joins and such. By far I have managed to give my SQL statement to the Parser. The query is correct, I already checked that.
The only problem is, that I don't know, how to map the database results to my Article class. I know, that there is a columnMapping(...) method but honestly, I am to lazy to map every single column manually...
Isn't there another way to just like myResults.mapToClass(Article.class) to retrieve something like a List<Article>?
This is the code I already have:
Finder<String, Article> find = new Finder<String, Article>(
String.class, Article.class);
String sql = "SELECT * FROM article [...]";
RawSql rawSql = RawSqlBuilder.parse(sql).create();
List<Article> returnList = find.setRawSql(rawSql).findList();
Alternatively:
Finder<String, Article> find = new Finder<String, Article>(
String.class, Article.class);
String sql = "SELECT id, title, sub_title FROM article [...]";
RawSql rawSql = RawSqlBuilder.parse(sql)
.columnMapping("id", "id")
.columnMapping("title", "title")
.columnMapping("sub_title", "subTitle")
.create();
List<Article> resultList = find.setRawSql(rawSql).findList();

A lot of happened in Ebean since the question was asked, but I think the problem is still valid. the new RawSqlBuilder.tableMapping() makes things easier as can be seen in the code below, but afaik it still needs manual mapping of all attributes (no SELECT table.* FROM table)
I have this exact problem, and worked around this by creating a helper object (#Entity/#Sql) that I map to. E.g. CustomerWithPurchaseStats.
Extract:
#Entity
#Sql
public class CustomerWithPurchaseStats {
#OneToOne
private Customer customer;
...
And in the DAO:
public List<CustomerWithPurchaseStats> getAllCustomersWithPurchaseStats() {
StringBuilder sql = new StringBuilder("SELECT cu.id, <manually add all fields you need mapped ").append(" FROM customer cu ");
RawSqlBuilder rawSqlBuilder = RawSqlBuilder.parse(sql.toString());
rawSqlBuilder.tableAliasMapping("cu", "customer").create();
return Ebean.find(CustomerWithPurchaseStats.class)
.setRawSql(rawSqlBuilder.create())
.findList();
}

Related

How to search by nested field id using NativeSearchQueryBuilder in ElasticsearchRepository?

I am using spring-data-elasticsearch and I have a class as follows,
#Document(indexName = "sample")
class Sample {
#Id
private String id;
private String name;
private Status status;
}
class Status {
private String id;
}
Above is persisted to elasticsearch. And I have the following method to find data in it.
List<Sample> getAll(String statusId, String text, Pageable pageable);
So here is the current code I am using to get the data,
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.multiMatchQuery(text)
.field("name")
.type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX));
NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();
What I need is I need to find results by statusId and then search for the texts containing in name field.
I tried the following way also but didn't work. I tried the criteria-api also but it is working for only words without space. If there is words with space it is throwing Cannot constructQuery '*\"text\"*'. Use expression or multiple clauses instead
.withFilter(QueryBuilders.termQuery("status.id", statusId))
So how can I build the query for the above scenario?
New query build after suggestions by P.J. Meisch
QueryBuilder query1 = QueryBuilders.matchQuery("status.id", statusId).operator(Operator.AND);
QueryBuilder query2 = QueryBuilders.multiMatchQuery(text).field("name").type(MultiMatchQueryBuilder.Type.PHRASE_PREFIX);
new NativeSearchQueryBuilder().withQuery(QueryBuilders.boolQuery().must(query1).must(query2));
Your top level query should be a bool query with two entries as must:
the first for the name that would be the one you already have.
the second for the "status.id" property

JPA - Select All Rows from Dynamic Table Name

Hi guys I am new to jpa, named queries, etc.. and I need something like this:
select t from :tableName t
Later in code I want something like this:
em.createQuery(...);
setParameter("tableName", "Person")
Result would be:
select * from person
How to write such a generic jpa query statement allowing to select all rows from :tableName which may be defined at runtime? thanks in advance
Try this I think this works well
EntityManagerFactory emfactory=Persistence.createEntityManagerFactory("Eclipselink_JPA" );
EntityManager entitymanager = emfactory.createEntityManager();
Query query = entitymanager.
createQuery("Select p from Person p");
List<String> list = query.getResultList();
setParameter("foo", foo) is used to set the value for column of the table not to set the table name. I do not think it will work, as you want to set the table name dynamically.
You can try this:
public returnType foo(String tableName){
String jpql = "SELECT t FROM " + tableName+ " t";
Query query = em.createQuery(jpql);
//rest of the code
}

how can we give hibernate session.get 2nd arg as non id value

Usually I use hibernate session.get(Class.class,id) to get entity class.
here in get method I gave 2nd arg as id value (i.e int id=7)
My question is how to use non id value in hibernate session.get(Class.class,non_id), I need to get values with some other column (non id column).
You can use Criteria Queries. So in your case maybe like this:
Criteria crit = session.createCriteria(Class.class);
crit.add(Restrictions.eq("non_id", "myNonIdValue"));
List results = crit.list();
Class myClassObj = (myClassObj) results.get(0);
You can't do a get with a non-id. You will have to query for the object you need, like you would in SQL.
String queryText = "from SomeObject where objectPropery = :VALUE"
Query queryObj = session.createQuery( queryText );
queryObj.setParameter( "VALUE", value );
return queryObj.uniqueResult()
You can read all about queryObjects here: Query
If you are querying for objects, you will have to get to know HQL.
You can use Hibernate with JPA 2
class MyClass { #Id long id; #Basic String description; }
TypedQuery<MyClass> query =
getEntityManager().createNamedQuery("select myClass from MyClass where description = :description", MyClass.class);
query.setParameter("description", "example");
List<MyClass> myClasses = query.getResultList();
Or a single entity:
MyClass myClass = query.getSingleResult();

Ebean query using setDistinct() does not work

I'm using an ebean query in the play! framework to find a list of records based on a distinct column. It seems like a pretty simple query but the problem is the ebean method setDistinct(true) isn't actually setting the query to distinct.
My query is:
List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findList();
In my results I get duplicate artist names.
From what I've seen I believe this is the correct syntax but I could be wrong. I'd appreciate any help. Thank you.
I just faced the same issue out of the blue and can not figure it out. As hfs said its been fixed in a later version but if you are stuck for a while you can use
findSet()
So in your example use
List<Song> allSongs = Song.find.select("artistName").setDistinct(true).findSet();
According to issue #158: Add support for using setDistinct (by excluding id property from generated sql) on the Ebean bug tracker, the problem is that an ID column is added to the beginning of the select query implicitly. That makes the distinct keyword act on the ID column, which will always be distinct.
This is supposed to be fixed in Ebean 4.1.2.
As an alternative you can use a native SQL query (SqlQuery).
The mechanism is described here:
https://ebean-orm.github.io/apidocs/com/avaje/ebean/SqlQuery.html
This is from the documentation:
public interface SqlQuery
extends Serializable
Query object for performing native SQL queries that return SqlRow's.
Firstly note that you can use your own sql queries with entity beans by using the SqlSelect annotation. This should be your first approach when wanting to use your own SQL queries.
If ORM Mapping is too tight and constraining for your problem then SqlQuery could be a good approach.
The returned SqlRow objects are similar to a LinkedHashMap with some type conversion support added.
// its typically a good idea to use a named query
// and put the sql in the orm.xml instead of in your code
String sql = "select id, name from customer where name like :name and status_code = :status";
SqlQuery sqlQuery = Ebean.createSqlQuery(sql);
sqlQuery.setParameter("name", "Acme%");
sqlQuery.setParameter("status", "ACTIVE");
// execute the query returning a List of MapBean objects
List<SqlRow> list = sqlQuery.findList();
i have a solution for it:-
RawSql rawSql = RawSqlBuilder
.parse("SELECT distinct CASE WHEN PARENT_EQUIPMENT_NUMBER IS NULL THEN EQUIPMENT_NUMBER ELSE PARENT_EQUIPMENT_NUMBER END AS PARENT_EQUIPMENT_NUMBER " +
"FROM TOOLS_DETAILS").create();
Query<ToolsDetail> query = Ebean.find(ToolsDetail.class);
ExpressionList<ToolsDetail> expressionList = query.setRawSql(rawSql).where();//ToolsDetail.find.where();
if (StringUtils.isNotBlank(sortBy)) {
if (StringUtils.isNotBlank(sortMode) && sortMode.equals("descending")) {
expressionList.setOrderBy("LPAD("+sortBy+", 20) "+"desc");
//expressionList.orderBy().asc(sortBy);
}else if (StringUtils.isNotBlank(sortMode) && sortMode.equals("ascending")) {
expressionList.setOrderBy("LPAD("+sortBy+", 20) "+"asc");
// expressionList.orderBy().asc(sortBy);
} else {
expressionList.setOrderBy("LPAD("+sortBy+", 20) "+"desc");
}
}
if (StringUtils.isNotBlank(fullTextSearch)) {
fullTextSearch = fullTextSearch.replaceAll("\\*","%");
expressionList.disjunction()
.ilike("customerSerialNumber", fullTextSearch)
.ilike("organizationalReference", fullTextSearch)
.ilike("costCentre", fullTextSearch)
.ilike("inventoryKey", fullTextSearch)
.ilike("toolType", fullTextSearch);
}
//add filters for date range
String fromContractStartdate = Controller.request().getQueryString("fm_contract_start_date_from");
String toContractStartdate = Controller.request().getQueryString("fm_contract_start_date_to");
String fromContractEndtdate = Controller.request().getQueryString("fm_contract_end_date_from");
String toContractEnddate = Controller.request().getQueryString("fm_contract_end_date_to");
if(StringUtils.isNotBlank(fromContractStartdate) && StringUtils.isNotBlank(toContractStartdate))
{
Date fromSqlStartDate=new Date(AppUtils.convertStringToDate(fromContractStartdate).getTime());
Date toSqlStartDate=new Date(AppUtils.convertStringToDate(toContractStartdate).getTime());
expressionList.between("fmContractStartDate",fromSqlStartDate,toSqlStartDate);
}if(StringUtils.isNotBlank(fromContractEndtdate) && StringUtils.isNotBlank(toContractEnddate))
{
Date fromSqlEndDate=new Date(AppUtils.convertStringToDate(fromContractEndtdate).getTime());
Date toSqlEndDate=new Date(AppUtils.convertStringToDate(toContractEnddate).getTime());
expressionList.between("fmContractEndDate",fromSqlEndDate,toSqlEndDate);
}
PagedList pagedList = ToolsQueryFilter.getFilter().applyFilters(expressionList).findPagedList(pageNo-1, pageSize);
ToolsListCount toolsListCount = new ToolsListCount();
toolsListCount.setList(pagedList.getList());
toolsListCount.setCount(pagedList.getTotalRowCount());
return toolsListCount;

Get Hibernate Entity instance from id column in SQLQuery result

I have (non-Hibernated) database tables that contain ids for Hibernate entities. I can query them (using createSQLQuery), which gives me the ids, from which I can then load the entities.
I'd like to do that in one step, and I think I can do that with addEntity, but I am not sure how exactly. (Hibernate's documentation web site is down. Again.) I can use addEntity when all the columns for the entity table are present, but I have only the id now.
This complains about the missing columns:
return (List<MyEntity>) session.createSQLQuery(
"select entity_id from the_table where foreign_key_value = ?")
.addEntity("entity_id", MyEntity.class)
.setLong(0, foreignKey).list();
I think you want something like:
session.createSQLQuery("select {entity.*} from entity_table {entity} where ....")
.addEntity("entity", Entity.class).(bind-parameters).list();
Hibernate will expand "{entity.*}" to be the relevant columns from entity_table.
Although if you already have the IDs, you can simply use session.load() to convert those to actual instances (well, lazy-load proxies).
i would use a join
select *
from entity_table
where entity_id = (select entity_id
from non_hibernate_table
where id = ?)
For oracle dialect. If u have problem with mapping database column type to java data type u can set it manually like that: .addScalar("integerFieldName", Hibernate.INTEGER)
public class LookupCodeName
{
private String code;
private String name;
/*... getter-setters ... */
}
public class someBL {
public List<LookupCodeName> returnSomeEntity() {
SQLQuery sqlQuery = (SQLQuery)((HibernateSession)em).getHibernateSession()
.createSQLQuery( "SELECT st.name as name, st.code as code FROM someTable st")
.addScalar("code")
.addScalar("name")
.setResultTransformer(Transformers.aliasToBean(LookupCodeName.class));
}
return (List<LookupCodeName>)sqlQuery.list();
}

Categories