jpa select string found in where in caluse - jpa

I have a list of strings and i need to search them in my table then construct new class contain key as on of my search strings and value as my pojo if exist or null if not exist.
something like that :
Class MyMapper {
private String identificationKey;
private User user;
// Getter and Setter
}
My Expected Query:
SELECT NEW MyMapper(identificationKey, u) From User u where u.identification IN :identificationKeys
i need to select every identificationKey in identificationKeys even not exist

Related

Select Map<Integer, Integer> using mybatis

I want to select Map for each record. Result class looks like
public class Mapping {
private String name;
private Map<Integer, Integer>;
}
SQL table has only three columns namely name, id, partner_id.
How can I create Map of Id to Partner Id for each name using mybatis?
you can use sqlSession.selectForMap, and give mybatis the column which will be processed as the key of map , such as name. then it will return
Map<String,Map<String,Object>>
as a result, but the value is Map, key is the column name, you need to transform it .

Default name for database objects

I'm trying to find out what column name JPA uses when there's no explicit name set.
Example:
#Entity
public class TestType {
private Boolean active;
private Character testTypeCode;
public Boolean getActive() {
return active;
}
public void setActive(Boolean active) {
this.active = active;
}
#Id
public Character getTestTypeCode() {
return testTypeCode;
}
public void setTestTypeCode(Character testTypeCode) {
this.testTypeCode = testTypeCode;
}
}
What name will be used for the primary key column, which column name will be used for the property "active" and what table name will be used. I'm looking for the specification of what names JPA uses by default.
It is the chapter "2.13 Naming of Database Objects" that specifies that (JPA 2.0 spec).
This specification requires the following with regard to the
interpretation of the names referencing database objects. These names
include the names of tables, columns, and other database elements.
Such names also include names that result from defaulting (e.g., a
table name that is defaulted from an entity name or a column name that
is defaulted from a field or property name).
In you example, the table name would be TestType, the ID column name testTypeCode and the active column would the called the same. Please also note, that in the SQL Standard all column and table names are case insensitive, although there are some counterexamples (e.g MySQL on Unix: the table names are case sensitive).

How to access Map field in JPA via JPQL

for example, if there is an #ElementCollection file which is with a Map type, then if I try to get the map key or value field then how to process?
Class Deal{
.....
private String name;
private String department;
private DealType type;
#AttributeOverrides({
#AttributeOverride(name="value.in.available", column=#Column(name="in_avl")),
#AttributeOverride(name="value.in.unavailable", column=#Column(name="in_unv")),
#AttributeOverride(name="value.out.available", column=#Column(name="out_avl")),
#AttributeOverride(name="value.out.unavailable", column=#Column(name="out_unv"))
})
#ElementCollection(fetch = FetchType.EAGER)
......
}
So if I try to get something like this
select new SummaryAmount(SUM(t.value.in.available), SUM(t.value.in.unavailable),
SUM(t.value.out.available), SUM(t.value.out.unavailable)) from Deal AS d INNER
JOIN d.transactionAmounts t GROUP by t.key;
Is it something possible can work out now? Everything is follow the book except I invent the t.value and t.key as I really don't know how to present map key and value in JPQL.Thanks
Thanks
Try this:
SELECT new SummaryAmount(SUM(VALUE(t).in.available), SUM(VALUE(t)in.unavailable),
SUM(VALUE(t).out.available), SUM(VALUE(t).out.unavailable)) from Deal AS d INNER
JOIN d.transactionAmounts t GROUP by KEY(t);
And now an excerpt from the JPA specification:
An identification variable qualified by the KEY, VALUE, or ENTRY
operator is a path expression. The KEY, VALUE, and ENTRY operators may
only be applied to identification variables that correspond to
map-valued associations or map-valued element collections. The type of
the path expression is the type computed as the result of the
operation; that is, the abstract schema type of the field that is the
value of the KEY, VALUE, or ENTRY operator (the map key, map value, or
map entry respectively).[53]
The syntax for qualified identification variables is as follows.
qualified_identification_variable :: =
KEY(identification_variable) |
VALUE(identification_variable) |
ENTRY(identification_variable)
A path expression using the KEY or VALUE operator can be further
composed. A path expression using the ENTRY operator is terminal. It
cannot be further composed and can only appear in the SELECT list of a
query.

