我对Hibernate不是很有经验,而且我现在很快就遇到了Hibernate的无效列名问题。 我只是尝试使用Criteria API从数据库中选择数据,如下所示:
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调试模式,现在我可以看到它生成的选择:
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 = ?
直接在数据库上运行此查询它工作正常,但在休眠时它显示:
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 :(得分:1)
感谢您的帮助,但我结束了放弃并将hibernate版本更改为4.0.1。这解决了我的问题!
答案 1 :(得分:0)
这可能是你的问题吗?
this_.Template_id as Template13_11_0_
Template_id被截断为Template,Template是SQL关键字。尝试将列Template_id更改为templateId。