线程“main”中的异常org.hibernate.mappingexception:无法加载声明为< mapping class =" org.hibernate.session.emp" />的类 [英] Exception in thread "main" org.hibernate.mappingexception: unable to load class declared as <mapping class="org.hibernate.session.emp"/>

查看:41
本文介绍了线程“main”中的异常org.hibernate.mappingexception:无法加载声明为< mapping class =" org.hibernate.session.emp" />的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我对Java中的Hibernate完全不熟悉。我正在尝试访问数据库,但我收到此错误。请帮我算一下。非常感谢你的帮助。这就是我在试图访问的课程中尝试过的。



我尝试了什么:



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





和配置文件:.xml





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>





和Emp类:





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;
	}
	
	
}

推荐答案

< / 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>
</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>





和Emp类:





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;
	}
	
	
}


这篇关于线程“main”中的异常org.hibernate.mappingexception:无法加载声明为&lt; mapping class =&quot; org.hibernate.session.emp&quot; /&gt;的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