【hibernate】请各位帮帮看看这个错误的原因,十分感谢!!注:前几天为了解决一个问题,把分数都散完了

先注明下:前几天为了解决一个问题,把分数都散完了,所以已经没分给各位了,抱歉!


category.hbm.xml:

<hibernate-mapping>
    <class name="com.shaiyaya.dao.Category" table="category" catalog="shaiyaya">
        <id name="cateId" type="java.lang.Integer">
            <column name="cate_id" />
            <generator class="native" />
        </id>
        <property name="cid" type="java.lang.String">
            <column name="cid" length="20">
                <comment>宝淘的category编号</comment>
            </column>
        </property>
        <property name="isParent" type="java.lang.String">
            <column name="is_parent" length="20" />
        </property>
        <property name="cateName" type="java.lang.String">
            <column name="cate_name" length="20" />
        </property>
        <property name="desc" type="java.lang.String">
            <column name="desc" />
        </property>
        <property name="createTime" type="java.util.Date">
            <column name="create_time" length="19">
                <comment>建创时间</comment>
            </column>
        </property>
        <property name="memo" type="java.lang.String">
            <column name="memo" length="50" />
        </property>
        <set name="goodses" inverse="true">
            <key>
                <column name="cate_id" />
            </key>
            <one-to-many class="com.shaiyaya.dao.Goods" />
        </set>
    </class>
</hibernate-mapping>

 hibernate.cfg.xml:(这个应该是正确的,因为其他的表操作能成功,hibernate自动生成部署的,不可能一个行,一个不行把)

   <mapping resource="com/shaiyaya/dao/Category.hbm.xml" />

 测试代码:

public static void main(String[] args){
        Transaction tran = HibernateSessionFactory.getSession().beginTransaction();
        Category g = new Category();
        CategoryDAO cd = new CategoryDAO();
        g.setDesc("nbasad");
        //g.setCid("1231");
        cd.attachDirty(g);
        tran.commit();
        HibernateSessionFactory.getSession().close();
        System.out.println("success");
    }

 

报错:

Hibernate: insert into shaiyaya.category (cid, is_parent, cate_name, desc, create_time, memo) values (?, ?, ?, ?, ?, ?)
[ERROR] 10-12-09:27:33 org.hibernate.util.JDBCExceptionReporter.logExceptions(JDBCExceptionReporter.java:72)  [thread:main]  [messgage: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 'desc, create_time, memo) values (null, null, null, 'nbasad', null, null)' at line 1]
[ERROR] 10-12-09:27:33 com.shaiyaya.dao.CategoryDAO.attachDirty(CategoryDAO.java:148)  [thread:main]  [messgage:attach failed]
org.hibernate.exception.SQLGrammarException: could not insert: [com.shaiyaya.dao.Category]
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1986)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2405)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:37)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
    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.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
    at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
    at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:502)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:494)
    at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:490)
    at com.shaiyaya.dao.CategoryDAO.attachDirty(CategoryDAO.java:145)
    at com.shaiyaya.service.GoodsService.main(GoodsService.java:81)
Caused by: com.mysql.jdbc.exceptions.jdbc4.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 'desc, create_time, memo) values (null, null, null, 'nbasad', null, null)' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.Util.getInstance(Util.java:381)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1761)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2046)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1964)
    at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1949)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105)
    at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:1968)
    ... 15 more
c

 

5个回答

把你的desc字段改个名字,关键字是不行的。比如换成mydesc等等

字段desc,关键字吧?

[code="java"]你数据库字段与hibernate映射模板的不符,好好检查一下啊[/code]

desc是关键字 你把sql放到数据库客户端执行试试

级联原因,desc 可能造成生成顺序不一样,先由父表在有子表,你desc可能造成顺序正好相反。。。。。。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问