How to pass Dynamic value in #Where in JPA - java

I want to filter entity by using #Where and want to know how to pass dynamic value in JPA
Example : #Where(clause = "name = 'Alas'") //Here 'Alas' is static i want to make it dynamic

You cannot make the structure of the WHERE clause itself dynamic in this way. Assuming you want to make certain portions of the WHERE clause dynamic, you could use:
SELECT *
FROM yourTable
WHERE name = ? OR ? IS NULL;
To the ? placeholders above, you would bind 'Alas'. If you don't pass any value, then the crtierion on name would simply be ignored.

Related

Specifying a type handler for an annotated MyBatis insert

I am trying to create an annotated insert statement using MyBatis where the type handler is specified in an annotation.
For example, in a select query we can specify the typehandler like this:
#Results({
#Result(column = "strings", property = "strings", typeHandler = StringArrayTypeHandler.class)
})
#Select("SELECT * FROM ${name} ORDER BY id ASC;")
List<StringObject> getStringObjects(#Param("name") String name);
However, the same does not appear possible for an insert query as the #Results annotation is only for #Select queries.
Currently, my work around is to specify the type handler as part of the query string like this:
#Options(useGeneratedKeys = true)
#Insert({"INSERT INTO ${name} (text, value, strings) VALUES (#{obj.text}, #{obj.value}, #{obj.strings, typeHandler=com.mypackage.typehandler.StringArrayTypeHandler});"})
void insertStringObject(#Param("obj") SenticConcept concept, #Param("name") String version);
My question is, can we specify the type handler class in an annotation rather than being part of the query string?
This can't be done.
The result mapping uses column names as keys because column values in the result set can be identified using column name, that is mybatis can get the value of some column by its name. So it can use a mapping configuration (for example typeHandler) to process the value in that column.
The input parameters to the query in JDBC are identifiable only by the index of the parameter. In principle it is possible to implement an annotation that would specify the typeHandler based on the index of the parameter, but this is error prone because a change in the query may cause the change in the parameter indices (that's probably the reason it is not done in mybatis).

Hibernate: Fetching columns with their aliases

Consider this trivial query:
SELECT 1 as first, 2 as second
When using Hibernate we can then do something like:
em.createNativeQuery(query).fetchResultList()
However, there seem to be no way of getting the aliases (or column names). This would be very helpful for creating List<Map<String, Object>> where each map would be a row with their aliases, for instance in this case: [{first: 1, second: 2}].
Is there a way to do something like that?
I would suggest a bit different approach which may meet your needs.
In JPA 2.1 there is a feature called "result set mapping".
Basically you have to define a POJO class which would hold the result values (all the values must be passed using the constructor):
public class ResultClass{
private String fieldOne;
private String fieldTwo;
public ResultClass(String fieldOne, String fieldTwo){
this.fieldOne = fieldOne;
this.fieldTwo = fieldTwo;
}
}
Then you have to declare the mapping on one of your entities (does not matter on which, it just has to be a declated #Entity):
#SqlResultSetMapping(name="ResultMapping", classes = {
#ConstructorResult(targetClass = ResultClass.class,
columns = {#ColumnResult(name="columnOne"), #ColumnResult(name="columnTwo")})
})
The columnOne and columnTwo are aliases as declared in the select clause of the native query.
And finally use in the query creation:
List<ResultClass> results = em.createNativeQuery(query, "ResultMapping").getResultList();
In my opinion this is more elegant and "a level above" solution as you are not working with a generic Map key/values pairs but with a concrete POJO class.
You can use ResultTransformer interface . Implement custom mapper for mapping values with aliases.
here is example https://vladmihalcea.com/why-you-should-use-the-hibernate-resulttransformer-to-customize-result-set-mappings/
with ResultTransformer you can easy customize result set type , especially if you need aliases

Get physical column value with entity property value using hibernate

I have a table T with columns defined as usual.
#Entity
#Table(name="T")
public class T {
#Column(name="test_id")
private Long testId;
}
Given entity property "testId", I want to get corresponding DB column name (i.e. "test_id"). How could it be achieved?
Edit 1:
I want to keep this column at separate location with actual DB column name (test_id) than testId. I fetched these values from DB using HQL which have key as entity name (i.e. testId) and I want actual column name in DB.
If I understood your requirement correctly, you want to use HQL while having a consistent name for both DB column and the entity field, like this:
SELECT t.test_id FROM Test t
instead of
SELECT t.testId FROM Test t
There is only one way to do that - renaming the field to test_id. HQL works on entities, not on DB tables, so you must use proper field names in the query.
Since test_id contradicts the usual Java coding conventions, I would advise against it.
EDIT: Getting the annotation attribute value with reflection would work along this outline:
Field field = MyEntity.class.getDeclaredField("testId");
Column a = field.getAnnotation(Column.class);
String columnName = a.name();
I would try to avoid this by any means, but if you're really sure you'll need it, use:
Configuration configuration = sessionFactory.getConfiguration();
PersistentClass persistentClass = configuration
.getClassMapping(T.class.getName());
String columnName = ((Column) persistentClass.getProperty("testId")
.getColumnIterator().next()).getName();
See also Get table column names in Hibernate

set table name with hibernate name parameters

I need to set a table name dynamically so that I use query.setText(tname,abc)
e.g: select a.name from :tname where a.id = '2'
I used setText() because when I use setString() it says "tname is a invalid parameter" because I assume that Hibernate adds '' when setting string parameters.
But even setText() does not help and gives the same exception.
How can I set the table name dynamically?
Reply to PSR:
So you mean replace table name as a java string replacement. But then we can not take support of sql injections prevention etc from hibernate right? Also How we bind parameters in hibernate in a situation where like statement,
Eg: name like "%:name%"
This also gives me Illegal argument exception: Parameter does not exist as a named parameter when i try to bind it using query.setString(name,"def");
Hibernate will not do this for you, because it works with PreparedStatements, and you can't prepare a statement where the table being queried isn't known yet.
I don't see why you would be exposing table names to end users, so preventing SQL injection doing a regular string substitution should be easy. You use some sort of business logic to determine the correct table from a list that only you know. The table name isn't coming from user input at all.
Depending on your choice of RDBMS, you may find a discriminator column, or table inheritance with partitioning to be a better way of handling a situation where identical queries are made against different tables.
It is not possible to set table name dynamically.You can set dynamically column names.it is not possible to set table name
try like this
select a.name from '+table name+'where a.id = '2'
In my opinion, There are 2 ways to resolve this issue:
1- If you are using Spring and Hibernate together, you could use SpEL and it would be like #{#entityName} as it is described here
#Entity
public class User {
#Id
#GeneratedValue
Long id;
String lastname;
}
public interface UserRepository extends JpaRepository<User,Long> {
#Query("select u from #{#entityName} u where u.lastname = ?1")
List<User> findByLastname(String lastname);
}
2-You could use CriteriaBuilder like
CriteriaQuery<YourEntity> cr = cb.createQuery(YourEntity.class);
Root<YourEntity> root = cr.from(YourEntity.class);
cr.select(root);
I copied the source codes from the provided links and they are described there much better

hibernate select all projections group by

In hibernate Criteria specification, if I want to group property in table, I must use Projections.groupProperty. The problem is I can't select all the field in the table.
What I want to is:
SELECT * FROM entity GROUP BY field
If i use group in hibernate Criteria, then the groupProperty must be exist, and the sql result will be:
SELECT field FROM entity GROUP BY field.
Is there any way to get what I want? I see in hibernate code, in CriteriaJoinWalker, when I want to select all (select *), the code is already specify if there is projection or not. My assumption if I want to change the behavior, I must change the CriteriaJoinWalker code. I can modify the code, and maybe I can get the result what I want, but if I can, I do not want to override the hibernate core code to my project. Is there any way to change it directly without change hibernate code?
// CriteriaJoinWalker
if(translator.hasProjection())
{
resultTypes = translator.getProjectedTypes();
initProjection(translator.getSelect(), translator.getWhereCondition(), translator.getOrderBy(), translator.getGroupBy(), LockMode.NONE);
} else
{
resultTypes = (new Type[] {
TypeFactory.manyToOne(persister.getEntityName())
});
initAll(translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE);
}
Thanks
If you use Projections then you will not be able to get an Entity object as a whole. Unfortunately hibernate cant help you. Check out this Issue and also check out this thread.

Categories