java 使用 Hibernate 和 SQL Server 2008 的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3159662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem using Hibernate and SQL Server 2008
提问by Marquinio
I'm having problems using Hibernate and SQL Server 2008. When I try to save an object to database Hibernate throws this:
我在使用 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'.`
The user has select, insert,update privileges in database. So I ruled that problem out.
用户在数据库中具有选择、插入、更新权限。所以我排除了这个问题。
This is the SQL generated:
这是生成的 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=?
If I run the above SQL in SQL Server it says invalid object name emanagement.patient_visit, but if I manually add that "dbo" emanagement.dbo.patient_visit than it will get exsecuted.
如果我在 SQL Server 中运行上述 SQL,它会显示无效的对象名称 emanagement.patient_visit,但如果我手动添加该“dbo”emanagement.dbo.patient_visit,它会被排除。
So is there any other Hibernate configuration I need to make?
那么我需要进行任何其他 Hibernate 配置吗?
This is my Hibernate mapping. The below mapping works under MySQL. I can read and update patient_visit in database. But when switching to MS Server it fails. I have tried other hibernate mappings which work for both MySQL and MS Server and they both use the same declarations as below like table="db_table" schema="my_database". The only difference is that I created this new emanagement database under MS Server, so I'm thinking that I missed some specific database configuration on the MS Server management tool. The only way to prove this is for me to move the new tables from emanagement to an existing database and see if it works.
这是我的 Hibernate 映射。以下映射适用于 MySQL。我可以读取和更新数据库中的patient_visit。但是当切换到 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>
Thanks in advance.
提前致谢。
采纳答案by Pascal Thivent
So is there any other Hibernate configuration I need to make?
那么我需要进行任何其他 Hibernate 配置吗?
With your current setup, I guess you'll have to specify the schema. For example, in the mapping:
使用您当前的设置,我想您必须指定架构。例如,在映射中:
<class name="Users" table="Users" schema="dbo" catalog="suiteaccess">
But you can also specify the default schema using the hibernate.default_schemaproperty (see 3.4. Optional configuration properties).
但您也可以使用hibernate.default_schema属性指定默认架构(请参阅3.4. 可选配置属性)。
Just in case, you can create your own schema.
以防万一,您可以创建自己的架构。
回答by Anon246
Change the schema to dbo. In SQL Server, schema is a container in the database, not the database itself. This may be different in MySQL, but in this case, your schema should be dbo (based on your comment about it working if you add "dbo." to your resultant query.
将架构更改为 dbo。在 SQL Server 中,架构是数据库中的容器,而不是数据库本身。这在 MySQL 中可能有所不同,但在这种情况下,您的架构应该是 dbo(根据您对它的评论,如果您将“dbo.”添加到您的结果查询中。
Thanks, Eric
谢谢,埃里克
回答by Murali
Put below configuration into mapping file. It resolved the Invalid object exception.
将以下配置放入映射文件中。它解决了无效对象异常。
<class name="Users" table="databaseName.dbo.Users">
Change the above line into Mapping file. It should be work. Because Mssql server have default schema (dbo). That's why use the fully classified table name.
将上面的行更改为映射文件。应该是工作。因为 Mssql 服务器具有默认架构 (dbo)。这就是为什么使用完全分类的表名。

