国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

java - spring mvc integrates hibernate5 access error Could not locate cfg.xml resource
為情所困
為情所困 2017-05-17 10:03:38
0
2
2217

Spring MVC integrates the Hibernate5 framework—the database connection and other information has been configured in the dispatcher-servlet.xml file. I wrote a simple function to create a new data table (just a few files). No error is reported when running, but HTTP Status 500 error is reported when accessing. org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml], but it is strange that the data table was successfully created but no data was written.
What I am very confused about is that after hibernate4, sessionFactoryBean is not used to replace the hibernate.cfg.xml file. I did not create the hibernate.cfg.xml file. Some specific files and error screenshots are as follows

dispatcher-servlet.xml file

<?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: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">

    <!-- 掃描使用注解的類所在包 -->
    <context:component-scan base-package="com.hiber.*"/>

    <!-- 配置數(shù)據(jù)源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/hiber?useUnicode=yes&amp;characterEncoding=UTF-8" />
        <property name="username" value="root" />
        <property name="password" value="3443"/>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入數(shù)據(jù)源 -->
        <property name="dataSource" ref="dataSource"/>
        <!-- 找到實(shí)體包(pojo) -->
        <property name="packagesToScan" value="com.hiber.*" />
        <property name="hibernateProperties">
            <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>
            <prop key="hibernate.hbm2ddl.auto">create</prop>
            <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 找到實(shí)體包(pojo) -->
        <property name="packagesToScan" value="com.hiber.*" />
        <!--指定jpa適配器-->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <!--<!&ndash;指定jpa屬性&ndash;>-->
        <!--<property name="jpaProperties">-->
            <!--<props>-->
                <!--<prop key="hibernate.dialect">org.hibernate.dialect.MySQL57Dialect</prop>-->
                <!--<prop key="hibernate.hbm2ddl.auto">create</prop>-->
                <!--<prop key="hibernate.show_sql">true</prop>-->
            <!--</props>-->
        <!--</property>-->
    </bean>

    <!-- 配置hibernate事務(wù)管理器 -->
    <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:annotation-driven />
</beans>

Message.java file

package com.hiber.entity;

import javax.persistence.*;

@Entity
public class Message{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    int id;
    @Column(nullable = false)
    String text;

    public Message(String text) {
        setText(text);
    }
    public Message(){}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

IndexController.java file

package com.hiber.controllers;

import com.hiber.entity.Message;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class IndexController {
    @RequestMapping(value = "/persist")
    public String saveMessage(){
        Message message = new Message("Hello, world");
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
                .configure()
                .build();
        SessionFactory sessionFactory =  new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.persist(message);
        tx.commit();
        return "數(shù)據(jù)添加成功!";
    }
}

Browser error screenshot

Screenshot of successful data table creation

Project structure

Please help to find out what went wrong, Thanks in advance!

為情所困
為情所困

reply all(2)
黃舟

Hibernate version 5.2 or above is written like this:

Message message = new Message("Hello,world!");
        Configuration configuration = new Configuration();
        StandardServiceRegistry registry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        SessionFactory sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        session.persist(message);
        tx.commit();
        return "數(shù)據(jù)添加成功!";

org.hibernate.internal.util.config.ConfigurationException: Could not locate cfg.xml resource [hibernate.cfg.xml] The problem was solved, but org.hibernate.service.spi.ServiceException: Unable to create requested appeared again service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment], also help!

PHPzhong

View the web.xml configuration, as follows:

<!-- 加載Spring -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- spring默認(rèn)的配置文件名稱是:applicationContext.xml,如果是默認(rèn)則不需要配置 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml,
        /WEB-INF/daoContext.xml</param-value>
</context-param>

The data source, sessionFactory, transaction manager, and transactions are configured in daoContext.xml;
Do you add these? See if there are any errors

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template