java.math.BigInteger cannot be cast to java.lang.Long

Better option is use SQLQuery#addScalar than casting to Long or BigDecimal. Here is modified query that returns count column as Long Query query = session .createSQLQuery(“SELECT COUNT(*) as count FROM SpyPath WHERE DATE(time)>=DATE_SUB(CURDATE(),INTERVAL 6 DAY) GROUP BY DATE(time) ORDER BY time;”) .addScalar(“count”, LongType.INSTANCE); Then List<Long> result = query.list(); //No ClassCastException here Related link Hibernate javadocs … Read more

Unsigned long in Java

In Java 8, unsigned long support was introduced. Still, these are typical longs, but the sign doesn’t affect adding and subtracting. For dividing and comparing, you have dedicated methods in Long. Also, you can do the following: long l1 = Long.parseUnsignedLong(“12345678901234567890”); String l1Str = Long.toUnsignedString(l1) BigInteger is a bit different. It can keep huge numbers. … Read more

Why are 2 Long variables not equal with == operator in Java?

== compares references, .equals() compares values. These two Longs are objects, therefore object references are compared when using == operator. However, note that in Long id1 = 123L; literal value 123L will be auto-boxed into a Long object using Long.valueOf(String), and internally, this process will use a LongCache which has a [-128,127] range, and 123 … Read more

Generate random values in C#

This should do the trick. (It’s an extension method so that you can call it just as you call the normal Next or NextDouble methods on a Random object). public static Int64 NextInt64(this Random rnd) { var buffer = new byte[sizeof(Int64)]; rnd.NextBytes(buffer); return BitConverter.ToInt64(buffer, 0); } Just replace Int64 with UInt64 everywhere if you want … Read more

Java: Is there a difference between L and l (lowercase L) when specifying a long?

No practical difference. Either L or l can be used, both indicate a long primitive. Also, either can be autoboxed to the corresponding Long wrapper type. However, it is worth noting that JLS-3.10.1 – Integer Literals says (in part) An integer literal is of type long if it is suffixed with an ASCII letter L … Read more