4

I'm using Java 6, Hibernate 4.1.0.Final, and Spring 3.1.1.RELEASE. I have a String (maximum of 32 characters) that I wish to convert to a number and then back again to a String. I want to know what MySQL (v 5.5) data type I should use to store the number (which I think is a BigInteger) and I would also like to know if there's a better way to do the conversion. Right now, I'm doing

// Convert to integer
final BigInteger bigInt = new BigInteger(myString.getBytes());

// Convert back to a string
final String oldString = new String(bigInt.toByteArray());

The Hibernate mapping on the field in question is

@Column(name = "ORDER_ID")
private BigInteger orderId;

Thanks for your help, -

2
  • Could you specify the format of the String? Is it a numeric string, e.g. "12345", or a sequence of byte integers? Commented Jun 18, 2013 at 19:12
  • The string may contain numbers, but not necessarily. It will be a GUID -- so a 32 character sequence in which each character is either a number of a letter from "A" to "F". Commented Jun 19, 2013 at 13:19

1 Answer 1

1

Look at the NUMERIC datatype. I've used that to store very large decimal numbers up to 39 digits long.

The database field has a type of "NUMERIC(39)".

The hibernate mapping used was:

<property name="foo" type="big_integer" column="ORDER_ID" length="39"/>

To convert a string to a BigInteger from a hex string, use:

BigInteger bigInt = new BigInteger(myString, 16);
Sign up to request clarification or add additional context in comments.

3 Comments

Tried that and it didn't work. Got error "org.hibernate.exception.DataException: Data truncation: Out of range value for column 'ORDER_ID' at row 1"
Updated my answer with hibernate and mysql particulars. What mapping and column types did you try it with?
I found that NUMERIC(50) took care of things (I was still getting some errors the other way). Added the Hibernate mapping for posterity.

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.