终于把hibernate测试成功了!不简单啊!哈哈!

1. 用到了以下几个文件

   ①test.java  , userinfo.java , hibernate.cfg.xml, userinfo.hbm.xml

   ②上面几个文件要都放在src目录下,当然是不是必须放在这个目录中我还不能确定

   ③我把jdbc包和hibernate包既放在了tomcat/common/lib下面了,也放在project/WEB-INF/classes/

      下面,网上的资料说需要放在classes下面,否则会出现一些其他的问题,具体的我没测试过,但是我想

      应该放在classes下面,否则我传到服务器上时包怎么办?但是有个问题是我用jdom时怎么可以?这

     里还不太清楚.

2. 下面是从头到尾出现的错误,以及解决办法:

   ①org.hibernate.HibernateException: Dialect class not found:  net.sf.hibernate.dialect.sqlserverdialect

      这是因为我的断言是 net.sf.hibernate.dialect.SQLServerDialect, 而我的hibernate版本是第3版的,所以要

     改成org.hibernate.dialect.SQLServerDialect.

  ② new Configuration().configure().buildSessionFactory() = Method threw 'java.lang.NoClassDefFoundError'    

      exception.后来为了更好的查错,我把这条语句分开成:

      Configuration cfg=new Configuration().configure("/hibernate.cfg.xml");
      SessionFactory sf=cfg.buildSessionFactory();

      会报以下错误:cfg.buildSessionFactory() = Method threw 'java.lang.NoClassDefFoundError' exception

       产生这个问题的是jta.jar包没有放进去的问题,据说是tomcat的版本问题,在tomcat5.0中没有这个包.

  ③e: org.hibernate.HibernateException =

           {org.hibernate.PropertyNotFoundException@3238}"org.hibernate.PropertyNotFou

           ndException: Could not find a getter for id in class userinfo"

     e: org.hibernate.HibernateException =

           {org.hibernate.PropertyNotFoundException@3307}"org.hibernate.PropertyNotFou

          ndException: Could not find a getter for username in class userinfo" 

    这是因为userinfo.java中的get和set方法的格式问题产生的,所以得弄清楚怎么自动生成这些方法.

   ④  a.  e: org.hibernate.HibernateException =

                 {org.hibernate.exception.JDBCConnectionException@3557}"org.hibernate.except

                 ion.JDBCConnectionException: Cannot open connection"

        b.  sqle: java.sql.SQLException =

             {java.sql.SQLException@3559}"java.sql.SQLException: No suitable driver"

        c. session.beginTransaction() = Method threw

            'org.hibernate.exception.JDBCConnectionException' exception.

        d. e: org.hibernate.HibernateException =

            {org.hibernate.MappingException@2911}"org.hibernate.MappingException:

            invalid configuration"

       e. cause: java.lang.Throwable =

          {org.xml.sax.SAXParseException@2916}"org.xml.sax.SAXParseException: The

         content of element type /"hibernate-configuration/" must match /"(session-factory,security?)/"."

      f. session.beginTransaction() = Method threw org.hibernate.exception.JDBCConnectionException'exception.

      g. e: org.hibernate.HibernateException =

          {org.hibernate.exception.JDBCConnectionException@3567}"org.hibernate.except

          ion.JDBCConnectionException: Cannot open connection"

      h. sqle: java.sql.SQLException =

          {java.sql.SQLException@3569}"java.sql.SQLException: [Microsoft][SQLServer

         2000 Driver for JDBC]Error establishing socket."

      这些都是因为hibernate.cfg.xml中配置出现错误产生的问题,最主要的就是下面两个:

 <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>

<property  

 name="connection.url">jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=testhibernate;SelectMethod=cursor

</property>

  ⑤tx.commit() = Method threw 'org.hibernate.exception.ConstraintViolationException' exception.

  当 IDENTITY_INSERT 设置为 OFF 时,不能向表 'userinfo' 中的标识列插入显式值.

    我将increment改为native就能够解决了,但是具体的generator还得去找资料研究一下.

3. 其他方面一些要注意的问题

  ①<mapping resource="userInfo/User.hbm.xml"/>这个resource指定的目录是相对于classes的 

  ② 在最后还碰到了另一个问题,但是不影响结果

       javax.naming.NamingException:   Context   is   read   only  

      网上资料说是:tomcat不支持jndi动态绑定的原因,不清楚.

     可以参考这个页面(http://topic.csdn.net/t/20050222/09/3796914.html)

4. 还有好多好多问题在后面,又没书看,又没钱买,哎,叫我怎么活........

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
SpringMVC和Hibernate是两个不同的框架,它们分别负责Web应用程序的控制器和数据访问层。在SpringMVC中使用Hibernate进行数据验证,可以通过以下步骤实现: 1. 在SpringMVC中配置Hibernate的SessionFactory和TransactionManager。 2. 创建实体类,并使用Hibernate注解进行数据验证。 3. 在控制器中使用@Valid注解对实体类进行验证。 4. 在控制器中使用BindingResult对象获取验证结果,并根据结果进行相应的处理。 例如,以下是一个使用SpringMVC和Hibernate进行数据验证的示例: 1. 在SpringMVC中配置Hibernate的SessionFactory和TransactionManager: ``` <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="packagesToScan" value="com.example.entity"/> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> ``` 2. 创建实体类,并使用Hibernate注解进行数据验证: ``` @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank(message = "用户名不能为空") private String username; @NotBlank(message = "密码不能为空") private String password; @Email(message = "邮箱格式不正确") private String email; // 省略getter和setter方法 } ``` 3. 在控制器中使用@Valid注解对实体类进行验证: ``` @Controller @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @PostMapping("/register") public String register(@Valid User user, BindingResult result, Model model) { if (result.hasErrors()) { model.addAttribute("errors", result.getAllErrors()); return "register"; } userService.save(user); return "redirect:/login"; } // 省略其他方法 } ``` 4. 在控制器中使用BindingResult对象获取验证结果,并根据结果进行相应的处理: ``` <form action="/user/register" method="post"> <div class="form-group"> <label for="username">用户名</label> <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名"> <span class="text-danger" th:if="${errors != null} and ${errors.hasFieldErrors('username')}" th:text="${errors.getFieldError('username').defaultMessage}"></span> </div> <div class="form-group"> <label for="password">密码</label> <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"> <span class="text-danger" th:if="${errors != null} and ${errors.hasFieldErrors('password')}" th:text="${errors.getFieldError('password').defaultMessage}"></span> </div> <div class="form-group"> <label for="email">邮箱</label> <input type="email" class="form-control" id="email" name="email" placeholder="请输入邮箱"> <span class="text-danger" th:if="${errors != null} and ${errors.hasFieldErrors('email')}" th:text="${errors.getFieldError('email').defaultMessage}"></span> </div> <button type="submit" class="btn btn-primary">注册</button> </form> ``` 以上就是一个简单的使用SpringMVC和Hibernate进行数据验证的示例。通过这种方式,可以方便地对用户提交的数据进行验证,保证数据的正确性和安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zunshanke2004

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值