Search code examples
hibernatenullpointerexceptionhibernate-mappingtablename

java.lang.NullPointerException at org.hibernate.mapping.Table.setName


this is my exception

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

this is my hibernate.cfg.xml 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="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>

and this is my 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 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>

and this is my modal class

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

please help me in this project it has very little mistake, i have created DAO also but right now i am not using any DAO. i am doing it just in a simple manner first. I am using hibernate version 4.3.11. please help.


Solution

  • Provide values for name and table attributes. Hibernate mapping configuration is invalid.

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

    You've got this error since there is no name specified.

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

    EDIT: I think you should change to

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