Case when statement within HQL query - java

What I tried:-
Select new fully.qualified.classname.ParkingEntry(p.arrivalDate, p.departureDate,
(case when p.chargedAmount is NULL then 0 else p.chargedAmount end) as chargedAmount)
 
from fully.qualified.classname.ParkingEntry p
Entity ParkingEntry:-
#Entity
class ParkingEntry {
Date arrivalDate;
Date departureDate;
BigDecimal chargedAmount;
ParkingEntry(Date arrivalDate, Date departureDate, BigDecimal chargedAmount) {
this.arrivalDate = arrivalDate;
this.departureDate = departureDate;
this.chargedAmount = chargedAmount;
}
...
}
I am trying to get arrivalDate, departureDate, and chargedAmount from the entity ParkingEntry as an Object. I want to ensure the if chargedAmount is null in the table then it should return the value as 0.
The above query has some syntax errors and hence not working. Any suggestion on how can this be achieved will be highly appreciated.

Use this:
select new fully.qualified.classname.ParkingEntry(p.arrivalDate, p.departureDate, coalesce(p.chargedAmount, 0))
 
from fully.qualified.classname.ParkingEntry p

Related

String Date in Named query generate ClassCastException

I am trying to create a named query for all the stringified arguments. This was how my stringified method was:
public Collection<Companypermcache> findExpiredPerms() {
String date = DateUtils.getSQLDate(DateUtils.getToday());
StringBuffer qbe = new StringBuffer("select cpc from Companypermcache cpc");
qbe.append(" where cpc.expire < '"+date+"'");
return super.findByQuery(qbe.toString());
}
I convert this to named query like below
String date = DateUtils.getSQLDate(DateUtils.getToday());
StringBuffer qbe = new StringBuffer("select cpc from Companypermcache cpc");
qbe.append(" where cpc.expire < :date");
return super.findByQuery(qbe.toString(),"date",date);
}
But this generates
java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Date
at org.hibernate.type.descriptor.java.JdbcTimestampTypeDescriptor.unwrap(JdbcTimestampTypeDescriptor.java:24)
at org.hibernate.type.descriptor.sql.TimestampTypeDescriptor$1.doBind(TimestampTypeDescriptor.java:48)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:74)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:253)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:248)
at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:52)
at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:627) ...
Exception.
However If I update the named query like below I don't have any exception and everything looks good.
String date = DateUtils.getSQLDate(DateUtils.getToday());
DateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
Date edate = simpleDateFormat.parse(date);
StringBuffer qbe = new StringBuffer("select cpc from Companypermcache cpc");
qbe.append(" where cpc.expire < :date");
return super.findByQuery(qbe.toString(),"date",edate);
}
However I really did not understand the difference and and the need of parsing.I have many place in my project where I am using getSQLDate(), So I am concerned whether I want to parse all those dates?
In addition to that type for edate in my table is DATETIME
Why we need to parse the Date when we pass a date as argument to named query????

JPA Query Column Specification [duplicate]

