med Tete med Tete - 4 months ago 9
Java Question

Spring hibernate Issue

Hibernate spring and maven
Im using hibernate 5 and spring 4 in a personal project. I am unable to find root cause of my problem.

Can anyone help me to find out issue with my code?

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


please can anyone help me ?

Answer

The errror is that SessionFactory.getCurrentSession() returns a Session, not a SessionFactory, thereby the class cast exception.

Remove the .getCurrentSession()

        @Repository
    public class LineDAO implements GeometryDaoInterface {

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