Ich weiß einfach nicht genau woran es liegt. Bekomme immer folgende Exception:
Es muss damit zu tun haben, wie der name der exception schon sagt "You have an error in your SQL syntax". aber warum? ich zeige mal die hibernate-cfg.xml und mapping-datei.
hibernate-cfg.xml
User.hbm.xml
In der MySQL DB habe ich eine Tabelle mit 3 Spalten USER_ID, NAME und ALTER angelegt.
Wenn ich in Zeile 15 der hibernate-cfg.xml den Wert auf create ändere, dann wird zumindest schonmal die Tabelle user gelöscht. Also klappt der DB-Zugriff schonmal.
Ich will ein Objekt speichern, aber anscheinend wird die falsche MySQL-Syntax benutzt. Verstehe aber nicht warum, da ich ja die Attribute des Objektes durch die mapping-datei den Spalten zuordne und da mache ich ja ansonsten nichts.
Wenn ich Zeile 17 und 18 der hibernat-cfg.xml weglasse, dann steht in zeile 9 der exception:
ich weiß nicht woran es liegt und bin bisher auch im inet nicht besonder weiter gekommen. deswegen hoffe ich jemand kann mir helfen.[/code]
Code:
exception
javax.servlet.ServletException: could not insert: [beans.User]
javax.faces.webapp.FacesServlet.service(FacesServlet.java:154)
root cause
org.hibernate.exception.SQLGrammarException: could not insert: [beans.User]
org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2267)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
beans.UserHandler.processAction(UserHandler.java:45)
javax.faces.event.ActionEvent.processListener(ActionEvent.java:51)
javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:487)
javax.faces.component.UICommand.broadcast(UICommand.java:78)
javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
root cause
java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ALTER, USER_ID) values ('tim', 0, 0)' at line 1
com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1257)
com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:943)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
org.hibernate.jdbc.BatchingBatcher.addToBatch(BatchingBatcher.java:34)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2247)
org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
beans.UserHandler.processAction(UserHandler.java:45)
javax.faces.event.ActionEvent.processListener(ActionEvent.java:51)
javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:487)
javax.faces.component.UICommand.broadcast(UICommand.java:78)
javax.faces.component.UIViewRoot._broadcastForPhase(UIViewRoot.java:97)
javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:171)
org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:32)
org.apache.myfaces.lifecycle.LifecycleImpl.executePhase(LifecycleImpl.java:95)
org.apache.myfaces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:70)
javax.faces.webapp.FacesServlet.service(FacesServlet.java:139)
Es muss damit zu tun haben, wie der name der exception schon sagt "You have an error in your SQL syntax". aber warum? ich zeige mal die hibernate-cfg.xml und mapping-datei.
hibernate-cfg.xml
Code:
<?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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/USERTEST</property>
<property name="connection.username">*****</property>
<property name="connection.password">*****</property>
<property name="connection.autocommit">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show.sql">true</property>
<property name="hibernate.jdbc.batch_versioned_data">true</property>
<property name="hibernate.jdbc.batch_size">1</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<mapping resource="beans/User.hbm.xml" />
</session-factory>
</hibernate-configuration>
User.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping>
<class name="beans.User" table="USER" >
<id name="userId" type="java.lang.Integer" column="USER_ID" >
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String" column="NAME"/>
<property name="alter" type="java.lang.Integer" column="ALTER"/>
</class>
</hibernate-mapping>
In der MySQL DB habe ich eine Tabelle mit 3 Spalten USER_ID, NAME und ALTER angelegt.
Wenn ich in Zeile 15 der hibernate-cfg.xml den Wert auf create ändere, dann wird zumindest schonmal die Tabelle user gelöscht. Also klappt der DB-Zugriff schonmal.
Ich will ein Objekt speichern, aber anscheinend wird die falsche MySQL-Syntax benutzt. Verstehe aber nicht warum, da ich ja die Attribute des Objektes durch die mapping-datei den Spalten zuordne und da mache ich ja ansonsten nichts.
Wenn ich Zeile 17 und 18 der hibernat-cfg.xml weglasse, dann steht in zeile 9 der exception:
Code:
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
ich weiß nicht woran es liegt und bin bisher auch im inet nicht besonder weiter gekommen. deswegen hoffe ich jemand kann mir helfen.[/code]