Composite Primary key in JPA

The EmbeddedId or IdClass annotation is used to denote a composite primary key.
How can i use composite primary key without ( EmbeddedId or IdClass ) ?
If it is possible to use composite primary key without ( EmbeddedId or IdClass ) then how can i use EntityManager.find( Entity Class , Object primaryKey) method to find entity in case of composite primary key(Multiple Primarykey) (because of no IdClass or EmbeddedId) .
EclipseLink take List of pk in the find() operation but if composite pk key defined in example -
Entity Person {
#Id
String username;
#Id
String emailId;
#Basic
String firstName;
#Basic
String lastName;
}
List list = new ArrayList();
list.add(${username}); //Run time value
list.add(${emailId}); //Run time value
then EnitityManager.find(list) will take these arguments , is i am right?
If i am assuming correct then how will EnitityManager.find() operation will know that List 1st argument is username or emailId pk value (means sequence of composite pk fields value)
Let me give my thoughts about it.
find
<T> T find(java.lang.Class<T> entityClass,
java.lang.Object primaryKey)
In order to find an entity of class Person, you should use something like
find(Person.class, Object primaryKey)
As you have a composed key, you should have a IdClass or EmbeddedId like this.
public class PersonKey implements Serializable{
String username;
String emailId;
public PersonKey(String username, String emailId){
//Add lines for correct constructor
}
//Override hascode and equals
}
Then you can find objects based on that key.
find(Person.class, personKey);
your key need to be something like this.
PersonKey personKey = new PersonKey(1,1);
Person p = find(Person.class, personKey);
FIND DON'T ACCEPT LISTS IN PURE JPA !!, accept and return only ONE managed object. If you would use find to retrieve several objects you should call the method N times passed the keys you want to find.
If you use find passing a list you will see something like this.
org.springframework.dao.InvalidDataAccessApiUsageException:
Provided id of the wrong type for class domain.model.Person. Expected:
class domain.key.PersonKey, got class java.util.ArrayList; nested
exception is java.lang.IllegalArgumentException: Provided id of the
wrong type for class
com.staples.sa.pricemart.domain.model.ItemFileEntity. Expected: class
com.staples.sa.pricemart.domain.key.ItemFileKey, got class
java.util.ArrayList
It seems eclipseLink have find that you can do that, but in order to make you application more portable try to use find as is described in JPA.
You should give an entity as the primary key:
Person id = new Person();
id.setUsername("Username");
id.setEmailId("EmailId");
entityManager.find(Person.class, id);
JPA will use the the fields annotated with #Id to find the the record You need.
You should be able to use a List with the find() operation in EclipseLink.

named native query and mapping columns to field of entity that doesn't exist in table

I have a named native query and I am trying to map it to the return results of the named native query. There is a field that I want to add to my entity that doesn't exist in the table, but it will exist in the return result of the query. I guess this would be the same with a stored proc...
How do you map the return results of a stored proc in JPA?...
How do you even call a stored proc?
here is an example query of what I would like to do...
select d.list_id as LIST_ID, 0 as Parent_ID, d.description from EPCD13.distribution_list d
The Result will be mapped to this entity...
public class DistributionList implements Serializable {
#Id
#Column(name="LIST_ID")
private long listId;
private String description;
private String owner;
private String flag;
#Column(name="PARENT_ID", nullable = true)
private long parentID;
}
parent ID is not in any table in my database. I will also need to use this entity again for other calls, that have nothing to do with this call, and that will not need this parent_id? Is there anything in the JPA standard that will help me out?
If results from database are not required for further manipulation, just for preview, you can consider using database view or result classes constructor expression.
If entities retrieved from database are required for further manipulation, you can make use of multiple select expression and transient fields.
Replace #Column annotation with #Transient annotation over parentID.
After retrieving multiple columns from database, iterate over results and manually set parentID.