开发者

Hibernate Unknown Entity

I have two jar files with hibernate classes mapped. One jar f开发者_如何转开发ile is perfectly working and for the next jar file it is not mapped. I get Unknown Entity exception. Persistence.xml is good but i dont know why this is happening. Any guess what mite be the issue???


Check if you have @Entity on your Equipment class.

This annotation (@Entity) is marking a class as Hibernate (JPA) entity.


For me it was a refactoring issue. I refactored my entity package but forgot to change the component scan path to the new package name. After that was realized and changed, it worked!


Try adding following Annotation on top of your Entity Class Equipment. Change the table name accordingly if required :

@Entity
@Table(name = "Equipment")
@XmlRootElement


package Test;

import java.util.EnumSet;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.Metadata;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.hibernate.tool.schema.TargetType;
import org.junit.Test;

import Entity.Users;

public class TestMain {
    public static void main(String[] arg){
         try {
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        SessionFactory sessionFactory = new MetadataSources( serviceRegistry ).buildMetadata().buildSessionFactory();
        Session session=sessionFactory.openSession();
        Transaction transaction=session.beginTransaction();
        Users users=new Users();
        users.setId(2);
        session.save(users);
        transaction.commit();
        session.close();
        sessionFactory.close();
         }catch(Throwable th){
                System.err.println("Init SessionFactory creation failed" );
                System.err.println(th);
                throw new ExceptionInInitializerError(th);
         }finally {

        }
    }


}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