I just wanted to know how to pass column name and its value to #Query annotation in Spring Data JPA.
Basically column names will be static and we used to put every column as a element in Entity class. But here I want something different, here column name will be dynamic I will be passing this value as Parameter to the method defined in repository.
Table - Calendar
Columns - id, PersonName, 1, 2, 3......31
Above is the table structure, 1,2,3,.....31 are the column names which represents calendar days and we have values in that columns. I'm using Spring Data JPA to fetch data from DB.
Here I just wanted to fetch person name for a particular day.
Below given the function defined in repository.
#Query("select c from Calendar c where :calendarDay=:value")
List<Calendar> getPersonName(#Param("calendarDay") String calendarDay, #Param("value") String value);
This is not working for me.
Any help would be appreciated.
The only dynamic parameter Spring JPA supports is #{#entityName}. Dynamic column names in #Query annotations are not supported., and that is what you are trying to accomplish.
Your only option is to construct a query manually using either QueryDSL, Specifications or Criteria API or simply by building a query string and passing it to your EntityManager. Regardless, you'll have to write code for that.
See, for instance:
https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
How to add custom column name Spring Data JPA?
Take a look at sping data Specifications. You can find your solution there!
Reading the docs you can see that if Calendar is your domain (I would try to find a different name for my domain, there is a Calendar class in Java SE already), then you could use something like the above,
#Repository
public interface CalendarRepository extends JpaRepository<Calendar, Integer>, JpaSpecificationExecutor<Calendar> {
}
public class CalendarSpecification implements Specification<Calendar> {
private String randomColumnName; // A varchar column.
private String valueToSearchFor;
public CalendarSpecification(String randomColumnName, String valueToSearchFor) {
this.randomColumnName = randomColumnName;
this.valueToSearchFor = valueToSearchFor;
}
#Override
public Predicate toPredicate(Root<Calendar> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.and(builder.equal(root.<String>get(this.randomColumnName), this.valueToSearchFor));
}
}
#Service
public class CalendarService {
#Autowired
private CalendarRepository calendarRepository;
public List<Calendar> findCustom(String randomColumnName, String valueToSearchFor) {
CalendarSpecification cs = new CalendarSpecification(randomColumnName, valueToSearchFor);
return calendarRepository.find(cs);
// Or using lambda expression - without the need of CalendarSpecification class.
// return calendarRepository.find((Root<ProductCategory> root, CriteriaQuery<?> query, CriteriaBuilder builder) -> {
// return builder.and(builder.equal(root.<String>get(randomColumnName), valueToSearchFor));
// });
}
}
Maybe you can use CASE, WHEN.
SELECT
Id,
PersonName,
CASE
WHEN ? = 'day_01' THEN day_01
WHEN ? = 'day_02' THEN day_02
WHEN ? = 'day_03' THEN day_03
WHEN ? = 'day_04' THEN day_04
WHEN ? = 'day_05' THEN day_05'
ELSE 0
END
AS Value FROM Calendar
Java Code
// customize entity
public interface ITask {
Long getId();
String getName();
String getValue();
}
#Repository
public interface CalendarRepository {
static final String CASE_WHEN = "\nCASE\n"
+ " WHEN :field = 'day_01' THEN day_01\n"
+ " WHEN :field = 'day_02' THEN day_02\n"
+ " WHEN :field = 'day_03' THEN day_03\n"
+ " WHEN :field = 'day_04' THEN day_04\n"
+ " WHEN :field = 'day_05' THEN day_05\n"
+ " ELSE 0\n"
+ "END\n";
#Query(nativeQuery = true, value = "SELECT Id, PersoneName, " + CASE_WHEN + " AS Value FROM Calendar WHERE field = :field")
public List<ITask> findValues(#Param(value = "field") String field);
}

JPA #Convert issue with timestamp

I want to encrypt some data stored in a MySQL database using the JPA #Convert options with an AES algorithm. In general, is working fine with all fields, but I am having issues with one of them that is a Timestamp. My version of Hibernate is 4.3.8.Final.
As it is my first time with converters, I am following this GiT example. For this test, the AES encryption is disabled, I will enable it later and is the reason which I want to convert some fields to String. Therefore the issue must be in the converter.
The entity stores a user with several typical information (name, lastname,...) and a birthdate that is stored as a Timestamp. Also, as I want to perform some search by birthdate, I remove all hours, seconds and milliseconds for birthdate in the entity in the setter.
public class User {
#Column(length = 100, nullable = false)
#Convert(converter = StringCryptoConverter.class)
private String firstname;
....
#Column(nullable = false)
#Convert(converter = TimestampCryptoConverter.class)
private Timestamp birthdate;
public void setBirthdate(Timestamp birthdate) {
// Remove time from birthdate. Is useless and can cause troubles when
// using in SQL queries.
if (birthdate != null) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(birthdate.getTime());
calendar.set(Calendar.HOUR_OF_DAY, 0); // set hour to midnight
calendar.set(Calendar.MINUTE, 0); // set minute in hour
calendar.set(Calendar.SECOND, 0); // set second in minute
calendar.set(Calendar.MILLISECOND, 0); // set millis in second
this.birthdate = new Timestamp(calendar.getTime().getTime());
} else {
this.birthdate = null;
}
}
....
}
The TimestampCryptoConverter.class has few methods that are very simple. In general the scope is to convert the Timestamp to string to apply later an AES algorithm, I take the time of the timestamp as a long with getTime(), and convert them to String:
#Override
protected Timestamp stringToEntityAttribute(String dbData) {
try {
return (dbData == null || dbData.isEmpty()) ? null : new Timestamp(Long.parseLong(dbData));
} catch (NumberFormatException nfe) {
Logger.errorMessage("Invalid long value in database.");
return null;
}
}
#Override
protected String entityAttributeToString(Timestamp attribute) {
return attribute == null ? null : attribute.getTime() + "";
}
This is a very simple code. And I can store the entity into the database correctly, and retrieve it from database correctly, for example, if I get the user by ID. Therefore, the converter must be correct.
The stored data into MySQL is something like:
# id, birthdate, firstname, ...
'1', '1525384800000', 'TEST', ...
If I search the user by any field, I retrieve the entity with all data correctly converted. The issue appears when I want to perform a search from the birthdate. For example, in my DAO, I have a method is:
public List<User> get(String email, Timestamp birthdate) {
// Get the criteria builder instance from entity manager
CriteriaBuilder criteriaBuilder = getEntityManager().getCriteriaBuilder();
CriteriaQuery<User> criteriaQuery = criteriaBuilder.createQuery(getEntityClass());
// Tell to criteria query which tables/entities you want to fetch
Root<User> typesRoot = criteriaQuery.from(getEntityClass());
List<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(criteriaBuilder.equal(typesRoot.get("email"), email));
if (birthdate != null) {
// Remove hours and seconds.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(birthdate.getTime());
calendar.set(Calendar.HOUR_OF_DAY, 0); // set hour to midnight
calendar.set(Calendar.MINUTE, 0); // set minute in hour
calendar.set(Calendar.SECOND, 0); // set second in minute
calendar.set(Calendar.MILLISECOND, 0); // set millis in second
birthdate = new Timestamp(calendar.getTime().getTime());
predicates.add(criteriaBuilder.equal(typesRoot.<Timestamp> get("birthdate"), birthdate));
}
criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[] {})));
return getEntityManager().createQuery(criteriaQuery).getResultList();
}
As you can see, I also remove the hours, seconds and milliseconds from the search query to match the value on the database.
If I call this method with only the email get('test#email.com', null), it works fine, as before the user is retrieved and the birthdate is correct in the user.
But if I call this method with the birthdate get('test#email.com', 2018-05-04 12:09:05.862) then the result obtained is null. In some unitary tests, the timestamp used in the call is exactly the same parameter used for the creation of the user, and therefore must match the value on the database. For example, I have this unitary tests:
#Test(dependsOnMethods = { "storeUser" })
#Rollback(value = false)
#Transactional(value = TxType.NEVER)
public void searchByMailUser() {
Assert.assertEquals(userDao.getRowCount(), 1);
List<User> dbUsers = userDao.get(EMAIL, null);
Assert.assertTrue(!dbUsers.isEmpty());
User dbUser = dbUsers.iterator().next();
Assert.assertEquals(dbUser.getFirstname(), FIRSTNAME);
Assert.assertEquals(dbUser.getEmail(), EMAIL);
....
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(BIRTHDATE.getTime());
calendar.set(Calendar.HOUR_OF_DAY, 0); // set hour to midnight
calendar.set(Calendar.MINUTE, 0); // set minute in hour
calendar.set(Calendar.SECOND, 0); // set second in minute
calendar.set(Calendar.MILLISECOND, 0); // set millis in second
Timestamp birthdate = new Timestamp(calendar.getTime().getTime());
Assert.assertEquals(dbUser.getBirthdate(), birthdate);
}
That is executed fine, and the last assert tell me that the birthdate is stored and retrieved correctly. But in this test:
#Test(dependsOnMethods = { "storeUser" })
public void searchByMailAndBirthdateUser() {
Assert.assertEquals(userDao.getRowCount(), 1);
Assert.assertTrue(!userDao.get(EMAIL, BIRTHDATE).isEmpty());
}
This test is failed due to no users are found, but passed if changed to:
#Test(dependsOnMethods = { "storeUser" })
public void searchByMailAndBirthdateUser() {
Assert.assertEquals(userDao.getRowCount(), 1);
Assert.assertTrue(!userDao.get(EMAIL, null).isEmpty());
}
BUT, if I disable the converter, both tests are passed.
If the birthdate is correctly retrieved from the database. Why I am having a null value when using birthdate as a criteria?
EDIT
Seems that the method protected String entityAttributeToString(Timestamp attribute); is not used when calling get('test#email.com', 2018-05-04 12:09:05.862).
After some research. It is possible that still Hibernate has some bugs when filtering criteria with an attribute with #Convert. I have make several tests with different options with no success at all.
As a workaround, I have changed the birthdate attribute as a Long.
#Column(nullable = false)
#Convert(converter = LongCryptoConverter.class)
private Long birthdate;
Updating the setters and getters to convert the Timestamp to Long with getTime()
And creating a Long CryptoConverter with very simple methods for converting a Long to String and the opposite:
#Override
protected Long stringToEntityAttribute(String dbData) {
try {
return (dbData == null || dbData.isEmpty()) ? null : Long.parseLong(dbData);
} catch (NumberFormatException nfe) {
UsmoLogger.errorMessage(this.getClass().getName(), "Invalid long value in database.");
return null;
}
}
#Override
protected String entityAttributeToString(Long attribute) {
return attribute == null ? null : attribute.toString();
}
This is working as expected and the criteria filter now is working fine. Still I am not sure why the version with a Timestamp is not working fine.
After some effort, I have changed the version of Hibernate to 5.2.17.Final and the Timestamps are handled correctly with the #Convert annotation. That means that is really an Hibernate bug with version 4.X and this feature is better implemented in Hibernate 5.X

