Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I'm completely new to Hibernate in java. I'm trying to access database but I'm getting this error. Please help me figure. Would greatly appreciate your help. This is what I have tried from the class where I'm trying to access.

What I have tried:

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.*;
import org.jboss.logging.Logger.Level;


public class InsertLec {
	public static void main(String[] args) {
		
		Configuration cfg = new Configuration(); //hibernate class to locate and refer the configuration file for this program
		cfg.configure("hibernate.cfg.xml");
		SessionFactory sf = cfg.buildSessionFactory();
		Session s = sf.openSession();
		Transaction tax = s.beginTransaction();
		Emp obj = new Emp();
		obj.setId(29);
		obj.setName("Vineeth");	
		obj.setMobile(7259190);
		obj.setEmail("vineeth@gmail.com");
		
        s.save(obj);
        s.flush();
        tax.commit();
        s.close();
        
        Emp  user = null;  //Now getting a user object from database table from session object
        Session s1= sf.openSession(); //Creating a new session object for fetching user object
        s1.beginTransaction(); //Again Open the transaction of the session object
       
        user = (Emp)s1.get(Emp.class,23);
        System.out.println(user.getId()+" - "+user.getName()+" - "+user.getMobile()+" - "+user.getEmail());
	}
}


and the configuration file : .xml

<!-- Hibernate file-based configuration document.-->

<!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:///sample</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">Hamburg89$</property>

<property name = "connection.pool_size">1</property>

<property name = "dialect">org.hibernate.dialect.MySQLDialect</property>

<property name = "current_session_context_class">thread</property>

<property name = "cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<property name = "format_sql">false</property>

<property name = "use_sql_comments">false</property>

<property name = "show_sql">true</property>

<property name = "hbm2ddl.auto">update</property>

<mapping class = "hiber.Emp"/>

</session-factory>

</hibernate-configuration>


And the Emp class :

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity
@Table(name = "Employee")
public class Emp {
	
	@Id
	private int Id;
	
	@Transient
	private String Name;
	
	@Column(name = "Mobile")
	private long Mobile;
	
	@Column(name = "Email")
	private String Email;

	public int getId() {
		return Id;
	}
	public void setId(int id) {
		Id = id;
	}
	
	public String getName() {
		return Name;
	}
	
	public void setName(String name) {
		Name = name;
	}
	
	public long getMobile() {
		return Mobile;
	}
	
	public void setMobile(long mobile) {
		Mobile = mobile;
	}
	
	public String getEmail() {
		return Email;
	}

	public void setEmail(String email) {
		Email = email;
	}
	
	
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900