0

This is my first Hibernate Application Program. I got this error when executing the main class.I Am trying to insert new record at the table student. Also it is good to say that hibernate doing the connection to the database without any problem !I keep getting this MappingException:

Hibernate: drop table if exists student 
Hibernate: create table student  (student_id integer not null auto_increment, First_Name varchar(255), Last_Name varchar(255), Age integer,  primary key (student_id))
Dec 22, 2015 10:04:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Exception in thread "main" org.hibernate.MappingException: Unknown entity: org.hibernate.internal.SessionImpl
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1146)
at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1358)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:116)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:206)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:191)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:683)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:675)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
at biztime.Manager.main(Manager.java:23)

Does anyone have any ideas as I've looked at so many duplicates but the resolutions don't appear to work for me.This is my cfg.xml

My hibernate.cfg.xml

   <!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="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/anwardb</property>
    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">create-drop</property>
    <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
    <mapping resource="biztime/student.hbm.xml"/>    
    </session-factory>
    </hibernate-configuration>

My student.java

package biztime;
public class student 
{
    private int student_id;
    private String First_Name;
    private String Last_Name;
    private int Age;
    public int getStudent_id() 
    {
      return student_id;
    }

 public void setStudent_id(int student_id) 
  {
    this.student_id = student_id;
  }
public String getFirst_Name() 
{
    return First_Name;
}
public void setFirst_Name(String first_Name) 
{
    First_Name = first_Name;
}

public String getLast_Name() 
{
    return Last_Name;
}

public void setLast_Name(String last_Name) 
{
    Last_Name = last_Name;
}

  public int getAge() 
 {
    return Age;
 }

  public void setAge(int age)

  {
    Age = age;
  }
}

student.hbm.xml

     <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">


    <hibernate-mapping package="biztime">

    <class name="student" table="student ">
    <id name="student_id" column="student_id">
        <generator class="native"/>
    </id>
    <property name="First_Name" >
        <column name="First_Name"/>
    </property>
    <property name="Last_Name">
        <column name="Last_Name"/>
    </property>
    <property name="Age">
        <column name="Age"/>
    </property>
</class>

</hibernate-mapping>

I call it using this main:

Manager.java

  package biztime;
  import org.hibernate.Session;
  import org.hibernate.SessionFactory;
  import org.hibernate.cfg.Configuration;

   public class Manager 
  {

   public static void main(String[] args) 
  {

    Configuration con=new Configuration();
    con.configure();
    SessionFactory sf=con.buildSessionFactory();

    Session s1=sf.openSession();
    student  s=new student ();
    s.setFirst_Name("hello");
    s.setLast_Name("test");
    s.setStudent_id(1);
    s.setAge(20);
    s1.beginTransaction();
    s1.save(s1);
    s1.getTransaction().commit();
    s1.flush();
    s1.close();
    System.out.println("done");

    }
 }
0

2 Answers 2

1

You're attempting to save s1 which is a Session. Save s instead, which is a student (and capitalize your classnames).

Sign up to request clarification or add additional context in comments.

Comments

0

I don't think you should set student_id, as it is an auto_increment field. Try removing this line: s.setStudent_id(1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.