views:

42

answers:

1

I'm using netbeans 6.9 and Hibernate and I'm trying to insert some values in a database (postgres) but I get the following exception:

INFO: Not binding factory to JNDI, no JNDI name configured
Exception in thread "main" org.hibernate.MappingException: Unknown entity: SELECTPACK.beanClass
        at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:550)
        at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1338)
        at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
        at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
        at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:3

and my hbm.xml file is given below

<class name="SELECTPACK.beanClass" table="login">
  <id column="id" name="id">
      <generator class="increment"/>
  </id>
  <property name="username" column="username"/>
  <property name="password" column="password"/>
</class>

my cfg.xml file is given below

<?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"&gt;
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://192.168.1.100:54321/postgres</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">dbserver</property>
        <property name="hibernate.show_sql">true</property>
        <mapping resource="SELECTPACK/select.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

my pojo class

package SELECTPACK;

public class beanClass {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

and hear is my main class

package SELECTPACK;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class selectMainClass {

    public static void main(String... args) {
        SessionFactory sessionFact = new Configuration().configure().buildSessionFactory();
        Session ses = sessionFact.openSession();
        Transaction tx = ses.beginTransaction();
        beanClass bean = new beanClass();
        bean.setPassword("hi....");
        bean.setUsername("hi....");
        ses.save(bean);
        tx.commit();
        /*String query = "select * from login";
        Query qry = ses.createQuery(query);
        ArrayList list = (ArrayList) qry.list();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }*/
        ses.close();
    }
}

I don't understand what the problem is.

+1  A: 

The entity SELECTPACK.beanClass is not recognized as a valid entity and there should be a previous stacktrace in the log (when creating the SessionFactory) showing the real error. Look for a previous trace. But possible causes include:

  • the table doesn't exist
  • one of the mapped column is not there

Apart from that, I have a few minor remarks (that are not the problem):

  • traditionally, packages are in lower case (i.e. selectpack);
  • the Java class name should start with an upper case letter (i.e. BeanClass);
  • it is recommended to map a class selectpack.BeanClass in a selectpack/BeanClass.hbm.xml mapping file (to ease the maintenance);
  • my suggestion would be to use the native generator in the mapping file.

Something like this for the class:

package selectpack;

public class BeanClass {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

And for the mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
<hibernate-mapping>
    <class name="selectpack.BeanClass" table="login">
      <id column="id" name="id">
          <generator class="native"/>
      </id>
      <property name="username" column="username"/>
      <property name="password" column="password"/>
    </class>
</hibernate-mapping>

Of course, update the hibernate.cfg.xml accordingly:

   <mapping resource="selectpack/BeanClass.hbm.xml"/>

But as I said, these remarks are not directly related to the problem.

Pascal Thivent
@Pascal Thivent Good(+1) Hi Pascal, if possible, vote for this issue http://opensource.atlassian.com/projects/hibernate/browse/BVAL-208 @Valid should support groups attribute Take a look. Thank you!
Arthur Ronald F D Garcia
@Arthur Hello Arthur. Done. And thanks!
Pascal Thivent
Thanks for ur reply........
Mouli
thanks for ur reply...give me some example using hibernate...
Mouli