how to resolve ==> org.hibernate.exception.GenericJDBCException: Could not open connection - java

when i execute my Main class i get this execution
cant figure out the issue point
the error comes in the line
Transaction tr = session.beginTransaction();
error stack says :
ERROR: Access denied for user 'root'#'localhost' (using password: NO)
error===>org.hibernate.exception.GenericJDBCException: Could not open connection
my Main class File :
package com.hussi.model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Main {
public static void main(String[] args)
{
User user = new User();
user.setUser_id(1);
user.setUsername("hussi");
user.setPassword("maria");
SessionFactory sesionFactory = new Configuration().configure().buildSessionFactory() ;
Session session = sesionFactory.openSession();
try{
Transaction tr = session.beginTransaction();
session.save(user);
}
catch(Exception e)
{
System.out.println("error===>"+e);
}
finally
{
session.flush();
session.close();
}
}
}
my model file
package com.hussi.model;
public class User
{
int user_id;
String username;
String password;
public int getUser_id() {
return user_id;
}
public void setUser_id(int user_id) {
this.user_id = user_id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString()
{
return "username==>"+this.username+" : password==>"+this.password;
}
}
my user.hbm.xml file
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hussi.model.User" table="users">
<id name="user_id" type="int" column="user_id">
<generator class="increment" />
</id>
<property name="username">
<column name="username"/>
</property>
<property name="password">
<column name="password"/>
</property>
</class>
</hibernate-mapping>
my hibernate configuration file : hibernate.cfg.xml
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/my_hibernate_1</property>
<property name="connection.username">root</property>
<property name="connecttion.password">root</property>
<!-- Database connection settings -->
<property name="connection.pool_size">1</property>
<!-- MySql Dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">false</property>
<mapping resource="user.hbm.xml"/>
</session-factory>
</hibernate-configuration>

I believe you need to reset your database password. Follow this link to do the same:
http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html
or the user priviliges are not correct. Follow this to set priviliges:
http://dev.mysql.com/doc/refman/5.1/en/default-privileges.html

Related

Hibernate + SQLite do not create tables

I need SQLite database in my project, but SQL is not as handy as HQL. So we have Hibernate with SQLite.
Project is being made in NetBeans 8.2, my OS is Ubuntu 17.10.
So for example we have this mini-project for testing
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.sqlite.JDBC</property>
<property name="hibernate.connection.url">jdbc:sqlite:stuff.db</property>
<property name="hibernate.dialect">com.enigmabridge.hibernate.dialect.SQLiteDialect</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="stuff/Stuff.hbm.xml"/>
</session-factory>
</hibernate-configuration>
My class called Stuff:
package stuff;
public class Stuff {
private Integer id;
private String stuff;
public Stuff() {
}
public Stuff(String stuff) {
this.stuff = stuff;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getStuff() {
return stuff;
}
public void setStuff(String stuff) {
this.stuff = stuff;
}
#Override
public String toString() {
return "Stuff{" + "id=" + id + ", stuff=" + stuff + '}';
}
}
Mapping file
<hibernate-mapping>
<class name="stuff.Stuff" table="stuff">
<id name="id" type="integer" column="id">
<generator class="increment"/>
</id>
<property name="stuff" type="string" column="stuff"/>
</class>
</hibernate-mapping>
And the main code
package stuff;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
public class Main {
public static void main(String[] args) {
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(new Stuff("blah blah"));
//Query q = s.createQuery("select from Stuff");
//List<Stuff> l = q.list();
//for (Stuff s:l)System.out.println(s);
session.getTransaction().commit();
session.close();
System.exit(0);
}
}
Data base was created via Services tab in NetBeans. Its name is stuff.db.
I use Hibernate 4.3 (JPA 2.1)
Hibernate 4 SQLite dialect was taken from here: link
Session opens and closes just fine, database file is automatically created in my home folder. But for some reason table just isn't created, I really dunno why.
pom: SQLite JDBC library
<dependency>
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.7.2</version>
</dependency>
in hibernate config:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.SQLiteDialect</property>
<property name="connection.driver_class">org.sqlite.JDBC</property>
<property name="connection.url">jdbc:sqlite:mydb.db</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="your mapping class"/>
</session-factory>
</hibernate-configuration>

hibernate unknown entity when using hibernate 5 java 8

when i want to save my object it throws unknown entity exception.
tnx in advance
Exception in thread "main" org.hibernate.MappingException: Unknown
entity: com.simpleProgrammer.User at
org.hibernate.metamodel.internal.MetamodelImpl.entityPersister(MetamodelImpl.java:618)
at
org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1595)
at
org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:104)
at
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at
org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at
org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at
org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:667)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:659) at
org.hibernate.internal.SessionImpl.save(SessionImpl.java:654) at
com.simpleProgrammer.Program.main(Program.java:22)
my hibernate.cfg.xml file is here
<?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.cj.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/protein_tracker</property>
<property name="hibernate.connection.username">root</property>
<!-- <property name="hibernate.default_schema"></property> -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">create</property>
<mapping resource="com/simpleProgrammer/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
my User.hbm.xml mapping file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping">
<hibernate-mapping>
<class name="com.simpleProgrammer.User" table="USERS">
<id name="id" type="int" column="id">
<generator class="identity" />
</id>
<property name="name" column="NAME" type="string" />
<component name="proteinData">
<property name="total" column="TOTAL" type="int" />
<property name="goal" column="GOAL" type="int" />
</component>
<set name="history" table="USER_HISTORY">
<key column="ID"/>
<composite-element class="com.simpleProgrammer.UserHistory">
<property name="entryTime" type="date" column="ENTRY_TIME"/>
<property name="entry" type="string" column="ENTRY"/>
</composite-element>
</set>
</class>
</hibernate-mapping>
my progaram.java
package com.simpleProgrammer;
import java.util.Date;
import org.hibernate.Session;
public class Program {
public static void main(String[] args) {
Session session = new HibernateUtilities().getSessionFactory().openSession();
System.out.println("Session Opened!!!");
session.beginTransaction();
System.out.println("Transaction begined!");
User user = new User();
user.setName("Joe");
user.getHistory().add(new UserHistory(new Date(), "Setting Name to Joe"));
user.getProteinData().setGoal(250);
user.getHistory().add(new UserHistory(new Date(), "Setting Goal to 250!"));
System.out.println("Setting user Object");
session.save(user);
System.out.println("Saving user object on table");
session.getTransaction().commit();
System.out.println("Transaction Commited!");
session.beginTransaction();
User loadedUser = session.get(User.class, 1);
System.out.println(loadedUser.getName());
System.out.println(loadedUser.getProteinData().getGoal());
System.out.println(loadedUser.getProteinData().getTotal());
loadedUser.getProteinData().setTotal(loadedUser.getProteinData().getTotal() + 50);
loadedUser.getHistory().add(new UserHistory(new Date(), "Added 50 Protein!"));
for (UserHistory history : loadedUser.getHistory()) {
System.out.println(history.getEntryTime().toString() + " " + history.getEntry());
}
session.getTransaction().commit();
session.close();
System.out.println("Session Closed!!!");
HibernateUtilities().getSessionFactory().close();
System.out.println("Session Factory Closed");
}
}
user class
package com.simpleProgrammer;
import java.util.HashSet;
import java.util.Set;
public class User {
private int id;
private String name;
private ProteinData proteinData = new ProteinData();
private Set<UserHistory> history = new HashSet<UserHistory>();
public Set<UserHistory> getHistory() {
return history;
}
public void setHistory(Set<UserHistory> history) {
this.history = history;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ProteinData getProteinData() {
return proteinData;
}
public void setProteinData(ProteinData proteinData) {
this.proteinData = proteinData;
}
}
here is my project structure
I think you should check your other configure file."Unknown entity" means your application doesn't find something like User.hbm.xml in the path.

org.hibernate.exception.JDBCConnectionException: Cannot open connection

for testing purpose i wrote very small code,which repeatedly throws exception related to jdbc driver.
package sally;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class MainClass {
public static void main(String args[]){
try{
Session session=HibernateUtil.getSession();
Transaction tx=session.beginTransaction();
Employee ee=new Employee("abx","asd",2000);
session.save(ee);
tx.commit();
}catch(Exception e){
e.printStackTrace();
}
}
}
This is my ben/pojo class for mapping
package sally;
public class Employee {
private int id;
private String firstName;
private String lastName;
private int salary;
public Employee() {}
public Employee(String fname, String lname, int salary) {
this.firstName = fname;
this.lastName = lname;
this.salary = salary;
}
public int getId() {
return id;
}
public void setId( int id ) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName( String first_name ) {
this.firstName = first_name;
}
public String getLastName() {
return lastName;
}
public void setLastName( String last_name ) {
this.lastName = last_name;
}
public int getSalary() {
return salary;
}
public void setSalary( int salary ) {
this.salary = salary;
}
}
<?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="sally.Employee" table="EMPLOYEE">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<id name="id" type="int" column="id">
<generator class="native"/>
</id>
<property name="firstName" column="first_name" type="string"/>
<property name="lastName" column="last_name" type="string"/>
<property name="salary" column="salary" type="int"/>
</class>
</hibernate-mapping>
and this is cfg file for pojo class:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/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/test</property>
<property name="hibernate.connection.password"></property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<mapping class="sally.Employee"></mapping>
</session-factory>
but every time i am running the code it throws exxception :
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
org.hibernate.exception.JDBCConnectionException: Cannot open connection
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420)
at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119)
at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326)
at sally.MainClass.main(MainClass.java:12)
Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:187)
at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417)
... 5 more
can any body tell whats wrong here???
this is db column list : first_name last_name salary.
You need to put the mysql JDBC driver JAR on your classpath
name for the driver class must be updated as well as server time zone (for me this is insane but it has to be done
<?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.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?serverTimezone=UTC</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
org.hibernate.exception.JDBCConnectionException: Cannot open connection It occurs when you have not started the xampp and MySql.

org.hibernate.MappingNotFoundException

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();

Can not established a connection to jdbc with Hibernate-SQL Server 2008

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.

Categories