-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: org.hibernate.exception.SQLGrammarException:
PostPosted: Sat Nov 26, 2005 2:45 pm 
Newbie

Joined: Thu Nov 24, 2005 8:17 am
Posts: 10
Hey All,
I have just started using Hibernate I tried an example i.e saving a record into a table(MS SQL Server) .Now when i try to insert into the table .
It throws me an exception

org.hibernate.exception.SQLGrammarException:

The SQL generated is

insert into Login (Password) values (?) select scope_identity()

But my table has two fields UserName and Password.

Can u please help me ,I have tried the example given by hibernate also.I am stuck

Thanks a million in advance.
Mehdi


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 26, 2005 8:21 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
post the mapping document for Login , a full stack trace, and the java that causes this !!!


Top
 Profile  
 
 Post subject: org.hibernate.exception.SQLGrammarException:
PostPosted: Sun Nov 27, 2005 1:31 am 
Newbie

Joined: Thu Nov 24, 2005 8:17 am
Posts: 10
Hi.
This is my Login.hbm.xml


<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Thu Nov 24 17:18:04 GST 2005 -->
<hibernate-mapping package="org">

<class name="Login" table="Login">
<id name="username" column="UserName" type="string">
<generator class="native"/>
</id>

<property name="password" column="Password" type="string" />
</class>

</hibernate-mapping>



This is My hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">


<hibernate-configuration>

<session-factory name="org.DemoSessionFactory">
<property name="show_sql">true</property>
<property name="myeclipse.connection.profile">SQLSERVER</property>
<property name="connection.url">jdbc:inetdae7:Aedxb-ashkar:1433?database=Telecom_HelpDesk</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">com.inet.tds.TdsDriver</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<mapping resource="org/Login.hbm.xml" />


</session-factory>

</hibernate-configuration>




This is my Code to insert into the table

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
TestClient test=new TestClient();
Configuration cfg = new Configuration() .setProperty(Environment.HBM2DDL_AUTO, "create")
.setProperty(Environment.DIALECT,"org.hibernate.dialect.SQLServerDialect")
.setProperty(Environment.DRIVER,"com.inet.tds.TdsDriver")
.setProperty(Environment.URL,"jdbc:inetdae7:Aedxb-ashkar:1433?database=Telecom_HelpDesk")
.setProperty(Environment.USER,"sa")
.setProperty(Environment.PASS,"sa")
.setProperty("hibernate.show_sql", "true");
test.factory = cfg.buildSessionFactory();
test.createUser();
}
public void createUser() throws Exception
{
Session s = factory.openSession();
Transaction tx = s.beginTransaction();
Login log=new Login();
log.setUsername("UserName");
log.setPassword("Password");
System.out.println("log"+log.getPassword());
System.out.println("log"+log.getUsername());
s.save(log);
s.close();
}


This is the Exception i Get while ruinning the code

og4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
logPassword
logUsername
Hibernate: insert into Login (Password) values (?) select scope_identity()
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [org.Login]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:65)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1985)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2404)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:243)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:269)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:167)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:101)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:524)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:514)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:510)
at org.TestClient.createUser(TestClient.java:46)
at org.TestClient.main(TestClient.java:35)
Caused by: com.inet.tds.SQLException: Msg 208, Level 16, State 1, Line 1, Sqlstate S0002
[AEDXB-ASHKAR]Invalid object name 'Login'.
at com.inet.tds.q.a(Unknown Source)
at com.inet.tds.a.a(Unknown Source)
at com.inet.tds.k.a(Unknown Source)
at com.inet.tds.k.a(Unknown Source)
at com.inet.tds.k.b(Unknown Source)
at com.inet.tds.d.f(Unknown Source)
at com.inet.tds.d.execute(Unknown Source)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1952)
... 16 more


Thanks a lot
Mehdi


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 4:01 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
couple of things ...

the reason username is not ending up in the insert statement, like password, is because it is not mapped in Login.hbm.xml - username needs to be mapped, just like you have w/ password.

use plain jdbc to see if that database really has a table called "Login" - and make sure you check the case.

you will get rid of those WARN statments by putting a file called log4j.properties in the classpath - see hibernate distribution for an example of what might go in this file.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 4:03 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
also, make sure "Login" is not some keyword for SQL server ... i know "User" is.

