English 中文(简体)
Java、(预报)可射物体与其他物体
原标题:Java, convert(cast) Serializable objects to other objects

I m using Session.save() method (in Hibernate) to persist my entity objects which returns an object of type java.io.Serializable.

The returned value is the generated primary key for the entity.

The generated primary key is of type long (or bigint).

问题是:我如何将返还价值转换成long?

Serializable result = session.save(myEntityObject);

//This line of code fails. 
long r = (long)result;
最佳回答

Try casting the result (since it s not a primitive) to Long instead of long.

Long r = (Long)result;
long longValue = r.longValue();
问题回答

Try

long r = Long.valueOf(String.valueOf(result)).longValue();

Have you tried using a debugger and breaking at that line to see what "result" is exactly?

它可以是大Dec或长 or,或......

或者说,你只是把主要的关键采集方法称作目标,我希望到那个时候确定。

HTH, Chris

Try using long r = ((Long) result).getLong() instead.

您本应指明你正在经历的错误类型! 任何途径都应奏效。

long r = Long.valueOf(result) 

Above answer not worked for me.

I misunderstood the function of statelessSession.get(...). The second parameter should be the primitive value of the primary-key if it is a non-composte key.

如果实体拥有一个综合钥匙,你应将实体本身(填满了克)作为第二点。

I was confused, because I thought that I need to pass always an entity as second argument to statelessSession.get (because it works for multi-key-entitys).





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签