[英]Getting Exception : org.hibernate.exception.sqlgrammarexception
Hi I am implementing hibernate
, When I am trying to insert a new record getting the Exception org.hibernate.exception.sqlgrammarexception
嗨,我正在实现
hibernate
,当我尝试插入一条新记录时,获取异常org.hibernate.exception.sqlgrammarexception
My hibernate.cfg.xml
file : 我的
hibernate.cfg.xml
文件:
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_db</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">system</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping class="com.hib.UserDetails"/>
</session-factory>
</hibernate-configuration>
If I changed the 如果我改变了
<property name="hbm2ddl.auto">update</property>
<mapping class="com.hib.UserDetails"/>
It worked, But I have to create table manually. 它起作用了,但是我必须手动创建表。 What might be the issue?
可能是什么问题?
Look for the piece of code where you are throwing your SQL/HQL query. 寻找您要引发SQL / HQL查询的代码段。 There you may find some simple yet hard to find mistakes like wrongly spelt commands,etc.
在这里,您可能会发现一些简单而又难以发现的错误,例如拼写错误的命令等。 That's why sql 'grammar' exception
这就是为什么SQL“语法”异常
Check mapping class "UserDeatails". 检查映射类“ UserDeatails”。
There is mismatch between manually created table and UserDetails mapping class. 手动创建的表和UserDetails映射类之间不匹配。
Possibly mismatch Column_Names, Id, Constraints ..etc Column_Names,Id,Constraints等可能不匹配
check for column names in table and match with column names in @column annotation in bean. 检查表中的列名,并与bean中@column批注中的列名匹配。 Most probably its column mismatch.
最可能是其列不匹配。
Could you post the Stacktrace of the Exception, also it would be of help if you could post the code which is adding the User to Database. 您能否发布异常的Stacktrace,如果您可以发布将用户添加到数据库的代码,那也会有所帮助。 It can either be a problem with the Column names of Database table and the declared variables.
数据库表的列名和声明的变量可能是一个问题。
<property name="show_sql">true</property>
<property name="format_sql">true</property>
These must be generating the SQL Statement, check if there is an error in the Generated SQL Statement. 这些必须正在生成SQL语句,请检查“生成的SQL语句”中是否有错误。 Posting the SQL could also help.
发布SQL也可能有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.