0

i have a problem and i need your help.i use eclipse 4.5.1. hibernate5.1 and here is the code

Exception in thread "main" org.hibernate.MappingException: Unknown entity: net.runze.hb1.entity.News

public class NewsManager {
 public static void main(String[] args) throws Exception {
  Configuration conf = new Configuration()
    .configure();
  ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
    .applySettings(conf.getProperties()).build();

  SessionFactory sf = conf.buildSessionFactory(serviceRegistry);

  Session sess = sf.openSession();
  Transaction ta = sess.beginTransaction();

  News n = new News();
  n.setTitle("Aliens");
  n.setContent("hahahaha!");

  sess.save(n);  // here is where it threw exception

  ta.commit();
  sess.close();
  sf.close();
 }
}

and i use annotation to the entity class:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="news_inf")
public class News {
 @Id
 @GeneratedValue(strategy=GenerationType.IDENTITY)
 private Integer id;
 private String title;
 private String content;
    // getters and setters...
}

here 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>
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">
   jdbc:mysql://localhost:3306/hibernate?useUnicode=true&amp;characterEncoding=UTF-8</property>
  <property name="connection.username">root</property>
  <property name="connection.password">123456</property>
  <property name="hibernate.c3p0.max_size">20</property>
  <property name="hibernate.c3p0.min_size">1</property>
  <property name="hibernate.c3p0.timeout">5000</property>
  <property name="hibernate.c3p0.max_statements">100</property>
  <property name="hibernate.c3p0.idle_test_period">3000</property>
  <property name="hibernate.c3p0.aquire_increment">2</property>
  <property name="hibernate.c3p0.validate">true</property>
  <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
  <property name="hbm2ddl.auto">update</property>
  <property name="show_sql">true</property>
  <property name="hibernate.format_sql">true</property>
  <property name="hibernate.temp.use_jdbc_metadata_defaults">false</property>
  

  <mapping class="net.runze.hb1.entity.News" />  
 </session-factory>
</hibernate-configuration>

they says this problem will happen when my @Entity import the wrong package,or the hibernate.cfg.xml forgot the "mapping". both them i made it right but it still don't work.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29

2 Answers2

1

It is simply :)

Configuration conf = new Configuration()
                .configure();
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                .applySettings(conf.getProperties()).build();

        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);

Change to

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

Hibernate 5 :- org.hibernate.MappingException: Unknown entity

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • wow! it work now. really amazing!Thank you! The book i'm reading now use hibernate 4.35 but i use 5.1 so so many differences really confused me!Thank you – T4Technologic Jun 06 '16 at 10:37
  • @T4Technologic You are welcome. Consider to accept the answer, if you think it is valid. – v.ladynev Jun 06 '16 at 11:52
0

you have to specify where your configuration file is:

onfiguration conf = new Configuration().configure("[your package]/hibernate.cfg.xml");
Mohamed Nabli
  • 1,629
  • 3
  • 17
  • 24