1

I got an exception when I use hibernate in Maven. The hibernate version is 5.1.0.Final. The exception is: enter image description here Here is my project structure: enter image description here

Here is my Entity class ABC:

package com;
import javax.persistence.*;
@Entity

@Table(name = "abc_inf")

public class ABC {

    @Id@GeneratedValue
    private Integer id;

    private String name;

    public ABC() {
    }

    setters and getters omitted 

}

Here is my main class:

package com;

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 Main {
    public static void main(String[] args) {
        Configuration conf = new Configuration().configure();
        ServiceRegistry sr = new             StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
        SessionFactory sf = conf.buildSessionFactory(sr);
        Session session = sf.openSession();

        ABC abc = new ABC();
        abc.setName("abc");

        session.save(abc);
        session.flush();
        session.close();
        sf.close();
    }
}

Here is my hibernate.cfg.xml:

<hibernate-configuration>
  <session-factory>
    mysql connection and properties settings omitted
    <mapping class="com.ABC"/>
  </session-factory>
</hibernate-configuration>
user2716189
  • 63
  • 1
  • 6
  • Can you please confirm you are calling the hibernate.cfg.xml to configure your entity before you run your main program – LearningPhase Apr 10 '16 at 00:14
  • it uses the hibernate.cfg.xml, otherwise it would throw some connection exception as it could not connect to the database in the first place. I also printed the properties from Configuration, it is identical to the ones defined in hibernate.cfg.xml – user2716189 Apr 10 '16 at 00:51

4 Answers4

1

It is a Hibernate 5 configuration problem. You can't use this code to build a session factory

Configuration conf = new Configuration().configure();
ServiceRegistry sr = new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
SessionFactory sf = conf.buildSessionFactory(sr);

Use this instead

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

Refer for additional notes

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

Community
  • 1
  • 1
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
0

The problem is solved if it uses Hibernate version 4

user2716189
  • 63
  • 1
  • 6
0

You need to add the annotated classes to the hibernate config. Use the following code

AnnotationConfiguration conf = new AnnotationConfiguration();
conf.addAnnotatedClass(ABC.class);
SessionFactry sf = conf.configure(hbmFile).buildSessionFactory();
KayV
  • 12,987
  • 11
  • 98
  • 148
  • Using `AnnotationConfiguration` is valid only for Hibernate 3. This code will work with `Configuration`, but it is need to add every persistent class. – v.ladynev Apr 11 '16 at 09:51
0

As for my case i'm use LocalSessionFactoryBean and setPackagesToScan, and services, writed for audit entities work fine.

But!!!

In our project we have table EntityInfo with column entityname, for example,

entytyname = bel.rdigital.cashbox.models.cbmain.merchantcore.versions.CBVersions

and this persist in db. Then I refactored entity CBVersions and transfer it to package

bel.rdigital.cashbox.models.cbmain.versions.cashbox.CBVersions,

but in db stayed old package, and than, when I using method CrossTypeRevisionChangesReader.findEntitiesGroupByRevisionType(revision),

I get exception Unknown entity: bel.rdigital.cashbox.models.cbmain.merchantcore.versions.CBVersions

So, this exaption means, that hiberante not find entity... Problem may be in package scanning or in location entity...