I created a hibernate project using spring although it has any error/warning, anyone of hibernate methods works. Here is my code: Student.java
public class Student {
private long id;
private String name;
private String dept;
//getters and setters
}
StudentDao.java
public class StudentDao{
HibernateTemplate template;
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public void saveStudent(Student s){
template.save(s);
}
}
Main.java
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDao = (StudentDao) ctx.getBean("st");
Student s=new Student();
s.setId(5);
s.setName("Messi");
s.setDept("Physics");
studentDao.saveStudent(s);
System.out.println(" Successfully persisted the student. Please check your database for results.");
}
}
Configuration files are like that: applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=";
xmlns:xsi=";
xmlns:p=";
xsi:schemaLocation="
.0.xsd">
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value="***" />
</bean>
<bean id="localSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="ds"></property>
<property name="mappingResources">
<list>
<value>student.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQL10Dialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="connection.autocommit">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="localSessionFactory"></property>
<property name="checkWriteOperations" value="false"></property>
</bean>
<bean id="st" class="example.StudentDao">
<property name="template" ref="hibernateTemplate"></property>
</bean>
</beans>
student.hbm.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
".0.dtd">
<hibernate-mapping>
<class name="example.Student" table="student">
<id name="id">
<generator class="assigned"></generator>
</id>
<property name="name"></property>
<property name="dept"></property>
</class>
</hibernate-mapping>
I think the problem is about configuration files but I can not find it.