开发者

Hibernate returning another object

I have a query that looks like th开发者_StackOverflow社区is

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