2013-11-22 149 views
0

我對Hibernate不是很有經驗,現在我在Hibernate中遇到了無效的列名問題。 我只是想選擇使用標準的API數據庫中的數據,如下:Hibernate java.sql.SQLException:無效的列名

public Subject lookupByID(SubjectValue subjectValue) { 
    List list; 
    Iterator it; 
    Criteria criteria = getBaseCriteria(); 
    criteria = criteria.add(Restrictions.eq("subjectId", subjectValue.getSubjectId())); 
    list = criteria.list(); 
    it = list.iterator(); 
    if(it.hasNext()){ 
     return (Subject)it.next(); 
    } 
    else{ 
     return null; 
    } 
} 

我啓用了休眠調試模式,現在我可以看到選擇,它產生:

Hibernate: select this_.Subject_id as Subject1_11_0_, this_.Site_id as Site2_11_0_, this_.Notification_id as Notifica3_11_0_, this_.Name as Name11_0_, this_.Description as Descript5_11_0_, this_.UserName as UserName11_0_, this_.DisplayOrder as DisplayO7_11_0_, this_.InsertDate as InsertDate11_0_, this_.insertedBy_id as insertedBy9_11_0_, this_.UpdateDate as UpdateDate11_0_, this_.updatedBy_id as updatedBy11_11_0_, this_.StatusCode as StatusCode11_0_, this_.Template_id as Template13_11_0_, this_.ForwardMail as Forward14_11_0_, this_.AutoReply as AutoReply11_0_, this_.FromName as FromName11_0_, this_.FromMail as FromMail11_0_, this_.ResponseRequired as Respons18_11_0_ from MYSCHEMA.Subject this_ where this_.Subject_id = ? 

直接在它工作正常的DB運行此查詢,但在冬眠它呈現出:

org.hibernate.exception.SQLGrammarException: could not execute query 
Caused by: java.sql.SQLException: Invalid column name 'Template_id'. 

這裏是我的XML映射:

<hibernate-mapping> 
<class name="MYPACKAGE.model.Subject" table="Subject" dynamic-update="true" > 
    <id name="subjectId" type="integer" column="Subject_id"> 
     <generator class="identity" /> 
    </id> 
    <property name="siteId" column="Site_id" type="integer" /> 
    <property name="notificationId" column="Notification_id" type="integer" not-null="false" /> 
    <property name="name" column="Name" type="string" /> 
    <property name="description" column="Description" type="string" /> 
    <property name="userName" column="UserName" type="string" /> 
    <property name="displayOrder" column="DisplayOrder" type="integer" /> 
    <property name="insertDate" column="InsertDate" type="timestamp" /> 
    <property name="insertedBy_id" column="insertedBy_id" type="integer" /> 
    <property name="updateDate" column="UpdateDate" type="timestamp" /> 
    <property name="updatedBy_id" column="updatedBy_id" type="integer" /> 
    <property name="statusCode" column="StatusCode" type="string" /> 
    <property name="templateId" column="Template_id" type="integer" /> 
    <property name="forwardMail" column="ForwardMail" type="string" /> 
    <property name="autoReply" column="AutoReply" type="boolean" /> 
    <property name="fromName" column="FromName" type="string" /> 
    <property name="fromMail" column="FromMail" type="string" /> 
    <property name="responseRequired" column="ResponseRequired" type="boolean" /> 

</class> 

我在Java 1.5.022上運行,Hibernate 3.26GA和JBOSS 6.0 EAP(非常類似於JBOSS AS7)。 任何人都可以請幫忙嗎?如果您需要任何進一步的細節,請讓我知道,我很樂意提供。

祝您有美好的一天!

編輯: 表結構:

CREATE TABLE [ITrInnoFramework].[Subject](
    [Subject_id] [int] IDENTITY(1,1) NOT NULL, 
    [Site_id] [int] NOT NULL, 
    [Notification_id] [int] NULL, 
    [Name] [nvarchar](50) NOT NULL, 
    [Description] [nvarchar](1000) NOT NULL, 
    [DisplayOrder] [int] NOT NULL, 
    [InsertDate] [datetime] NOT NULL, 
    [InsertedBy_id] [int] NOT NULL, 
    [UpdateDate] [datetime] NOT NULL, 
    [UpdatedBy_id] [int] NOT NULL, 
    [StatusCode] [char](10) NULL, 
    [UserName] [varchar](50) NULL, 
    [Template_id] [int] NULL, 
    [ForwardMail] [varchar](255) NULL, 
    [ResponseRequired] [bit] NULL, 
    [AutoReply] [bit] NULL, 
    [FromName] [varchar](255) NULL, 
    [FromMail] [varchar](255) NULL, 
CONSTRAINT [PK_Subject] PRIMARY KEY CLUSTERED 
(
    [Site_id] ASC, 
    [Name] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY], 
CONSTRAINT [IX_Subject_1] UNIQUE NONCLUSTERED 
(
    [Subject_id] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

GO 

SET ANSI_PADDING OFF 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] WITH CHECK ADD CONSTRAINT [FK_Subject_Notification] FOREIGN KEY([Notification_id]) 
REFERENCES [ITrInnoFramework].[Notification] ([Notification_id]) 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] CHECK CONSTRAINT [FK_Subject_Notification] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] WITH CHECK ADD CONSTRAINT [FK_Subject_SiteInfo] FOREIGN KEY([Site_id]) 
REFERENCES [ITrInnoFramework].[SiteInfo] ([Site_id]) 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] CHECK CONSTRAINT [FK_Subject_SiteInfo] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] WITH CHECK ADD CONSTRAINT [FK_Subject_StatusLkp] FOREIGN KEY([StatusCode]) 
REFERENCES [ITrInnoFramework].[StatusLkp] ([StatusCode]) 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] CHECK CONSTRAINT [FK_Subject_StatusLkp] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] WITH CHECK ADD CONSTRAINT [FK_Subject_Template] FOREIGN KEY([Template_id]) 
REFERENCES [ITrInnoFramework].[Template] ([Template_id]) 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] CHECK CONSTRAINT [FK_Subject_Template] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] ADD CONSTRAINT [DF_Subject_InsertDate] DEFAULT (getdate()) FOR [InsertDate] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] ADD CONSTRAINT [DF_Subject_InsertedBy_id] DEFAULT ((-1)) FOR [InsertedBy_id] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] ADD CONSTRAINT [DF_Subject_UpdateDate] DEFAULT (getdate()) FOR [UpdateDate] 
GO 

ALTER TABLE [ITrInnoFramework].[Subject] ADD CONSTRAINT [DF_Subject_UpdatedBy_id] DEFAULT ((-1)) FOR [UpdatedBy_id] 
GO 
+0

你是如何創建表格的?手動或通過休眠? – SAP

+0

表是手動創建的,我開發的這個應用程序實際上是基於現有的表。 – user3022964

+0

你可以在這裏分享你的餐桌結構嗎? – BDR

回答

1

感謝您的幫助,但我最終放棄了並將休眠版本更改爲4.0.1。這解決了我的問題!

0

難道這是你的問題?

this_.Template_id as Template13_11_0_ 

Template_id被截斷爲模板,模板是一個SQL關鍵字。嘗試將Template_id列更改爲templateId。

+0

謝謝你的回答gebuh buy我最終放棄了並改變了Hibernate版本來解決它。我沒有機會測試你的方法,實際上我不認爲我能做到,因爲我不擁有數據庫。不管怎麼說,還是要謝謝你! – user3022964