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>
2
  • Can you please confirm you are calling the hibernate.cfg.xml to configure your entity before you run your main program Commented Apr 10, 2016 at 0: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 Commented Apr 10, 2016 at 0:51

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is solved if it uses Hibernate version 4

Comments

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();

1 Comment

Using AnnotationConfiguration is valid only for Hibernate 3. This code will work with Configuration, but it is need to add every persistent class.
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...

3 Comments

You shouldn't use questions as the answers.
where is question in my answer??? I simply explain our situation, and how i solve this problem!!!
Okay, let it be an answer. There is not anything related to Spring in the original question by the way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.