我对Hibernate很新,因为我总是使用普通的JDBC。我正在尝试使用NetBeans对SQL Server数据库的逆向工程过程来配置实体。我已经关注了NetBeans的网页教程,我可以在我的项目中创建一个包含所有实体的新包,但是当我在HQL编辑器中运行这个简单的查询from Country
时,我收到此错误:
org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:217)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:240)
at org.hibernate.type.SerializableType.fromBytes(SerializableType.java:82)
at org.hibernate.type.SerializableType.get(SerializableType.java:39)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:163)
at org.hibernate.type.NullableType.nullSafeGet(NullableType.java:154)
at org.hibernate.type.AbstractType.hydrate(AbstractType.java:81)
at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2096)
at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1380)
at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1308)
at org.hibernate.loader.Loader.getRow(Loader.java:1206)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:580)
at org.hibernate.loader.Loader.doQuery(Loader.java:701)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
Caused by: java.io.StreamCorruptedException: invalid stream header: 32003300
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:782)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:279)
at org.hibernate.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:252)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:209)
... 23 more
Country.hbm.xml文件包含以下内容:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 08-may-2014 0:02:21 by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="Entity.Country" table="Country" schema="dbo" catalog="TestOTDB">
<id name="countryId" type="string">
<column name="Country_ID" length="36" />
<generator class="assigned" />
</id>
<property name="countryName" type="serializable">
<column name="Country_Name" not-null="true" />
</property>
<property name="countryDescription" type="serializable">
<column name="Country_Description" />
</property>
<property name="countryCode" type="serializable">
<column name="Country_Code" not-null="true" />
</property>
<set name="sites" inverse="true">
<key>
<column name="Site_CountryID" length="36" />
</key>
<one-to-many class="Entity.Site" />
</set>
</class>
</hibernate-mapping>
问题是所有那些出现“可序列化”的类型应该是我的SQL Server数据库中的varchar。 有人有任何想法吗?
谢谢!
答案 0 :(得分:1)
我终于找到了解决这个问题的方法。看起来在某些情况下应强制使用hibernate数据类型(我不确定它依赖于什么......),在我的情况下,应该强制来自SQL Server的所有字符串和/或文本列通过将java.lang.String
添加到hibernate.reveng.xml文件来type-mapping
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<schema-selection match-catalog="DB_Name" match-schema="dbo"/>
<type-mapping>
<sql-type jdbc-type="VARCHAR" hibernate-type="java.lang.String" />
<sql-type jdbc-type="NVARCHAR" hibernate-type="java.lang.String" />
<sql-type jdbc-type="NCHAR" hibernate-type="java.lang.String" />
</type-mapping>
<table-filter match-name="Table1"/>
<table-filter match-name="Table2"/>
<table-filter match-name="TableN"/>
</hibernate-reverse-engineering>
希望这对其他人有帮助。