I have a basic test for an embedded database that I'm trying to get working for a java project. As it stands now I am able to save rows to the database with my entity repository object, and after the app finishes running, I can connect to the database with intellij and see those rows are still there. But then, if I comment out the save methods and run it again, when I check the database after it finishes, the database is empty.
Entity
package closet.utilities.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
#Entity
#Table(name = "outfits")
public class Outfit {
#Id
#Column(name = "id")
String id;
#Column(name = "inv_name")
String invName;
#Column(name = "display_name")
String displayName;
#Column(name = "owner")
String owner;
public Outfit() {
}
public Outfit(String id, String invName, String displayName, String owner) {
this.id = id;
this.invName = invName;
this.displayName = displayName;
this.owner = owner;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getInvName() {
return invName;
}
public void setInvName(String invName) {
this.invName = invName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
#Override
public String toString() {
return "Outfit{" +
"id='" + id + '\'' +
", invName='" + invName + '\'' +
", displayName='" + displayName + '\'' +
", owner='" + owner + '\'' +
'}';
}
}
Repository
package closet.utilities.repositories;
import closet.utilities.entities.Outfit;
import javax.persistence.EntityManager;
import java.util.List;
import java.util.Optional;
public class OutfitRepository {
private EntityManager entityManager;
public OutfitRepository(EntityManager entityManager) {
this.entityManager = entityManager;
}
public Optional<Outfit> findById(String id) {
Outfit outfit = entityManager.find(Outfit.class, id);
return outfit != null ? Optional.of(outfit) : Optional.empty();
}
public List<Outfit> findAll() {
return entityManager.createQuery("from Outfit").getResultList();
}
public Optional<Outfit> save(Outfit outfit) {
try {
entityManager.getTransaction().begin();
entityManager.persist(outfit);
entityManager.getTransaction().commit();
return Optional.of(outfit);
} catch (Exception e) {
// TODO logging
e.printStackTrace();
}
return Optional.empty();
}
}
main method
package closet.utilities;
import closet.utilities.entities.Outfit;
import closet.utilities.repositories.OutfitRepository;
import org.hibernate.Session;
import org.hibernate.Transaction;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.List;
public class Runnable {
public static void main(String[] args) {
// Create our entity manager
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("closet");
EntityManager entityManager = entityManagerFactory.createEntityManager();
Outfit outfit = new Outfit("Ramesh", "Fadatare", "rameshfadatare#javaguides.com", "");
Outfit outfit1 = new Outfit("John", "Cena", "john#javaguides.com", "");
OutfitRepository outfitRepository = new OutfitRepository(entityManager);
//outfitRepository.save(outfit);
//outfitRepository.save(outfit1);
List<Outfit> outfits = outfitRepository.findAll();
for (Outfit o : outfits) {
System.out.println(o.getInvName());
}
}
}
persistence.xml
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="closet" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>closet.utilities.entities.Outfit</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:./data/closet" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="show_sql" value="true"/>
<property name="hibernate.temp.use_jdbc_metadata_defaults" value="false"/>
</properties>
</persistence-unit>
</persistence>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- JDBC Database connection settings -->
<property name="connection.driver_class">org.h2.Driver</property>
<property name="connection.url">jdbc:h2:./data/closet</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool settings ... using built-in test pool -->
<property name="connection.pool_size">1</property>
<!-- Select our SQL dialect -->
<property name="dialect">org.hibernate.dialect.H2Dialect</property>
<!-- Echo the SQL to stdout -->
<property name="show_sql">true</property>
<!-- Set the current session context -->
<property name="current_session_context_class">thread</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create-drop</property>
<!-- dbcp connection pool configuration -->
<property name="hibernate.dbcp.initialSize">5</property>
<property name="hibernate.dbcp.maxTotal">20</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.minIdle">5</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>
<mapping class="closet.utilities.entities.Outfit" />
</session-factory>
</hibernate-configuration>
I'm just following this tutorial here as a guide to just figure out how to get it all working. I can't see what I'm doing wrong based on that.
You use:
<property name="hbm2ddl.auto">create-drop</property>
As it is stated in the documentation:
create-drop
Drop the schema and recreate it on SessionFactory startup. Additionally, drop the schema on SessionFactory shutdown.
So, this is expected behaviour.
Related
Sorry 4 my bad English :P
Now I'm testing Hibernate, on MySQL it was OK, but on PostgreSQl I faced with problem (`cause I connected to Heroku DB).
I'm trying to connect to DB, but it throws to me exceptions (I had already checked all properties in hibernate.cfg.xml):
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
Caused by: org.hibernate.HibernateException: Unable to make JDBC Connection [jdbc:postgres://username:password#host:5432/database]
java.lang.NullPointerException: Cannot invoke "org.hibernate.SessionFactory.openSession()" because the return value of "ua.sillmarry.ArtToolsBot.util.HibernateUtil.getSessionFactory()" is null
There is my hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgres://username:password#host:5432/database</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.PostgreSQL94Dialect</property>
<property name="show_sql">true</property>
<property name="current_session_context_class">thread</property>
<property name="hbm2ddl.auto">validate</property>
<property name="hibernate.dbcp.initialSize">5</property>
<property name="hibernate.dbcp.maxTotal">20</property>
<property name="hibernate.dbcp.maxIdle">10</property>
<property name="hibernate.dbcp.minIdle">5</property>
<property name="hibernate.dbcp.maxWaitMillis">-1</property>
<mapping class="ua.sillmarry.ArtToolsBot.entity.Brush" />
</session-factory>
</hibernate-configuration>
Class HibernateSessionFactoryUtil:
package ua.sillmarry.ArtToolsBot.util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class HibernateSessionFactoryUtil {
private static StandardServiceRegistry registry;
private static SessionFactory sessionFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
registry = new StandardServiceRegistryBuilder().configure().build();
MetadataSources sources = new MetadataSources(registry);
Metadata metadata = sources.getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
} catch (Exception e) {
e.printStackTrace();
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
return sessionFactory;
}
public static void shutdown() {
if (registry != null) {
StandardServiceRegistryBuilder.destroy(registry);
}
}
}
Class Brush (mapping):
package ua.sillmarry.ArtToolsBot.entity;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
#Data
#Entity
#Table(name = "brushes")
public class Brush implements Serializable {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "brushes")
#Column(name = "id")
private int id;
#Column(name = "name")
private String name;
#Column(name = "category")
private String category;
#Column(name = "gd_brush")
private String gd_brush;
#Column(name = "gd_img")
private String gd_img;
public String toString() {
return "Brush{\n" +
"id=" + id + ";\n" +
"name='" + name + "';\n" +
"category='" + category + "';\n" +
"gd_brush='" + gd_brush + "';\n" +
"gd_image='" + gd_img + "'.";
}
}
I'm just getting started trying to use Hibernate to create a PostgreSQL database but keep getting this exception:
org.hibernate.HibernateException: java.lang.IllegalArgumentException: max size attribute is mandatory
It happens when I run my main method on the line where I try to build a session factory but I can't find anything about this exception anywhere online. I'm wondering anyone has any ideas on what could be causing this.
Main Class:
package com.package.ingestor;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args){
Student user = new Student();
user.setStudentId(1);
user.setStudentName("Bri Guy");
user.setStudentAge(32);
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
Hibernate Object:
package com.bossanova.ingestor;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
#Entity
#Table(name = "student", uniqueConstraints={#UniqueConstraint(columnNames={"id"})})
public class Student {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
#Column(name = "id", length=11, nullable=false, unique=true)
private Integer studentId;
#Column(name = "name", length=20, nullable=true)
private String studentName;
#Column(name="age", length=5, nullable=true)
private Integer studentAge;
public Student() { }
public Student(Integer studId, String studName, Integer studAge) {
this.studentId = studId;
this.studentName = studName;
this.studentAge = studAge;
}
public Integer getStudentId() {
return studentId;
}
public void setStudentId(Integer studentId) {
this.studentId = studentId;
}
public String getStudentName() {
return studentName;
}
public void setStudentName(String studentName) {
this.studentName = studentName;
}
public Integer getStudentAge() {
return studentAge;
}
public void setStudentAge(Integer studentAge) {
this.studentAge = studentAge;
}
#Override
public String toString() {
return "Student= Id: " + this.studentId + ", Name: " + this.studentName + ", Age: " + this.studentAge;
}
}
Config file:
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="connection.url">jdbc:postgresql://localhost:5433/hibernatedb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL95Dialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping class="com.bossanova.ingestor.Student"/>
</session-factory>
</hibernate-configuration>
I solved this for me by switching my Maven dependency from hibernate-agroal to hibernate-core.
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.0.Final</version>
</dependency>
If you are using hibernate-agroal add <type>pom</type> to your dependency
I'm trying to fetch resultset by making the query in Hibernate like
String id = "10";
Person per = session.load(Person .class, id); // This is wrong, because it accepts only an integer, not a string.
But, I require to fetch results using session.load by passing a string, because second level cache is not triggering when I try to fetch as
Criteria cr = session.createCriteria(Person.class);
cr.add(Restrictions.like("userId", id));
List<?> results = cr.list();
Person class
#Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
#Entity
#Table(name = "person")
public class Person {
#Override
public String toString() {
return "Personalisation [id=" + id + ", userId=" + userId
+ ", courseId=" + courseId + ", courseValue=" + courseValue
+ "]";
}
#Id
#GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String userId;
private String courseId;
private String courseValue;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getCourseId() {
return courseId;
}
public void setCourseId(String courseId) {
this.courseId = courseId;
}
public String getCourseValue() {
return courseValue;
}
public void setCourseValue(String courseValue) {
this.courseValue = courseValue;
}
}
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/intu</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Ehcache config -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="net.sf.ehcache.configurationResourceName">/ehcache.xml</property>
<!-- c3p0 Connection pool config -->
<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">100</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.validate">true</property>
<property name="hibernate.c3p0.privilegeSpawnedThreads">true</property>
<property name="hibernate.c3p0.contextClassLoaderSource">library</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping class="com.intu.Dashboard" />
<mapping class="com.intu.Employee"/>
<mapping class="com.intu.Person"/>
The above code is working, but not by second level cache, each time its hitting DB.
What steps do I need to do to get it done? Is there any other way
Please call cr.setCacheable(true) after cr.add(Restrictions.like("userId", id)).
I am trying to do a simple hibernate program. I am following the steps given in this tutorial.
The Error that I am getting is
org.hibernate.MappingNotFoundException: resource: org.manu.dtd.UserDetails not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:799)
at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:2344)
at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:2310)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2290)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2243)
at org.hibernate.cfg.Configuration.configure(Configuration.java:2158)
at org.manu.dtd.TestHibernate.main(TestHibernate.java:16)
Here is my folder structure
My persitence class with annotations is
package org.manu.dtd;
import javax.persistence.Entity;
import javax.persistence.Id;
#Entity
public class UserDetails {
#Id
private int userId;
private String userName;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
My hibernate.cfg.xml file is
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:#localhost:1521:XE</property>
<property name="connection.username">user</property>
<property name="connection.password">password</property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="org.manu.dtd.UserDetails"/>
</session-factory>
</hibernate-configuration>
And finally my main program is
package org.manu.dtd;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TestHibernate {
public static void main(String[] args) {
UserDetails user = new UserDetails();
user.setUserId(23);
user.setUserName("Renu");
System.out.println("setting values complete");
try {
SessionFactory sF = new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
Session session = sF.openSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
catch (HibernateException he) {
he.printStackTrace();
}
}
}
Can anyone please help me out to resolve this issue.
Well it looks it can be fixed even easier. You should use <mapping class=..> instead of <mapping resource=..> as resources is for mapping other xml files describing entities and such. Here is small example from the offical tutorials
Provide fully qualified name for hibernate configuration mapping file,
SessionFactory sF = new Configuration().
configure("/org/manu/dtd/hibernate.cfg.xml").buildSessionFactory();
I made a connection to SQL server with Hibernate configuration wizard, when I click test connection, I get success message, but when I want to make hibernate mapping, I cannot I get error message, Cannot established a connection to jdbc. The below code is jdbc connection with hibernate; please say me, what is the problem?
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="hibernate.connection.url">jdbc:sqlserver://localhost; databaseName=Test</property>
<property name="hibernate.connection.username">sa</property>
<property name="hibernate.connection.password">sa123</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
</session-factory>
</hibernate-configuration>
Best Regards
After looking into your code i found you did not specify the port number for database connection
<property name="hibernate.connection.url">jdbc:sqlserver://localhost; databaseName=Test</property>
There should be a port number if your trying to connect on your local machine for an example please refer following line
<property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=syv</property>
Please check and let me know ...if there is any further issue
Ok here i am sending you the basic hibernate configuration : -
STEP 1 :- *Create a Java File “AddStudent” and paste the below code into that.*
//package code;
import java.sql.*;
import java.io.*;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class AddStudent {
private static SessionFactory sessionFactory;
public static void main(String args[]) throws Exception {
DataInputStream d = new DataInputStream(System.in);
System.out.println("ENTER YOUR NAME");
String name = d.readLine();
System.out.println("ENTER YOUR DEGREE");
String degree = d.readLine();
System.out.println("ENTER YOUR PHONE");
String phone = d.readLine();
System.out.println("Name: " + name);
System.out.println("Degree: " + degree);
System.out.println("Phone: " + phone);
if ((name.equals("") || degree.equals("") || phone.equals(""))) {
System.out.println("All informations are Required");
} else {
try {
// begin try
sessionFactory = new Configuration().configure()
.buildSessionFactory();
} catch (Exception e) {
System.out.println(e.getMessage());
System.err.println("Initial SessionFactory creation failed."
+ e);
}
Session s = sessionFactory.openSession();
Transaction tx = s.beginTransaction();
Student stu = new Student();
stu.setName(name);
stu.setDegree(degree);
stu.setPhone(phone);
s.save(stu);
tx.commit();
System.out.println("Added to Database");
if (s != null)
s.close();
}
}
}
STEP 2 : Create a Java File “Student” paste the below code into that.
//package code;
import java.io.*;
public class Student implements Serializable {
private long id;
private String name;
private String degree;
private String phone;
public long getId() {
return id;
}
public String getName() {
return name;
}
public String getDegree() {
return degree;
}
public String getPhone() {
return phone;
}
public void setId(long string) {
id = string;
}
public void setName(String string) {
name = string;
}
public void setDegree(String string) {
degree = string;
}
public void setPhone(String string) {
phone = string;
}
public String toString() {
return name;
}
}
Step 3: Create an xml file “hibernate.cfg.xml” place the below code into that.
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="studentFactory">
<property name="connection.driver_class">
oracle.jdbc.OracleDriver
</property>
<property name="connection.url">
jdbc:oracle:thin:#localhost:1521:XE
</property>
<property name="connection.username">
system
</property>
<property name="connection.password">
system
</property>
<property name="connection.pool_size">5</property>
<!-- SQL dialect -->
<property name="dialect">
org.hibernate.dialect.OracleDialect
</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<mapping resource="Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
Step 4:Create an xml File “student.hbm.xml” and place below code into that.
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class name="Student" table="Stutbl" >
<id name="id" type="long" column ="ID">
<generator class="increment"/>
</id>
<property name="name" column="name" not-null="true"/>
<property name="degree" column="degree" />
<property name="phone" column="phone" />
</class>
</hibernate-mapping>
Step 5: Add the required jar to your project
Hope it will solve your problem
Add this:
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
and verify if your Database accept connections.