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

2
  • 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. Commented Nov 5, 2016 at 9:34
  • In addition it would also be useful if you post your Cls class. Commented Nov 5, 2016 at 9:37

2 Answers 2

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">
Sign up to request clarification or add additional context in comments.

6 Comments

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
This is the exception " Exception in thread "main" org.hibernate.exception.JDBCConnectionException: Cannot open connection "
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.
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
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.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.