首页 > 解决方案 > 我在 bean factory 中遇到的这个错误的解决方案是什么?

问题描述

** 2021 年 6 月 29 日晚上 9:45:18 org.hibernate.Version logVersion INFO:HHH000412:Hibernate Core {5.2.8.Final} 2021 年 6 月 29 日晚上 9:45:19 org.hibernate.cfg.Environment 信息: HHH000206:找不到 hibernate.properties 2021 年 6 月 29 日晚上 9:45:19 org.hibernate.annotations.common.reflection.java.JavaReflectionManager 信息:HCANN000001:Hibernate Commons Annotations {5.0.1.Final} 2021 年 6 月 29 日星期二 21:45 :19 PKT 2021 WARN: 不建议在没有服务器身份验证的情况下建立 SSL 连接。根据 MySQL 5.5.45+、5.6.26+ 和 5.7.6+ 的要求,如果未设置显式选项,则默认情况下必须建立 SSL 连接。为了符合不使用 SSL 的现有应用程序,verifyServerCertificate 属性设置为“false”。您需要通过设置 useSSL=false 来显式禁用 SSL,或设置 useSSL=true 并为服务器证书验证提供信任库。2021 年 6 月 29 日晚上 9:45:19 org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator InitialService WARN:HHH000342:无法获得与查询元数据的连接:通信链路故障

从服务器成功接收到的最后一个数据包是 286 毫秒前。最后一个成功发送到服务器的数据包是 272 毫秒前。2021 年 6 月 29 日晚上 9:45:19 org.hibernate.dialect.Dialect 信息:HHH000400:使用方言:org.hibernate.dialect.MySQLDialect 2021 年 6 月 29 日晚上 9:45:19 org.hibernate.engine.jdbc.env .internal.LobCreatorBuilderImpl makeLobCreatorBuilder 信息:HHH000422:禁用上下文 LOB 创建,因为连接为空 2021 年 6 月 29 日晚上 9:45:20 org.springframework.context.support.AbstractApplicationContext 刷新警告:上下文初始化期间遇到异常 - 取消刷新尝试:org .springframework.beans.factory.BeanCreationException:在类路径资源[config.xml]中定义名称为“factory”的bean创建错误:调用init方法失败;嵌套异常是 org. hibernate.MappingException:无法在线程“main”中获取 org.hibernate.persister.entity.SingleTableEntityPersister 异常的构造函数 org.springframework.beans.factory.BeanCreationException:在类路径资源 [config. xml]: init 方法调用失败;嵌套异常是 org.hibernate.MappingException: 无法在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794) 在 org.springframework.beans 获取 org.hibernate.persister.entity.SingleTableEntityPersister 的构造函数.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594) 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory。

这是我的 config.xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven />
<bean
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    name="ds">
    <property name="driverClassName"
        value="com.mysql.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://localhost:3306/springjdbc" />
    <property name="username" value="root" />
    <property name="password" value="root" />
</bean>

<bean
    class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"
    name="factory">
    <property name="dataSource" ref="ds" />
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.springDb.entities.Student</value>
        </list>
    </property>
</bean>  
<bean class="org.springframework.orm.hibernate5.HibernateTemplate"
    name="hibernateTemplat">
    <property name="sessionFactory" ref="factory"></property>

</bean>
    <bean class="com.springDb.Dao.StudentDao" name="studentDao">
        <property name="hibernateTemplate" ref="hibernateTemplat"></property>
    </bean>

    <bean
        class="org.springframework.orm.hibernate5.HibernateTransactionManager"
        name="transactionManager">
        <property name="sessionFactory" ref="factory"></property>
    </bean>


</beans>

学生道班

package com.springDb.Dao;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.transaction.annotation.Transactional;
import com.springDb.entities.Student;

public class StudentDao {

private HibernateTemplate hibernateTemplate;

@Transactional
public int insert(Student student) {
    
    Integer r=(Integer)hibernateTemplate.save(student);
    return r;
}

public HibernateTemplate getHibernateTemplate() {
    return hibernateTemplate;
}

public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    this.hibernateTemplate = hibernateTemplate;
}

public StudentDao(HibernateTemplate hibernateTemplate) {
    super();
    this.hibernateTemplate = hibernateTemplate;
}
public StudentDao() {


}}

学生班

package com.springDb.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "Student_Info")
public class Student {
@Id
@Column(name = "Student_Id")
private int studentId;
@Column(name = "Student_Name")
private String studentName;
@Column(name = "Student_City")
private String studentCity;

public Student(int studentId, String studentName, String studentCity) {
    super();
    this.studentId = studentId;
    this.studentName = studentName;
    this.studentCity = studentCity;
}



public Student() {

}

public int getStudentId() {
    return studentId;
}

public void setStudentId(int studentId) {
    this.studentId = studentId;
}

public String getStudentName() {
    return studentName;
}

public void setStudentName(String studentName) {
    this.studentName = studentName;
}

public String getStudentCity() {
    return studentCity;
}

public void setStudentCity(String studentCity) {
    this.studentCity = studentCity;
}

}
    

App.java 类

package com.springDb;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.springDb.Dao.StudentDao;
import com.springDb.entities.Student;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
       ApplicationContext context= new ClassPathXmlApplicationContext("config.xml");
       StudentDao studentDao = context.getBean("studentDao",StudentDao.class);
       
       Student student=new Student(1233,"Hery","Turky");
      
       int r = studentDao.insert(student);
       System.out.println("Done"+r);
    }
}
    
    

标签: javaspringhibernate

解决方案


无论您使用什么版本的 Spring/Hibernate,它都与您使用的 Java 版本不兼容。尝试更新 Spring/Hibernate 或使用较旧的 Java 版本。


推荐阅读