Perform select of data from multiple tablea using JDBC template

I need to perfom a select by date from my DB in my spring boot webapp. What I have so far is a list of sport competitions and there respective informations.
Problem : I can not figure out how my select query convert my String type (dateFrom = '2017-05-02' and dateTo = '2017-05-06') to date like '2017-02-12' in the ?
Alos how to fill my RowMapper with more then one date in some competition which have more then one date.
My data base schema:
CREATE TABLE competition (
competition_id integer PRIMARY KEY,
nom varchar(128) NOT NULL,
);
CREATE TABLE date (
id integer PRIMARY KEY,
date_time timestamptz,
competition_id integer REFERENCES competition (competition_id)
);
Json data:
{
"id": "420",
"name": "SOCCER",
"dates": [
"2016-05-12T03:00:00.000Z"
"2016-05-12T04:00:00.000Z"
"2016-05-12T05:00:00.000Z"
]
},
{
"id": "220",
"name": "BASKETBALL",
"dates": [
"2016-05-12T03:00:00.000Z"
"2016-05-12T04:00:00.000Z"
]
}
My competition Class:
public class Competition{
private int id;
private String name;
private String[] dates;
// setters ... getters
}
My RowMapper Class:
public class RowMapper implements RowMapper
{
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
Competition competition = new Competition();
competition.setId(rs.getInt("id"));
competition.setName(rs.getString("name"));
competition. // How to fill dates
return competition;
}
}
Function to select data :
private static final String SELECT_STMT =
" select * from competition INNER JOIN date ON
+ " competition.competition_id = date.competition_id"
+ " WHERE date(date.date_time) BETWEEN ? AND ?"
;
public List<Competition> findByOptionsAll(String dateFrom, String dateTo ){
List<Competition> competitions = jdbcTemplate.query(SELECT_STMT, new
RowMapper(), dateFrom, dateTo);
return competitions ;
}
Date converting
Right now you have all dates as a String both in your DB and domain model. To convert strings to date you need a date formatter:
private static final String DATE_FORMAT = "dd-MM-yy";
// parsing date; Note you should handle ParseException
java.util.Date date = new SimpleDateFormat(DATE_FORMAT).parse(dateAsString);
// converting date to string
String dateAsString = new SimpleDateFormat(DATE_FORMAT).format(date);
Note that SimpleDateFormat is not thread-safe so it's a good practice to have static final String DATE_FORMAT instead of static final DateFormatter
Converting date and time is tricky in some cases (what about time zone? java.util.Date vs joda.time vs LocalDate from Java 8) but out of scope. I suggest use LocalDate if possible just because it's a new way without old issues.
Mapping
You have two entities in your DB (Competition and Date-of-competition) and only one class Competition in your domain model. Most probably, later you'll want to add additional info to the Date-of-competition (boolean finished, cancelled, Score etc) so it's a good idea to create CompetitionInstance class right now.
Since you have One-to-Many relationship you have to write some additional stuff to map objects. Normally that's what an ORM like Hibernate do istead of you. First, add a 'GROUP BY competition_id' in your sql statement.
Then use RowSetExtractor instead of RowMapper as described here:
private static final class CompetitionMapExtractor implements ResultSetExtractor<List<Competition>> {
#Override
public List<Competition> extractData(ResultSet rs) throws SQLException {
List<Competition> result = new ArrayList<>(rs.getCount());
int previousCompetitionId = NEVER_EXIST; // normally -1 is good enough
while (rs.next()) {
// we have some dates with the same competition_id
// dates are grouped thanks to GROUP BY clause
if ( rs.getInt("id") != previousCompetitionId) {
Competition currentCompetition = new Competition(rs.getInt("id"),
rs.getString("name");
/* I prefer constructor initializers "o = new O(propertyValue)"
instead of snippet "o = new O(); o.setProperty(value)"
*/
result.add(currentCompetition);
previousCompetitionId = currentCompetition.getid();
} else {
currentCompetition.addDate(new CompetitionInstance(rs.getString("date")));
}
}
return result;
}
I suppose Competition has method public void addDate(String date) which simply add a new CompetitionInstance to a list.
Update:
1.
column name in DB and in MapExtractor is different. I prefer to change the query:
SELECT c.id, c.name, d.date_time as date
from competition c
INNER JOIN date d ON c.competition_id = d.competition_id
WHERE date(d.date_time) BETWEEN ? AND ?"
2. I can't reproduce issues you have with date. Most probably you mixed up java.util.Date, java.sql.Date and java.sql.Timestamp - this is a common mistake. There are many answers already, probably you could find one of them useful.

In neo4j, how can I index by date and search in a date range?

In neo4j, how can I index by date and search in a date range. Also for times, I would like to search between 8am and 9am in a date range as well.
Index the dates and times as integer timestamps. Then you can easily search in an index for dates between other timestamps. You can also index the time part of the timestamp separately as another integer, allowing you to query for specific times between given dates.
Example:
The date and time to store is "2012-02-05 8:15 AM"
So in your index, store "timestamp=1328447700" and "time=815"
Now you want to query the index for all events between 2012-02-01 and 2012-02-10 that occurred from 8:00 am to 9:00 am. You do that by querying the index for
"timestamp>=1328072400 and timestamp<=1328936399 and time>=800 and time<=900"
The exact syntax for doing this depends on how you are connecting to Neo4j (REST or embedded) and which programming language you are using. But the idea is the same in any case.
There's a convenient org.neo4j.index.lucene.LuceneTimeline which does this (using an integrated lucene index in neo4j).
This is an extension to Josh Adell's answer. For readability, I suggest having two date and time integer fields like
date:19970716 (YYYYMMDD)
time:203045000 (HHmmssuuu): last three digits for microseconds.
The int datatype can store upto 2147483647. If you are feeling adventurous, the long datatype can store upto 9223372036854775807.
http://docs.neo4j.org/chunked/stable/graphdb-neo4j-properties.html
Inspired from ISO 8601 timestamps like 1997-07-16T19:20:30.45Z.
Disclaimer: I have only minimal experience with Neo4J.
with Spring data neo4j
public List<Email> getAllEmailData(Date startDate, Date endDate) {
List<Email> list = new ArrayList<Email>();
if (startDate == null || endDate == null) {
return null;
}
long first = ConversionsUtils.convertDateToLong(startDate);
long second = ConversionsUtils.convertDateToLong(endDate);
try {
list = emailRepository.searchAllData(first, second);
// System.out.println("List size " +list.size());
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
#Query(
"START email=node:__types__(className='com.backend.core.neo.entities.Email') "
+ "WHERE email.searchDate > {0} and email.searchDate < {1}"
+ "RETURN email")
List<Email> searchAllData(long startDate, long endDate);
email entity
#NodeEntity
public class Email implements Serializable {
private static final long serialVersionUID = 1L;
public static final String CC = "CC";
public static final String TO = "TO";
#GraphId
private Long id;
#GraphProperty
private Long senderId;
#GraphProperty
private String subject;
#Indexed
// #GraphProperty(propertyType = java.util.Date.class)
private String dateSent;
#Indexed
private long searchDate;
#GraphProperty
private String emailTxt;
#GraphProperty
private String emailHtml;
#GraphProperty
private String emailId;
//mail to
#Fetch
#RelatedTo(elementClass = User.class, type = TO, direction = Direction.OUTGOING)
private Set<User> intoUsers;
//mail shared
#Fetch
#RelatedTo(elementClass = User.class, type = CC, direction = Direction.OUTGOING)
private Set<User> sharedUsers;

Categories