java hibernate database connection

1.9k Views Asked by At

I am new to Hibernate. I am trying to make a simple database driven application using Hibernate. But I am stuck. I can't even run a simple database query. I am getting this error:

log file generated while running the program

hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!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.dialect">org.hibernate.dialect.DerbyDialect</property>
    <property name="hibernate.connection.driver_class">org.apache.derby.jdbc.ClientDriver</property>
    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/dbtest </property>
    <property name="hibernate.connection.username">root</property>
    <property name="connection.password">pass</property>
    <property name="hibernate.connection.autocommit">false</property>
     <property name="current_session_context_class">thread</property>
    <property name = "show_sql">true</property>
    <property name = "format_sql">true</property>
    <property name = "hibernate.transaction.factory_class">org.hibernate.testing.cache.CachingRegionFactory     
</property>
    <property name = "hibernate.hbm2ddl.auto">create</property>
    <mapping resource="com/enroll/hibernate.hbm.xml"></mapping>
    <mapping class = "com/enroll/insert"></mapping>    
  </session-factory>
</hibernate-configuration>

hibernate.hbm.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
  <class catalog="user" name="com.enroll.UserDetail" table="users"/>
  <id column="id" name="id" type="java.lang.Integer">
    <generator class="assigned"/>
  </id>
  <property column="uname" name="uname" type="string"/>
   <property column="pword" name="pword" type="string"/>   
</hibernate-mapping>

UserDetail.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.enroll;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetail {
   @Id private int id;
    String uname;
    String pword;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getPword() {
        return pword;
    }

    public void setPword(String pword) {
        this.pword = pword;
    }
}

insert.java:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.enroll;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class insert {
    public static void main(String[] args){
       UserDetail user1 = new UserDetail();
       user1.setId(7);
       user1.setUname("shri");
       user1.setPword("password");


       SessionFactory sf = (SessionFactory) new Configuration().configure("hibernate.cfg.xml").buildSessionFactory();
       Session session = sf.openSession();
       session.beginTransaction();
       session.save(user1);
       session.getTransaction().commit();       
    }
}  

Can any one please tell me where I am wrong and what I should do? 

  [1]: https://i.stack.imgur.com/YbJxA.png
3

There are 3 best solutions below

0
Reimeus On

You need to enclose your entity attributes in your class tag definition in the mapping config file

<class catalog="user" name="com.enroll.UserDetail" table="users">
     <id column="id" name="id" type="java.lang.Integer">
        <generator class="assigned"/>
     </id>
    <property column="uname" name="uname" type="string"/>
    <property column="pword" name="pword" type="string"/>
</class>
5
Tinh Cao On
Your hibernate.hbm.xml should be

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping>
      <class catalog="user" name="com.enroll.UserDetail" table="users">
      <id column="id" name="id" type="java.lang.Integer">
        <generator class="assigned"/>
      </id>
      <property column="uname" name="uname" type="string"/>
       <property column="pword" name="pword" type="string"/>
</class>
    </hibernate-mapping>
0
Pham Thai Thinh On

Your hibernate config file wrong in this line:. Please remove it cause:

  1. It is not persistent unit with annotation
  2. com/enroll/insert if right must be com.enroll.insert Also java class should be begin with uppercase. You can check this post to do have quickstart hibernate