0

Yes, I have seen this question all over the place,but am still not able to figure out what is the issue with my code. Certainly am overlooking something, can't just tell what. I could certainly use a second eye.

Here's my POJO.

package hibernate;

public class User {
    private int id;
    private String name;
    private int total;
    private int goal;

    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 int getTotal() {
        return total;
    }
    public void setTotal(int total) {
        this.total = total;
    }
    public int getGoal() {
        return goal;
    }
    public void setGoal(int goal) {
        this.goal = goal;
    }
}

Below is my 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 name="">
  <property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
  <property name="hibernate.connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=test;</property>
  <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="hibernate.connection.username">sa</property>
  <property name="hibernate.connection.password">admin</property>

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

Here's my User.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Jan 18, 2016 12:10:00 AM by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
    <class name="hibernate.User" table="USER">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" />
        </property>
        <property name="total" type="int">
            <column name="TOTAL" />
        </property>
        <property name="goal" type="int">
            <column name="GOAL" />
        </property>
    </class>
</hibernate-mapping>

Here's the main app putting the above together:

package hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class App {

    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();
        serviceRegistry = new StandardServiceRegistryBuilder()
                    .applySettings(configuration.getProperties()).build();

        sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

        Session session = sessionFactory.openSession();
        session.beginTransaction();

        User user = new User();     
        user.setName("Joe");
        user.setGoal(250);
        session.save(user);

        session.getTransaction().commit();
        session.close();    
        HibernateUtility.getSessionFactory().close();
    }
}

When i run the above, I get the exception below.

Jan 18, 2016 9:34:04 AM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect
Exception in thread "main" org.hibernate.MappingException: Unknown entity: hibernate.User
    at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:776)
    at org.hibernate.internal.SessionImpl.getEntityPersister(SessionImpl.java:1520)
    at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:100)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
    at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
    at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
    at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
    at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:679)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:671)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:666)
    at hibernate.App.main(App.java:27)

I have seen many people having this issues,but still havent been able to figure out mine. Thanks a bunch for your help.

Aliyu Fonyuy
  • 515
  • 1
  • 5
  • 13

2 Answers2

0

I think mistake is on this line:

<class name="hibernate.User" table="USER">

Change hibernate.User to User, it should help.

eg04lt3r
  • 2,467
  • 14
  • 19
  • Attribute `name` must be the fully qualified Java class name, if attribute `package` is not specified in the `hibernate-mapping` element. – v.ladynev Jan 18 '16 at 15:31
0

Your session factory configuration is incorrect for Hibernate 5. If you use Hibernate 5, you can create a session factory by this way

sessionFactory = new Configuration().configure().buildSessionFactory();

In the file User.hbm.xml you shouldn't use namespace

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"

Use

"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"

instead

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • Thanks a bunch v.ladynev. Creating sessionFactory as shown above did the trick. I will go ahead and change the namespace as well. Thanks a bunch. – Aliyu Fonyuy Jan 18 '16 at 17:01