映射文件:
<hibernate-mapping package="bean.admin.order">
<class name="Order" table="order">
<id name="oid" column="oid">
<generator class="uuid.hex">
</generator>
</id>
<property name="name" length="20"/>
<property name="pay"/>
<property name="deliver"/>
<property name="adds" type="string" length="100"/>
<property name="postCode" type="string" length="10"/>
<property name="phone" type="string" length="20"/>
<property name="email" length="50"/>
<property name="summed"/>
<property name="state"/>
<property name="regdate"/> </class>
</hibernate-mapping>bean:
public class Order {
private String oid;
private String name;
private int pay;
private int deliver;
private String adds;
private String postCode;
private String phone;
private String email;
private float summed;
private int state;
private Date regdate;
//省略get() set()
}DAO:
public class OrderDao {
public int newOrder(Order order){
SessionFactory sessionfactory=HibernateUtil.getSessionFactory();
Session session=sessionfactory.openSession(); try{
session.beginTransaction();
session.save(order);
session.getTransaction().commit();
return 0;
}catch(Exception e){
e.printStackTrace();
return -9;
}
}
}public class Test {
public static void main(String args[]){
OrderDao orderdao=new OrderDao();
int ret;
Order order=new Order();
order.setName("zsy");
order.setAdminid("gemini");
order.setPhone("123546798");
order.setRegdate(new Date());
ret=orderdao.newOrder(order);
System.out.println(ret);}
}结果每次运行的时候都是
Hibernate: insert into order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate, oid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
13:07:14,051 WARN JDBCExceptionReporter:71 - SQL Error: 1064, SQLState: 42000
13:07:14,061 ERROR JDBCExceptionReporter:72 - 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 'order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate,' at line 1
13:07:14,061 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at dao.OrderDao.newOrder(OrderDao.java:35)
at test.Test.main(Test.java:262)
Caused by: 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 'order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate,' at line 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1099)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:849)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 9 more
-9是怎么回事啊!
请大侠们指点,谢谢了~~~
<hibernate-mapping package="bean.admin.order">
<class name="Order" table="order">
<id name="oid" column="oid">
<generator class="uuid.hex">
</generator>
</id>
<property name="name" length="20"/>
<property name="pay"/>
<property name="deliver"/>
<property name="adds" type="string" length="100"/>
<property name="postCode" type="string" length="10"/>
<property name="phone" type="string" length="20"/>
<property name="email" length="50"/>
<property name="summed"/>
<property name="state"/>
<property name="regdate"/> </class>
</hibernate-mapping>bean:
public class Order {
private String oid;
private String name;
private int pay;
private int deliver;
private String adds;
private String postCode;
private String phone;
private String email;
private float summed;
private int state;
private Date regdate;
//省略get() set()
}DAO:
public class OrderDao {
public int newOrder(Order order){
SessionFactory sessionfactory=HibernateUtil.getSessionFactory();
Session session=sessionfactory.openSession(); try{
session.beginTransaction();
session.save(order);
session.getTransaction().commit();
return 0;
}catch(Exception e){
e.printStackTrace();
return -9;
}
}
}public class Test {
public static void main(String args[]){
OrderDao orderdao=new OrderDao();
int ret;
Order order=new Order();
order.setName("zsy");
order.setAdminid("gemini");
order.setPhone("123546798");
order.setRegdate(new Date());
ret=orderdao.newOrder(order);
System.out.println(ret);}
}结果每次运行的时候都是
Hibernate: insert into order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate, oid) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
13:07:14,051 WARN JDBCExceptionReporter:71 - SQL Error: 1064, SQLState: 42000
13:07:14,061 ERROR JDBCExceptionReporter:72 - 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 'order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate,' at line 1
13:07:14,061 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at dao.OrderDao.newOrder(OrderDao.java:35)
at test.Test.main(Test.java:262)
Caused by: 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 'order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate,' at line 1
at com.mysql.jdbc.PreparedStatement.executeBatchSerially(PreparedStatement.java:1099)
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:849)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 9 more
-9是怎么回事啊!
请大侠们指点,谢谢了~~~
<class name="Order" table="order">
<id name="oid" column="oid">
<generator class="uuid.hex">
</generator>
</id>
<property name="name" length="20"/>
<property name="pay"/>
<property name="deliver"/>
<property name="adds" type="string" length="100"/>
<property name="postCode" type="string" length="10"/>
<property name="phone" type="string" length="20"/>
<property name="email" length="50"/>
<property name="summed"/>
<property name="state"/>
<property name="regdate"/> </class>
</hibernate-mapping>---------------------------------
<property name="name" length="20"/>这些是什么类型的,你设置了吗?
<property name="phone" type="string" length="20"/>这里的string是什么?改成type="java.lang.String"Order order=new Order();
order.setName("zsy");
order.setAdminid("gemini");
order.setPhone("123546798");
order.setRegdate(new Date());
ret=orderdao.newOrder(order);
--------------------
其他的属性的默认值是什么,是"",还是null<generator class="uuid.hex">
------------------------------
这个我没有用过,,一般我用<generator class="native" />希望对你有帮助
public class Order {
private String oid;
private String name;
private int pay;
private int deliver;
private String adds;
private String postCode;
private String phone;
private String email;
private float summed;
private int state;
private Date regdate; private String adminid; //省略get() set()
}
掉了一个 private String adminid;
<property name="name" length="20"/>
和<property name="name" type="string" length="20"/> 一样 在数据库中建立表的时候的结果一样都是 name varchar(20)
但是我还是改成了<property name="phone" type="java.lang.String" length="20"/>结果还是不行
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: 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 'order (name, pay, deliver, adds, postCode, phone, email, summed, state, regdate)' at line 1
OrderDao orderdao=new OrderDao();
int ret;
Order order=new Order();
order.setName("zsy");
order.setAdminid("gemini");
order.setPhone("123546798");
order.setRegdate(new Date());
ret=orderdao.newOrder(order);
System.out.println(ret);
你把所有的字段都赋值一次看看
我把order改了,就没问题了.
看来是因为 order是 关键字.
A 模式下 访问 B模式下 的 t 表,对应 T查询是 select * from A.t但是save(T); 的时候出现这样的错误....
不解了...求助