当我只将一条记录插入或保存到mysql时,它工作正常。但是当我用 create
财产(非 update
),它抛出一个 SqlGrammerException
例外。
代码如下:
package com.aartek.DTO;
public class UserDTO
{
private int id;
private String firstName,aka,houseName;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getAka() {
return aka;
}
public void setAka(String aka) {
this.aka = aka;
}
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
}
测试添加类:
package com.aartek.DTO;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
public class TestAdd
{
public static void main(String []args) throws Exception
{
UserDTO dto=new UserDTO();
UserDTO dto2=new UserDTO();
UserDTO dto3=new UserDTO();
dto.setFirstName("Eddard_Stark");
dto.setAka("NED/Lord_of_winterfall");
dto.setHouseName("Stark");
dto2.setFirstName("Tyrion_lannister");
dto2.setAka("Imp/Hand_of_the_queen");
dto2.setHouseName("Lannister");
dto3.setFirstName("Dannereous_Targareyen");
dto3.setAka("Mother_of_Dragons");
dto3.setHouseName("Targareyen");
Configuration cfg=new Configuration();
cfg.configure("hibernet.cfg.xml");
SessionFactory sessionFactory=cfg.buildSessionFactory();
Session s=sessionFactory.openSession();
Transaction t=s.beginTransaction();
s.save(dto);
s.save(dto2);
s.save(dto3);
t.commit();
s.close();
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"C:\Users\s\.tooling\Desktop\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/suyash</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="userDTO.hbm.xml" />
</session-factory>
</hibernate-configuration>
休眠Map文件
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"C:\Users\s\.tooling\Desktop\hibernate-mapping-3.0.dtd">
<hibernate-mapping package = "com.aartek.DTO">
<class name="UserDTO" table="WESTEROUS_ISLE">
<id name="id" column="CHARACTER">
<generator class="increment"></generator>
</id>
<property name="firstName" column="FIRST_of_his_NAME"></property>
<property name="aka" column="A.K.A"></property>
<property name="houseName" column="HOUSE_NAME"></property>
</class>
</hibernate-mapping>
暂无答案!
目前还没有任何答案,快来回答吧!