【问题标题】:Spring hibernate Issue春季休眠问题
【发布时间】:2016-12-14 00:02:24
【问题描述】:

休眠spring和maven 我在个人项目中使用hibernate 5和spring 4。我无法找到问题的根本原因。

谁能帮我找出我的代码的问题?

<?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-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="fr.geom.core.controller"/>
    <context:component-scan base-package="fr.geom.core.service"/>
    <context:component-scan base-package="fr.geom.core.repository"/>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="mappingResources">
            <list>
                <value>LineEntity.hbm.xml</value>
                <value>PointEntity.hbm.xml</value>
                <value>PolygoneEntity.hbm.xml</value>
                <value>Coordinate.hbm.xml</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                <prop key="current_session_context_class">thread</prop>
                <prop key="show.provider_class">org.hibernate.cache.internal.NoCacheProvider</prop>
                <prop key="show_sql">true</prop>
                <prop key="format_sql">true</prop>
                <prop key="hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
    <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="org.postgresql.Driver" />
        <property name="url" value="jdbc:postgresql://localhost:5432/bdgeo" />
        <property name="username" value="postgres" />
        <property name="password" value="postgres" />
    </bean>
</beans>
        package fr.geom.core.util;
        import org.hibernate.SessionFactory;
        import org.hibernate.cfg.Configuration;
        public class HibernateUtil {
            private static final SessionFactory sessionFactory = buildSessionFactory();

            private static SessionFactory buildSessionFactory() {
                try {
                    return new Configuration().configure().buildSessionFactory();
                }
                catch (Throwable ex) {
                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);
                }
            }
            public static SessionFactory getSessionFactory() {
                return sessionFactory;
            }
        }

        package fr.geom.core.util;

        import org.hibernate.SessionFactory;
        import org.hibernate.cfg.Configuration;

        public class HibernateUtil {

            private static final SessionFactory sessionFactory = buildSessionFactory();

            private static SessionFactory buildSessionFactory() {
                try {
                    return new Configuration().configure().buildSessionFactory();
                }
                catch (Throwable ex) {
                    System.err.println("Initial SessionFactory creation failed." + ex);
                    throw new ExceptionInInitializerError(ex);
                }
            }

            public static SessionFactory getSessionFactory() {
                return sessionFactory;
            }

        }
        package fr.geom.core.service;

        import javax.annotation.Resource;
        import org.springframework.stereotype.Service;
        import fr.geom.core.entity.LineEntity;
        import fr.geom.core.geometrie.Geometrie;
        import fr.geom.core.repository.GeometryDaoInterface;

        @Service
        public class LineService implements GeometryServiceInterface {


            @Resource
            private GeometryDaoInterface lineDAO;

            public LineEntity loadService(int id) {
                return (LineEntity) lineDAO.load(id);
            }

            public void saveService(Geometrie line) {
                lineDAO.save(line);
            }
        }

        package fr.geom.core.service;

        import javax.annotation.Resource;
        import org.springframework.stereotype.Service;
        import fr.geom.core.entity.PointEntity;
        import fr.geom.core.geometrie.Geometrie;
        import fr.geom.core.repository.GeometryDaoInterface;


        @Service
        public class PointService implements GeometryServiceInterface {

            @Resource
            private GeometryDaoInterface pointDAO;

            public PointEntity loadService(int id) {
                return (PointEntity) pointDAO.load(id);
            }

            public void saveService(Geometrie point) {
                pointDAO.save(point);
            }

        }

        package fr.geom.core.service;

        import javax.annotation.Resource;
        import org.springframework.stereotype.Service;
        import fr.geom.core.entity.PolygoneEntity;
        import fr.geom.core.geometrie.Geometrie;
        import fr.geom.core.repository.GeometryDaoInterface;

        @Service
        public class PolygoneService implements GeometryServiceInterface {

            @Resource
            private GeometryDaoInterface polygoneDAO;

            public PolygoneEntity loadService(int id) {
                return (PolygoneEntity) polygoneDAO.load(id);
            }

            public void saveService(Geometrie polygone) {
                polygoneDAO.save(polygone);
            }

        }


        package fr.geom.core.repository;

        import fr.geom.core.geometrie.Geometrie;

        public interface GeometryDaoInterface {

            public void save(Geometrie geometry);
            public Geometrie load(int id);
        }
        package fr.geom.core.repository;

        import javax.annotation.Resource;
        import org.hibernate.Session;
        import org.hibernate.SessionFactory;
        import org.springframework.stereotype.Repository;
        import fr.geom.core.entity.LineEntity;
        import fr.geom.core.geometrie.Geometrie;
        import fr.geom.core.util.HibernateUtil;

        @Repository
        public class LineDAO implements GeometryDaoInterface {

            @Resource
            private SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory().getCurrentSession();

            public void save(Geometrie line) {

                try {
                    Session session = sessionFactory.openSession();
                    session.beginTransaction();
                    session.save(line);
                    session.getTransaction().commit();
                    session.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
            public LineEntity load(int id) {
                LineEntity line = null;
                try {
                    Session session = sessionFactory.openSession();
                    session.beginTransaction();
                    line = (LineEntity) session.load(LineEntity.class, id);
                    session.getTransaction().commit();
                    session.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }   

                return line;
            }
        }

        package fr.geom.core.repository;

        import javax.annotation.Resource;
        import org.hibernate.Session;
        import org.hibernate.SessionFactory;
        import org.springframework.stereotype.Repository;
        import fr.geom.core.entity.PointEntity;
        import fr.geom.core.geometrie.Geometrie;
        import fr.geom.core.util.HibernateUtil;

        @Repository
        public class PointDAO implements GeometryDaoInterface {

            @Resource
            private SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory().getCurrentSession();

            public void save(Geometrie point) {

                try {
                    Session session = sessionFactory.openSession();
                    session.beginTransaction();
                    session.save(point);
                    session.getTransaction().commit();
                    session.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }

            }
            public PointEntity load(int id) {
                PointEntity point = null;

                try {
                    Session session = sessionFactory.openSession();
                    session.beginTransaction();
                    point = (PointEntity) session.load(PointEntity.class, id);
                    session.getTransaction().commit();
                    session.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }   

                return point;
            }
        }
        package fr.geom.core.repository;

        import javax.annotation.Resource;
        import org.hibernate.Session;
        import org.hibernate.SessionFactory;
        import org.springframework.stereotype.Repository;
        import fr.geom.core.entity.PolygoneEntity;
        import fr.geom.core.geometrie.Geometrie;
        import fr.geom.core.util.HibernateUtil;

        @Repository
        public class PolygoneDAO implements GeometryDaoInterface {

            @Resource
            private SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory().getCurrentSession();

            public void save(Geometrie polygone) {

                try {
                    Session session = sessionFactory.openSession();
                    session.beginTransaction();
                    session.save(polygone);
                    session.getTransaction().commit();
                    session.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }

            }

            public PolygoneEntity load(int id) {
                PolygoneEntity polygone = null ;
                try {
                    Session session = sessionFactory.openSession();
                    session.beginTransaction();
                    polygone = (PolygoneEntity) session.load(PolygoneEntity.class, id);
                    session.getTransaction().commit();
                    session.close();

                } catch (Exception e) {
                    // TODO: handle exception
                }
                return polygone;
            }package fr.geom.core.entity;

        public class Coordinate {

            private int idCoordinate;
            private double x;
            private double y;

            public Coordinate() {

            }

            public int getIdCoordinate() {
                return idCoordinate;
            }

            public void setIdCoordinate(int idCoordinate) {
                this.idCoordinate = idCoordinate;
            }

            public double getX() {
                return x;
            }

            public void setX(double x) {
                this.x = x;
            }

            public double getY() {
                return y;
            }

            public void setY(double y) {
                this.y = y;
            }
        }

        package fr.geom.core.entity;

        import java.util.Set;

        import fr.geom.core.geometrie.Geometrie;

        public class LineEntity implements Geometrie{

            private int idLine;
            private Set<PointEntity> line;

            public LineEntity(){
            }
            public int getIdLine() {
                return idLine;
            }
            public void setIdLine(int idLine) {
                this.idLine = idLine;
            }
            public Set<PointEntity> getLine() {
                return line;
            }

            public void setLine(Set<PointEntity> line) {
                this.line = line;
            }
            public String GeoToWkt() {
                // TODO Auto-generated method stub
                return null;
            }
            public String GeomToGeoJSON() {
                // TODO Auto-generated method stub
                return null;
            }
            public String GeomToGml() {
                // TODO Auto-generated method stub
                return null;
            }
        }

        package fr.geom.core.entity;

        import fr.geom.core.entity.Coordinate;
        import fr.geom.core.geometrie.Geometrie;

        public class PointEntity implements Geometrie{

            private int idPoint;
            private Coordinate coordinate;

            public PointEntity(){
            }
            public int getIdPoint() {
                return idPoint;
            }
            public void setIdPoint(int idPoint) {
                this.idPoint = idPoint;
            }
            public Coordinate getCoordinate() {
                return coordinate;
            }
            public void setCoordinate(Coordinate coordinate) {
                this.coordinate = coordinate;
            }

            public String GeoToWkt() {
                // TODO Auto-generated method stub
                return null;
            }

            public String GeomToGeoJSON() {
                // TODO Auto-generated method stub
                return null;
            }

            public String GeomToGml() {
                // TODO Auto-generated method stub
                return null;
            }
        }

        package fr.geom.core.entity;

        import java.util.Set;
        import fr.geom.core.geometrie.Geometrie;

        public class PolygoneEntity implements Geometrie{

            private int idPolygone;
            private Set<PointEntity> polygone;

            public PolygoneEntity(){
            }
            public int getIdPolygone() {
                return idPolygone;
            }
            public void setIdPolygone(int idPolygone) {
                this.idPolygone = idPolygone;
            }
            public Set<PointEntity> getPolygone() {
                return polygone;
            }
            public void setPolygone(Set<PointEntity> polygone) {
                this.polygone = polygone;
            }
            public String GeoToWkt() {
                // TODO Auto-generated method stub
                return null;
            }
            public String GeomToGeoJSON() {
                // TODO Auto-generated method stub
                return null;
            }
            public String GeomToGml() {
                // TODO Auto-generated method stub
                return null;
            }
        }

        package fr.geom.core.controller;

        public interface GeometryControllerInterface {

            public void registerGeometrie();
            public void showGeometrie();
        }

        package fr.geom.core.controller;

        import java.util.Scanner;
        import javax.annotation.Resource;
        import org.springframework.stereotype.Controller;
        import fr.geom.core.entity.LineEntity;
        import fr.geom.core.service.GeometryServiceInterface;

        @Controller
        public class LineController implements GeometryControllerInterface {


            @Resource
            private GeometryServiceInterface lineService;
            private static final Scanner key = new Scanner(System.in);

            public void registerGeometrie() {
                LineEntity line = new LineEntity();
                System.out.println("___________________________");
                System.out.println("Quel est l'identifiant de la line ?");
                int id = key.nextInt();
                line.setIdLine(id);
                lineService.saveService(line);
                System.out.println("___________________________");
                System.out.println(" sauvegarde reussi .........");
            }

            public void showGeometrie() {
                System.out.println("___________________________");
                System.out.println("Quel est l'identifiant de la line ? ");
                int id = key.nextInt();
                lineService.loadService(id);
                System.out.println("___________________________");
                System.out.println(" chargement reussi ..^.. ");
            }

        }

        package fr.geom.core.controller;

        import java.util.Scanner;
        import javax.annotation.Resource;
        import org.springframework.stereotype.Controller;
        import fr.geom.core.entity.PointEntity;
        import fr.geom.core.entity.Coordinate;
        import fr.geom.core.service.GeometryServiceInterface;

        @Controller
        public class PointController implements GeometryControllerInterface{

            @Resource
            private GeometryServiceInterface pointService;
            private static final Scanner key = new Scanner(System.in);

            public void registerGeometrie() {
                PointEntity point = new PointEntity();
                Coordinate coordinate = new Coordinate();
                System.out.println("___________________________");
                System.out.println("Quel est l'identifiant de la line ?");
                int id = key.nextInt();
                point.setIdPoint(id);;
                pointService.saveService(point);
                System.out.println("Quelle est la valeur de X ? ");
                double x = key.nextDouble();
                coordinate.setX(x);
                System.out.println("Quelle est la valeur de Y ? ");
                double y = key.nextDouble();
                coordinate.setY(y);
                point.setCoordinate(coordinate);
                System.out.println("___________________________");
                System.out.println(" sauvegarde reussi .........");
            }

            public void showGeometrie() {
                System.out.println("___________________________");
                System.out.println("Quel est l'identifiant du Point ? ");
                int id = key.nextInt();
                pointService.loadService(id);
                System.out.println("___________________________");
                System.out.println(" chargement reussi ..^.. ");
            }
        }

        package fr.geom.core.controller;

        import java.util.Scanner;
        import javax.annotation.Resource;
        import org.springframework.stereotype.Controller;
        import fr.geom.core.entity.PolygoneEntity;
        import fr.geom.core.service.GeometryServiceInterface;

        @Controller
        public class PolygoneController implements GeometryControllerInterface {

            @Resource
            private GeometryServiceInterface polygoneService;
            private static final Scanner key = new Scanner(System.in);

            public void registerGeometrie() {
                PolygoneEntity polygone = new PolygoneEntity();
                System.out.println("___________________________");
                System.out.println("Quel est l'identifiant du Polygone ?");
                int id = key.nextInt();
                polygone.setIdPolygone(id);
                polygoneService.saveService(polygone);
                System.out.println("___________________________");
                System.out.println(" sauvegarde reussi .........");
            }

            public void showGeometrie() {
                System.out.println("___________________________");
                System.out.println("Quel est l'identifiant du Polygone ? ");
                int id = key.nextInt();
                polygoneService.loadService(id);
                System.out.println("___________________________");
                System.out.println(" chargement reussi ..^.. ");
            }
        }

        package fr.geom.core;

        import org.springframework.context.ApplicationContext;
        import org.springframework.context.support.ClassPathXmlApplicationContext;
        import fr.geom.core.controller.LineController;
        import fr.geom.core.controller.PointController;
        import fr.geom.core.controller.PolygoneController;

        /**
         * App
         *
         */
        public class App 
        {
            public static  void main( String[] args )
            {   
                @SuppressWarnings("resource")
                ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");

                PointController pointController = (PointController) context.getBean("pointController");
                pointController.registerGeometrie();
                pointController.showGeometrie();

                LineController lineController = (LineController) context.getBean("lineController");
                lineController.registerGeometrie();
                lineController.showGeometrie();

                PolygoneController polygoneController = (PolygoneController) context.getBean("polygoneController");
                polygoneController.registerGeometrie();
                pointController.showGeometrie();
            }
        }

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC 
         "-//Hibernate/Hibernate Mapping DTD//EN"
         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

        <hibernate-mapping>
            <class name="fr.geom.core.entity.Coordinate" table="COORDINATES">
                <id name="idCoordinate" column="ID_Coordinate">
                    <generator class="identity">
                    </generator>
                </id>
                <property name="x" column="X"></property>
                <property name="y" column="Y"></property>
            </class>
        </hibernate-mapping>

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC 
         "-//Hibernate/Hibernate Mapping DTD//EN"
         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

        <hibernate-mapping>
            <class name="fr.geom.core.entity.LineEntity" table="LINES">
                <id name="idLine" column="ID_LINE">
                    <generator class="identity">
                    </generator>
                </id>
                <set name="line" table="LINE_POINT">
                    <key column="idLine"/>
                    <many-to-many class="fr.geom.core.entity.PointEntity" column="idPoint"/>
                </set>
            </class>
        </hibernate-mapping>

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC 
         "-//Hibernate/Hibernate Mapping DTD//EN"
         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

        <hibernate-mapping>
            <class name="fr.geom.core.entity.PointEntity" table="POINTS">
                <id name="idPoint" column="ID_POINT">
                    <generator class="identity">
                    </generator>
                </id>
                <one-to-one name="coordinate" class="fr.geom.core.entity.Coordinate"
                    cascade="save-update"></one-to-one>
            </class>
        </hibernate-mapping>

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC 
         "-//Hibernate/Hibernate Mapping DTD//EN"
         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

        <hibernate-mapping>
            <class name="fr.geom.core.entity.PolygoneEntity" table="POLYGONES">
                <id name="idPolygone" column="ID_POLYGONE">
                    <generator class="identity">
                    </generator>
                </id>
                <set name="polygone" table="POLY_POINT">
                    <key column="idPolygone"/>
                    <many-to-many class="fr.geom.core.entity.PointEntity" column="idPoint"/>
                </set>
            </class>
        </hibernate-mapping>

谁能帮帮我?

【问题讨论】:

  • 你应该把你的错误放在你的问题中,而不是回答中。
  • 我无法将所有这些文字都放在问题中
  • 如果你不以一种清晰和结构化的方式重新提出你的问题,没有人会回答。相信我

标签: java spring postgresql hibernate maven


【解决方案1】:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <groupId>GeomAppjhjk</groupId>
            <artifactId>appl</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <packaging>jar</packaging>
            <name>appl</name>
            <url>http://maven.apache.org</url>
            <properties>
                <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            </properties>
            <dependencies>
                <dependency>
                    <groupId>org.postgresql</groupId>
                    <artifactId>postgresql</artifactId>
                    <version>9.4-1200-jdbc41</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-expression</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-core</artifactId>
                        <version>5.0.2.Final</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-api</artifactId>
                        <version>2.6.2</version>
                    </dependency>
                    <dependency>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-core</artifactId>
                        <version>2.6.2</version>
                    </dependency>
                    <dependency>
                        <groupId>javassist</groupId>
                        <artifactId>javassist</artifactId>
                        <version>3.12.1.GA</version>
                    </dependency>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-log4j12</artifactId>
                        <version>1.7.12</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-jdbc</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-tx</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aop</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-orm</artifactId>
                        <version>4.3.2.RELEASE</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-pool</groupId>
                        <artifactId>commons-pool</artifactId>
                        <version>1.6</version>
                    </dependency>
                    <dependency>
                        <groupId>commons-dbcp</groupId>
                        <artifactId>commons-dbcp</artifactId>
                        <version>1.4</version>
                    </dependency>
                    <dependency>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>3.8.1</version>
                        <scope>test</scope>
                    </dependency>
                    <dependency>
                        <groupId>org.hibernate</groupId>
                        <artifactId>hibernate-entitymanager</artifactId>
                        <version>5.0.2.Final</version>
                    </dependency>
                </dependencies>
            </project>

这是控制台中的错误:

2016-08-08 10:34:39 INFO  ClassPathXmlApplicationContext:581 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@291972f8: startup date [Mon Aug 08 10:34:39 CEST 2016]; root of context hierarchy
2016-08-08 10:34:39 INFO  XmlBeanDefinitionReader:317 - Loading XML bean definitions from class path resource [ApplicationContext.xml]
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:484 - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:745 - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b6e60b7: defining beans [lineController,pointController,polygoneController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,lineService,pointService,polygoneService,lineDAO,pointDAO,polygoneDAO,sessionFactory,dataSource,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'lineController'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'lineController'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'lineController' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'lineService'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'lineService'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:529 - Eagerly caching bean 'lineService' to allow for resolving potential circular references
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:221 - Creating shared instance of singleton bean 'lineDAO'
2016-08-08 10:34:39 DEBUG DefaultListableBeanFactory:448 - Creating instance of bean 'lineDAO'
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
Hibernate: 
    alter table LINE_POINT 
        drop constraint FKr9leyoj8dx4dgeny8vlkcktj6
10:34:42.014 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table LINE_POINT drop constraint FKr9leyoj8dx4dgeny8vlkcktj6
10:34:42.016 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - ERROR: relation "line_point" does not exist
Hibernate: 
    alter table LINE_POINT 
        drop constraint FKt6peoljxcllnf73vhg0afdkco
10:34:42.017 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table LINE_POINT drop constraint FKt6peoljxcllnf73vhg0afdkco
10:34:42.017 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - ERROR: relation "line_point" does not exist
Hibernate: 
    alter table POLY_POINT 
        drop constraint FK9osmn1ou9viwdivi798xhd7lf
10:34:42.017 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table POLY_POINT drop constraint FK9osmn1ou9viwdivi798xhd7lf
10:34:42.017 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - ERROR: relation "poly_point" does not exist
Hibernate: 
    alter table POLY_POINT 
        drop constraint FK9g9qmfbc78r5y42p0rcbe5par
10:34:42.018 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - HHH000389: Unsuccessful: alter table POLY_POINT drop constraint FK9g9qmfbc78r5y42p0rcbe5par
10:34:42.018 [main] ERROR org.hibernate.tool.hbm2ddl.SchemaExport - ERROR: relation "poly_point" does not exist
Hibernate: 
    drop table if exists COORDINATES cascade
Hibernate: 
    drop table if exists LINE_POINT cascade
Hibernate: 
    drop table if exists LINES cascade
Hibernate: 
    drop table if exists POINTS cascade
Hibernate: 
    drop table if exists POLY_POINT cascade
Hibernate: 
    drop table if exists POLYGONES cascade
Hibernate: 
    create table COORDINATES (
        ID_Coordinate  serial not null,
        X float8,
        Y float8,
        primary key (ID_Coordinate)
    )
Hibernate: 
    create table LINE_POINT (
        idLine int4 not null,
        idPoint int4 not null,
        primary key (idLine, idPoint)
    )
Hibernate: 
    create table LINES (
        ID_LINE  serial not null,
        primary key (ID_LINE)
    )
Hibernate: 
    create table POINTS (
        ID_POINT  serial not null,
        primary key (ID_POINT)
    )
Hibernate: 
    create table POLY_POINT (
        idPolygone int4 not null,
        idPoint int4 not null,
        primary key (idPolygone, idPoint)
    )
Hibernate: 
    create table POLYGONES (
        ID_POLYGONE  serial not null,
        primary key (ID_POLYGONE)
    )
Hibernate: 
    alter table LINE_POINT 
        add constraint FKr9leyoj8dx4dgeny8vlkcktj6 
        foreign key (idPoint) 
        references POINTS
Hibernate: 
    alter table LINE_POINT 
        add constraint FKt6peoljxcllnf73vhg0afdkco 
        foreign key (idLine) 
        references LINES
Hibernate: 
    alter table POLY_POINT 
        add constraint FK9osmn1ou9viwdivi798xhd7lf 
        foreign key (idPoint) 
        references POINTS
Hibernate: 
    alter table POLY_POINT 
        add constraint FK9g9qmfbc78r5y42p0rcbe5par 
        foreign key (idPolygone) 
        references POLYGONES
2016-08-08 10:34:42 WARN  ClassPathXmlApplicationContext:549 - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineDAO' defined in file [/home/amiri/Bureau/USB/Geom application/GeomApp/target/classes/fr/geom/core/repository/LineDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [fr.geom.core.repository.LineDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy30 cannot be cast to org.hibernate.SessionFactory
2016-08-08 10:34:42 DEBUG DefaultListableBeanFactory:512 - Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1b6e60b7: defining beans [lineController,pointController,polygoneController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.context.event.internalEventListenerProcessor,org.springframework.context.event.internalEventListenerFactory,lineService,pointService,polygoneService,lineDAO,pointDAO,polygoneDAO,sessionFactory,dataSource,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor]; root of factory hierarchy
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineDAO' defined in file [/home/amiri/Bureau/USB/Geom application/GeomApp/target/classes/fr/geom/core/repository/LineDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [fr.geom.core.repository.LineDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy30 cannot be cast to org.hibernate.SessionFactory
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:776)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at fr.geom.core.App.main(App.java:18)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineService': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineDAO' defined in file [/home/amiri/Bureau/USB/Geom application/GeomApp/target/classes/fr/geom/core/repository/LineDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [fr.geom.core.repository.LineDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy30 cannot be cast to org.hibernate.SessionFactory
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:321)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:522)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
    ... 13 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lineDAO' defined in file [/home/amiri/Bureau/USB/Geom application/GeomApp/target/classes/fr/geom/core/repository/LineDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [fr.geom.core.repository.LineDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy30 cannot be cast to org.hibernate.SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:522)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318)
    ... 26 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [fr.geom.core.repository.LineDAO]: Constructor threw exception; nested exception is java.lang.ClassCastException: com.sun.proxy.$Proxy30 cannot be cast to org.hibernate.SessionFactory
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
    ... 39 more
Caused by: java.lang.ClassCastException: com.sun.proxy.$Proxy30 cannot be cast to org.hibernate.SessionFactory
    at fr.geom.core.repository.LineDAO.<init>(LineDAO.java:14)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
    ... 41 more

【讨论】:

  • 这不是答案。这是问题中的额外代码。
【解决方案2】:

错误是SessionFactory.getCurrentSession()返回Session,而不是SessionFactory,因此类转换异常。

删除.getCurrentSession()

        @Repository
    public class LineDAO implements GeometryDaoInterface {

        @Resource
        private SessionFactory sessionFactory = (SessionFactory) HibernateUtil.getSessionFactory().getCurrentSession();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-23
    • 2013-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多