tags:

views:

57

answers:

1

Hi guys,

I have a query that looks like this

Document document = DocDAO.getSession().read(someId);
MyFile file = (MyFile) DAO.getSession()
.createQuery("SELECT file FROM MyFile file WHERE file.document = :document AND file.version = :version")
.setParameter("document", document)
.setParameter("version", version)
.uniqueResult();

Now, I should get a file where file.getDocument().getId() == someId, and someId is a BigInteger. But unfortunately, they are not equal. What kind of errors can lead to the queried entity not being the entity I was looking for?

Cheers

Nik

+1  A: 

BigInteger is an object, not a primitive. You may need to do bigInt1.equals(bigInt2) rather than bigInt1 == bigInt2.

EDIT:
Maybe I'm wrong about that. Javadoc says:

BigInteger provides analogues to all of Java's primitive integer operators

EDIT AGAIN: If you want to do away with BigInteger, try declaring your JPA entity attribute as a Long (or long if it is not nullable) and then use the @Column annotation to define what the actual database column structure is if necessary. Here is an example of this annotation:

@Column(updatable = false, name = "MY_DB_COLUMN", nullable = false, length=12)
private long myEntityAttribute;

I don't remember exactly how to use the length attribute when the column is numeric. Max number of digits? You can look up the details for @Column and experiment until you get it right.

Jim Tough
I haven't used BigInteger, but I'm pretty sure you were right the first time. "Provides analogs" isn't the same thing as "overrides the `==` operator" - which you can't do in Java.
Matt Ball
So, what should I use instead of bigint in MySQL, or BigInteger? I'd love to use a Long, but I don't know what that would map to in MySQL, and my IDs are too long for an integer
niklassaers
You can override the default mapping from database type to Java entity type. I generated the JPA entities on my current project from Eclipse, and I think BigInteger was the default for non-decimal numeric columns. I just changed them to long (or Long) without modifying the database schema. You'll only have a problem if a database column value exceeds Long.MAX_VALUE, which is very large.
Jim Tough
JPA doesn't perform any `==` nor `equals` comparison (I think that the OP was just trying to explain the expected result).
Pascal Thivent
I've updated everything to use Longs instead of BigIntegers, but the problem seems to be identical, so perhaps it can be something else that can typically be causing these kind of things?
niklassaers
@niklassaers It *is* definitely something else, there is no reason why using a `Long` would change anything. Activate the logging of the generated SQL and run it against your database as suggested.
Pascal Thivent
I agree with Pascal. If the generated SQL isn't right then you'll know that your entity definitions need to change.
Jim Tough