1

Simple pojo class

package practice041116;
public class Cls {
public String name;
public int roll;
public Cls(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRoll() {
return roll;
}
public void setRoll(int roll) {
this.roll = roll;
}
}

The main login file

package practice041116; 
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration; 
import org.hibernate.SessionFactory;
public class MainClass {
public static void main(String args[]){
Configuration cnfg=new Configuration();
cnfg.configure("hibernate.cfg.xml");
SessionFactory fact=cnfg.buildSessionFactory();
Session session=fact.openSession();        
Transaction tx=session.beginTransaction();
Cls s=new Cls();
s.setName("Taleev");
s.setRoll(23);
session.save(s);
tx.commit();

} }

The configuration file

<?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.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="hibernate.connection.url">
jdbc:microsoft:sqlserver://171.8.9.1;DatabaseName:test
</property>
<property name="hibernate.connection.username">
username
</property>
<property name="hibernate.connection.password">
password
</property>
<property name="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</property>
<property name="show_sql">true</property>
<mapping resource="practice041116/Cls.hbm.xml" />
</session-factory>
</hibernate-configuration>

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-3.0.dtd">
<hibernate-mapping>
<class name="Cls" table="cls">
<id name="id" column="id" type="integer"/>
<property name="name" column="NAME" type="string"/>
<property name="roll" column="ROLL" type="integer"/>
</class>
</hibernate-mapping>

I have the specified table in my database but while execute it gives Entity Class not found Exception, i have tried various answer available on the similar question such as this one and few more from other sources from the net Hierarchy of the project and the jar files used Thanks in advance and forgive me if i have done some format mistake on this question as this is the first question i am posting. Stack trace of Exception

Community
  • 1
  • 1
Taleev Aalam
  • 49
  • 1
  • 9
  • Rather good presentation for first question :) but you should also post the whole exception stacktrace because exception summary message or exception name is often not enough. – davidxxx Nov 05 '16 at 09:34
  • In addition it would also be useful if you post your Cls class. – biro Nov 05 '16 at 09:37

2 Answers2

3

In your mapping file :

<class name="Cls" table="cls">

you don't specify qualified class name but just simple class name. You should replace by :

<class name="practice041116.Cls" table="cls">
davidxxx
  • 125,838
  • 23
  • 214
  • 215
  • but i am getting another error which is "Caused by: java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://171.8.9.1;DatabaseName:test" why i am getting is there any mistake on my code or i am missing something – Taleev Aalam Nov 05 '16 at 09:51
  • This is the exception " Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection " – Taleev Aalam Nov 05 '16 at 09:53
  • You are welcome. You should accept the answer and re-open a new question if actual problem is solved. When the problem is related, editing the quesiton/answers of the post is relevant but when it has no relation it makes readability of post harder for community. – davidxxx Nov 05 '16 at 09:58
  • Ok thanks @davidxxx i have got my answer on this problem, what do i have to do i should leave this topic as it is or is there any procedure to close the discussion – Taleev Aalam Nov 05 '16 at 10:01
  • You are welcome. You can read this information : stackoverflow.com/help/accepted-answer It explains the principle of accepted answer and you should the help in general because it contains some important information. Here, to summarize, when you ask a question and you consider one of answers has addressed it, you should check this answer as accepted. Concretely to do it, the author of a question has close to each proposed answer, a kind of mark he may check to consider is as accepted. When it is checked, a green mark is displayed. – davidxxx Nov 05 '16 at 10:13
  • It is just under the number. Look at this post : http://stackoverflow.com/questions/3052718/what-is-the-best-way-to-launch-hsqldb-for-unit-testing-when-working-with-spring?rq=1 Look at the first answer : you see where is the green mark . – davidxxx Nov 05 '16 at 10:17
1

Hibernate is an ORM framework which maps a Java Object (Entity Object) to a relational database table, as you did NOT map any Entity Class to a database table you are getting this exception.

Option(1): Using Annotations

You need to use @Entity (class level mapping) and @Column (element level mapping) to map the java object to a database table.

Option(2): Using mapping xml files

As sugested by David above and also you can look at here

Vasu
  • 21,832
  • 11
  • 51
  • 67