Hibernate配置用于将属性映射到数据库表而不是映射

时间:2016-08-17 17:33:45

标签: java hibernate

我正在创建一个示例任务来将属性从java类映射到     数据库表。但我无法做到这一点,因为它产生了异常     找不到课程。即使我已在配置中正确设置路径     文件。这是我的项目的示例代码

Student.java
____________
package beans;

public class Student {
private int id;
private String name;
private String email;
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 String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
}

Student.hbm.xml
__________________
<?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="beans.Student" table="Student">
 <id name="id" column="id"/>
 <property name="name" column="name"/>
 <property name="email" column="email"/>
 </class>
 </hibernate-mapping>

hibernate.cfg.xml
___________________
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/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/loginuser</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="beans/Student.java"></mapping>
</session-factory>
</hibernate-configuration>

Client.java
________________
package test;

import org.hibernate.cfg.Configuration;

public class Client {

public static void main(String[] args) {
    Configuration cfg=new Configuration();
    cfg.configure("resources/hibernate.cfg.xml");
    cfg.buildSessionFactory();
}
}

Error Stack Trace
________________
log4j:WARN No appenders could be found for logger   
(org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.MappingNotFoundException: resource: 
beans/Student.java not found
at org.hibernate.cfg.Configuration.addResource(Configuration.java:517)
at org.hibernate.cfg.Configuration.parseMappingElement  
(Configuration.java:1511)
at org.hibernate.cfg.Configuration.parseSessionFactory
(Configuration.java:1479)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1458)
at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1432)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1352)
at test.Client.main(Client.java:9)

2 个答案:

答案 0 :(得分:0)

映射到映射文件而不是实体本身

<mapping resource="Student.hbm.xml"/>

答案 1 :(得分:0)

映射应该是hbm文件而不是类文件