org.hibernate.mapping.Table.setName中的java.lang.NullPointerException

时间:2016-04-09 11:26:12

标签: hibernate nullpointerexception hibernate-mapping tablename

这是我的例外

Caused by: java.lang.NullPointerException
    at org.hibernate.mapping.Table.setName(Table.java:181)
    at org.hibernate.cfg.Configuration$MappingsImpl.addTable(Configuration.java:2937)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:344)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:327)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:178)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXml(Configuration.java:3816)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processHbmXmlQueue(Configuration.java:3808)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3796)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1412)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
    at sessionfactoryconfig.HibernateUtil.<clinit>(HibernateUtil.java:13)
    ... 31 more

这是我的hibernate.cfg.xml文件

<?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="connection.url">jdbc:mysql://127.0.0.1:3306/university</property>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="hbm2ddl.auto">update</property>
        <property name="connection.pool_size">10</property>
        <property name="show_sql">true</property>

        <mapping resource="mappingfiles/student.hbm.xml" />
    </session-factory>
</hibernate-configuration>

这是我的映射文件

<?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 package="mappingfiles">
    <class name="" table="" schema="university">
        <id name="id" column="id">
            <generator class="native"/>
        </id>
        <property name="name" column="name" />
        <property name="address" column="address" />
        <property name="mobile" column="mobile" />    
    </class>
</hibernate-mapping>

这是我的模态类

package modal;

public class Student {
    private int id;
    private String name;
    private String address;
    private String mobile;

    public Student() {
    }

    public Student(int id, String name, String address, String mobile) {
        this.id = id;
        this.name = name;
        this.address = address;
        this.mobile = mobile;
    }

    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 getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}

Screenshot

请帮助我在这个项目中它有很少的错误,我也创建了DAO但是现在我没有使用任何DAO。我首先以一种简单的方式做到这一点。我正在使用hibernate版本4.3.11。请帮忙。

1 个答案:

答案 0 :(得分:1)

提供名称和表属性的值。 Hibernate映射配置无效。

<class name="" table="" schema="university">

由于没有指定名称,您收到此错误。

Caused by: java.lang.NullPointerException
    at org.hibernate.mapping.Table.setName(Table.java:181)

编辑: 我认为你应该改为

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