Spring+Hibernate+MySQL怎么结合?
最近我学习了一下Spring+Hibernate+Junit+Oracle的最简单的人门这篇教程. 不过我用的是MySQL数据库,如何使用MySQL中测试此例子?以下是我测试过程中出现的问题,请求指点:

1. 建立数据表
create table hofman_user (
pk_user_id int not null auto_increment,
vc_email varchar(50),
vc_password varchar(80),
primary key(pk_user_id));
2.修改User.hbm.xml中的关于id的代码
    <id
            name="id"
            column=
"PK_USER_ID"
            type=
"java.lang.Integer"
            unsaved-value=
"-1"
        >
   <generator class=
"increment">
            
          </generator>

        </id>
3、测试代码
test.java
package hofman;

public class test{


  public static void main(String[] args) {
try{ UserDAO userDAO;
        String password;

      ApplicationContextFactory.init(
"/hofman/applicationContext.xml");
      userDAO = (UserDAO)ApplicationContextFactory.
getApplicationContext().getBean(
"userDAO");
  
    
/* User user =
userDAO.findUserByEmail("hofman@zhuoda.org");
      password = user.getPassword();
      System.out.println(password);
     已经测试通过的。
      */

    
      User user2 = new User();
       String email =
"hofman@zhuoda2.org";
       user2.setEmail(email);
       user2.setPassword(
"fakePass");
       userDAO.addUser(user2);
      
//这一段运行出现错误,不能保存数据。
       System.out.println(
"ok insert");
    }catch(Exception e){
     System.err.println(e.getMessage());
    }
}

}
4、运行时的提示(部分)
2006-4-3 19:25:37 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 1064, SQLState: 42000
2006-4-3 19:25:37 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
严重: Syntax error or access violation,  message from server:
"You have an error
in your SQL syntax; check the manual that corresponds to your MySQL server vers
ion for the right syntax to use near '
"HOFMAN_USER"' at line 1"
Hibernate operation: Could not save object; bad SQL grammar []; nested exception
is java.sql.SQLException: Syntax error or access violation,  message from serve
r:
"You have an error in your SQL syntax; check the manual that corresponds to y
our MySQL server version for the right syntax to use near '
"HOFMAN_USER"' at lin
e 1
"
Press any key to continue...
5、这个条件建立hofman_user表主键pk_user_id
使用的序列
   create sequence hofmanid_seq;
   我在建表时没用上,我想跟这个应该有关,不知道
怎么在MySQL数据库中设置。
   除了在applicationContext.xml连接数据库和以
上修改之外,别的地方没动。
不知道错误到底是由那引起的?












      
      R>
lunzi   2006-04-03 23:22:10 评论:10   阅读:3131   引用:0
问题解决了 @2006-05-30 15:22:48  lunzi
原来是SpringFramework的配置文件:. ApplicationContext.xml中的
<prop key=
"hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
没有相对应的改为net.sf.hibernate.dialect.MySQLDialect,以上用的是oracle数据库而我的是MySQL数据库。
另外我想问一下好的可迁移的方案中的pk该如何设计?

谢谢 @2006-04-06 19:26:44  lunzi
好的,我明白了,我试试其他的方式.
何必纠缠autocrement @2006-04-06 15:56:51  hofman
有的问题可以暂时绕开的,autocrement就不是一个好的可迁移的方案,oracle就无此项.
接下边的 @2006-04-06 13:58:18  游客
Base table or view not found,  message from server: "
Unknown table 'hibernate_sequence' in field list"
Press any key to continue...



还是有错 @2006-04-06 13:56:18  lunzi
部分提示:
信息: SQLErrorCodes loaded: [DB2, HSQL, MS-SQL, MySQL, Oracle, Informix, Postgre
SQL, Sybase]
2006-4-4 0:44:55 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 1109, SQLState: 42S02
2006-4-4 0:44:55 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
严重: Base table or view not found,  message from server: "Unknown table 'hibern
ate_sequence' in field list"
2006-4-4 0:44:55 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
警告: SQL Error: 1109, SQLState: 42S02
2006-4-4 0:44:55 net.sf.hibernate.util.JDBCExceptionReporter logExceptions
严重: Base table or view not found,  message from server: "Unknown table 'hibern
ate_sequence' in field list"
Hibernate operation: Could not save object; bad SQL grammar []; nested exception
is java.sql.SQLException: Base table or view not fo
pk @2006-04-03 23:51:10  hofman
PK不对。 <id      name="id"           column="PK_USER_ID"    type="int">    <generator class="native">                      </generator>        </id>试一试吧。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2008 powered by YuLog