我在使用 Hibernate 和 SQL Server 2008 时遇到问题。当我尝试将对象保存到数据库时,Hibernate 会抛出以下错误:
could not retrieve snapshot: com.my.MyClass
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid object name `'emanagement.patient_visit'.`
用户在数据库中具有选择、插入、更新权限。所以我排除了这个问题。
这是生成的 SQL:
select
patientvis_.account_number,
patientvis_.status as status1_,
patientvis_.cpt_code as cpt3_1_,
patientvis_.locked as locked1_,
patientvis_.state as state1_,
patientvis_.userid as userid1_
from
emanagement.patient_visit patientvis_
where
patientvis_.account_number=?
如果我在 SQL Server 中运行上述 SQL,它会显示无效的对象名称 emanagement.patent_visit,但如果我手动添加“dbo”emanagement.dbo.patent_visit,它将被执行。
那么我还需要进行其他 Hibernate 配置吗?
这是我的 Hibernate 映射。下面的映射在 MySQL 下工作。我可以读取并更新数据库中的病人访问。但当切换到 MS Server 时,它失败了。
我尝试过其他适用于 MySQL 和 MS Server 的休眠映射,它们都使用与下面相同的声明,如 table="db_table" schema="my_database"。
唯一的区别是我在 MS Server 下创建了这个新的电子管理数据库,所以我认为我错过了 MS Server 管理工具上的一些特定数据库配置。
证明这一点的唯一方法是将新表从电子管理移动到现有数据库,看看它是否有效。
<class name="com.domain.patient.model.PatientVisit" table="patient_visit" schema="emanagement">
<id name="accountNumber" type="java.lang.Long">
<column name="account_number" precision="22" scale="0" />
<generator class="assigned"/>
</id>
<property name="status" type="string">
<column name="status"/>
</property>
<property name="cptCode" type="string">
<column name="cpt_code"/>
</property>
<property name="locked" type="boolean">
<column name="locked" precision="1" scale="0"/>
</property>
<property name="state" type="string">
<column name="state"/>
</property>
<property name="userId" type="string">
<column name="userid"/>
</property>
<set name="documents" lazy="false">
<key column="account_number"/>
<one-to-many class="com.domain.document.model.Document"/>
</set>
</class>
提前致谢。