2

I have two columns in my table are which is BigInt data type (NODEID and ULNODEID) and I want to keep it that way. I am using MYSQL workbench 8.0 for these table.

I want to get the value of my nodeid using the function below:

 public long get_urlnodeid(long nodeID) {
        try {


                String sql = "select NODEID from urllink where ULNODEID="+nodeID;
            if (em == null) {
                throw new Exception("could not found URL object.");
            }

            return (long) em.createNativeQuery(sql).getSingleResult();


        } catch (Exception e) {

            msg = CoreUtil.wrapMsg(CoreUtil.FUNC_ERROR,
                    this.getClass().getName(), "get", e.getMessage());

        }
        return 0;
    }

It throws an exception saying Big Integer cannot be cast to java.lang.Long

Is there a way I can retrieve the value while keeping it in long?

9
  • Is there something insufficient about bigInt.longValue()? Commented Apr 5, 2019 at 4:42
  • yes: ((Number)em.createNativeQuery(sql).getSingleResult()).longValue() Commented Apr 5, 2019 at 4:43
  • There's nothing insufficnet, just the fact that I want to use BigInt as a data type in my column and expected to cast the value to a long bothers me. Commented Apr 5, 2019 at 4:44
  • @MauricePerry casting to Number won't work. Commented Apr 5, 2019 at 4:44
  • @AniketSahrawat why not? Commented Apr 5, 2019 at 4:46

1 Answer 1

4

Just look at the Java doc for BigInteger:

public long longValue()

Converts this BigInteger to a long. This conversion is analogous to a narrowing primitive conversion from long to int as defined in section 5.1.3 of The Java™ Language Specification: if this BigInteger is too big to fit in a long, only the low-order 64 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.

So you'd want something like this:

return ((BigInteger)em.createNativeQuery(sql).getSingleResult()).longValue();

I would recommend adding some type checking.

--

Another option, if you have full control of your application, and you expect values that go beyond the range of long, is to have your method return BigInteger instead of long:

public BigInteger get_urlnodeid(long nodeID) {

And:

return (BigInteger) em.createNativeQuery(sql).getSingleResult();

Of course then the rest of your application that calls this method has to work with BigInteger as well.

Please be aware that using BigInteger instead of long is much less performant, so only use this if performance is not an issue or if you are absolutely sure that values will be so big that this is absolutely necessary.

Sign up to request clarification or add additional context in comments.

4 Comments

So it converts the BigInt value to long?
Well, might sound pedantic, but it's not a cast, it's a conversion. But yes, the value will be converted into long.
Would it be more convenient to use BigInt directly without any sort of conversion?
@Daredevil Sure. Just return BigInteger directly (see edit).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.