Frage

I'm not very experienced on Hibernate and I'm facing a Invalid column name problem with Hibernate for a quite while now. I'm just trying to select data from a database using Criteria API, as follow:

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;
    }
}

I enabled the Hibernate debug mode and now I can see the select that it generates:

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 = ?

Running this query directly on the DB it works fine, but on hibernate it is showing a:

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

Here is my mapping 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>

I'm running on Java 1.5.022, Hibernate 3.26GA and JBOSS 6.0 EAP (Very similar to JBOSS AS7). Could anyone please help? If you need any further details please let me know and I'll happy to provide.

Have a nice day!

EDIT: Table structure:

 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
War es hilfreich?

Lösung

Thank you for your help guys but I ended giving up and changing the hibernate version to 4.0.1. This solved my problem!

Andere Tipps

Could this be your problem?

 this_.Template_id as Template13_11_0_

Template_id is being truncated to Template, Template is a SQL keyword. Try changing the column Template_id to templateId.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top