I also don't see any call to Configuration().configure() ... and there is no Transaction.commit() ...


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 4:28 am 
Newbie

Joined: Thu Nov 24, 2005 8:17 am
Posts: 10
Hi,
I verified ,The table name Login is not a keyword in MS SQL.
And i have mapped the username in the Login.hbm.xml ,It is a PK

<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Thu Nov 24 17:18:04 GST 2005 -->
<hibernate-mapping package="org">

<class name="Login" table="Login">
<id name="username" column="UserName" type="string">
<generator class="native"/>
</id>

<property name="password" column="Password" type="string" />
</class>

</hibernate-mapping>



I changed the JAVA code also ..Now it is

public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
TestClient test=new TestClient();

Configuration cfg = new Configuration()
.addClass(Login.class)
// .setProperty(Environment.SESSION_FACTORY_NAME,"org.DemoSessionFactory")
//.setProperty(Environment.HBM2DDL_AUTO, "create")
//.addResource("org/Login.hbm.xml")
.setProperty(Environment.DIALECT,"org.hibernate.dialect.SQLServerDialect")
.setProperty(Environment.DRIVER,"com.inet.tds.TdsDriver")
.setProperty(Environment.URL,"jdbc:inetdae7:Aedxb-ashkar:1433?database=Telecom_HelpDesk")
.setProperty(Environment.USER,"sa")
.setProperty(Environment.PASS,"sa")
.setProperty("hibernate.show_sql", "true");
//cfg.buildMappings();
//cfg.configure();

cfg.addResource("org/Login.hbm.xml");

test.factory = cfg.configure().buildSessionFactory();

test.createUser();
}
public void createUser() throws Exception
{
Session s = factory.openSession();

Transaction tx = s.beginTransaction();

Login log=new Login();

log.setUsername("Username");

log.setPassword("Password");

System.out.println("log"+log.getPassword());
System.out.println("log"+log.getUsername());

s.save(log);

s.close();

tx.commit();

}



But it throws me a new error


Exception in thread "main" org.hibernate.MappingException: Could not read mappings from resource: org/Login.hbm.xml
at org.hibernate.cfg.Configuration.addResource(Configuration.java:476)
at org.TestClient.main(TestClient.java:39)
Caused by: org.hibernate.MappingException: duplicate class mapping: org.Login
at org.hibernate.cfg.Mappings.addClass(Mappings.java:96)
at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:111)
at org.hibernate.cfg.Configuration.add(Configuration.java:379)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:418)
at org.hibernate.cfg.Configuration.addResource(Configuration.java:473)
... 1 more


If i remove the code cfg.addResource
("org/Login.hbm.xml");



Then the same old error is thrown


Thanks
Mehdi


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 9:44 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
in hibernage.cfg.xml, it is "hibernate.dialect", not "dialect" . See hibernate documentation for the correct way to configure every one of the others as well.

if you don't have a reason to programmatically configure the Configuration, move all of those properties into hibernate.cfg.xml, like you have done w/ connection.username. (which should be hibernate.connection.username)

you are committing the transaction after closing the Session.

UserName is a primary key, but the you have it configured for "native", you want "assigned", which means you assign it before saving it.

no offense, but you really need to take some time and read the documentaion. this thing is not a toy ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 10:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it is quite alright to have only "dialect" instead of "hibernate.dialect" in cfg.xml since we append "hibernate." to every property in hibernate.cfg.xml

Second of all the original issue is that the username is mapped with id generator "native" which I assume is not what he actually wants but "assigned" since it is an ID assigned by the users of the system and not the database if i read the mappings correctly.

But yes, reading the documentation a bit more will definitly help ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 1:50 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
max wrote:
... the username is mapped with id generator "native" which I assume is not what he actually wants but "assigned" ...


alright, so I didn't know you guys appended "hibernate" to the properties. but I did already say this ;)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 27, 2005 2:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
..and im blind

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 12:56 am 
Newbie

Joined: Thu Nov 24, 2005 8:17 am
Posts: 10
Thanks a lot guys....
this helped me a lot and i am ready to work on hibernate......Complete Credit goes to u all...



Thanks again :)
[/list]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.