简体   繁体   English

Hibernate和Serializable

[英]Hibernate and Serializable

I'm a bit confused, I was under the impression that classes will only need to implement serializable, well, if I need to serialize them (as in, hibernate doesn't require classes to be serializable if all I want is persistance) 我有点困惑,我的印象是类只需要实现serializable,好吧,如果我需要序列化它们(就像在,hibernate不需要类可序列化,如果我想要的只是持久性)

Anyways, the following got me a bit confused. 无论如何,以下让我有点困惑。 I have a class, lets call it A. Which have a one to one relation to another class. 我有一个类,让我们称之为A.与另一个类有一对一的关系。 Lets call it B. 我们称之为B.

I have now added another one-to-one relation to class C as well, and now, I get a run time exception when i use mySession.get(A.class, myid), telling me that C cant be cast to Serializable 我现在已经添加了另一个与C类的一对一关系,现在,当我使用mySession.get(A.class,myid)时,我得到一个运行时异常,告诉我C不能被强制转换为Serializable

I cant make any sence of why C needs to have serializable implemented when none of my other hibernate classes needs it. 当我的其他任何hibernate类都不需要它时,我无法理解为什么C需要实现serializable。

Update 更新

Another weird thing is, this mapping have actually worked without the "C" class being serializable in the past. 另一个奇怪的事情是,这个映射实际上没有“C”类在过去可序列化。

I did a manual change of the data inside the table though, using the mysql client. 我使用mysql客户端手动更改了表中的数据。 I ran a update which converted all strings in the table to uppercase using the UPPER() function. 我运行了一个更新,它使用UPPER()函数将表中的所有字符串转换为大写。 Could this have been what made hibernate want the class to be serializable now? 这可能是因为hibernate希望这个类现在可以序列化吗?

that error is common when you forgot make the mapping to a persisting object of your class, 当您忘记将映射映射到类的持久对象时,该错误很常见,

for example you have a persisting class User inside have another persisting class Address, Address is mapped to but in User you forgot make the relation one to one then you mapped as Basic type 例如你有一个持久化类用户里面有另一个持久化类的地址,地址被映射到用户你忘了把关系一对一然后你映射为基本类型

check you code for any forgiven mapping of relationship 检查代码是否有任何原谅的关系映射

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM