4

Contrary to what's possible with the Java API, there doesn't seem to be a way to specify whether a numeric property is a byte, short, int or long:

CREATE (n:Test {value: 1}) RETURN n

always seems to create a long property. I've tried toInt(), but it is obviously understood in the mathematical sense of "integer" more than in the computer data type sense.

Is there some way I'm overlooking to actually force the type?

We have defined a model and want to insert test data using Cypher statements, but the code using the data then fails with a ClassCastException since the types don't match.

Frank Pavageau
  • 11,477
  • 1
  • 43
  • 53

1 Answers1

3

If you run your cypher queries with the embedded API then you can provide parameters in a hashmap with the correctly typed values.

For remote users it doesn't really matter as it goes through JSON serialization back and forth which looses the type information anyway. So it is just "numeric".

Why do you care about the numeric type?

you can also just use ((Number)n.getProperty("value")).xxxValue() (xxx = int,long,byte)

Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • I cared about the types because Neo4j supports different types itself, so it would have been useful to actually use the most appropriate one to avoid wasting (disk) space, for example. Also it seems a bit overkill to use a `long` in the code to represent a rounded percentage, so we'd been using `int` for that (yes, I know it fits in a `byte`). What you say about JSON serialization is enlightening, actually, because it means we'll actually end up with `long` storage for everything as the data is inserted using Spring Data Neo4j in remote mode. I guess we'll directly go with `long`s everywhere. – Frank Pavageau Jan 27 '15 at 13:39
  • As a side note, using `Number.xxxValue()` is a bit heavier and less readable than just `(int) n.getProperty("value")`. – Frank Pavageau Jan 27 '15 at 13:41