本文介绍了休眠错误-org.hibernate.MappingException:未知实体:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们!我在休眠5中有一个非常奇怪的问题.我在线程主" org.hibernate.MappingException中收到异常:未知实体:com.rosko.Merchandiser 当我删除configuration.addAnnotatedClass(com.rosko.Merchandiser.class)

guys ! i have very strange problem with hibernate 5.I receive Exception in thread "main" org.hibernate.MappingException: Unknown entity: com.rosko.Merchandiser When i remove configuration.addAnnotatedClass(com.rosko.Merchandiser.class)

HibernateUtil.java

public class HibernateUtil {

private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
    try {
        Configuration configuration = new Configuration();
        configuration.configure();
        /* **With this line, everyting is working..** configuration.addAnnotatedClass(com.rosko.Merchandiser.class);*/
        return configuration.buildSessionFactory(
                new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build());
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException("There was an error building the factory");
    }
}

public static SessionFactory getSessionFactory() {
    return sessionFactory;
}}

Application.java

公共类申请{

public static void main(String[] args) {


    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();

    Merchandiser merchant = new Merchandiser();

    merchant.setAddress("Viena");
    merchant.setName("sadkada");

    session.save(merchant);


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

hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
    <!-- Database connection settings -->
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/finance?serverTimezone=UTC</property>
    <property name="connection.username">root</property>
    <property name="connection.password">rozeto90</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>


      <mapping class="com.rosko.Merchandiser" />

</session-factory>

Merchandiser.java

  @Entity
@Table(name="merchandiser")
public class Merchandiser {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name="name")
private String name;

@Column(name= "address")
private String address;

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 String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}


}

我可以仅将XML文件用于映射类吗?

Can i use the xml file only for mapping class ?

最诚挚的问候.

推荐答案

确保hibernate.cfg.xml的文件名和路径正确.

make sure that file name and path of hibernate.cfg.xml are proper.

path = src/main/resources/hibernate.cfg.xml

也尝试使用以下HibernateUtil类:

Also try to use below HibernateUtil class :

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        Configuration configuration = new Configuration();
        configuration.configure();
        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
                .applySettings(configuration.getProperties())
                .buildServiceRegistry();
        return configuration.buildSessionFactory(serviceRegistry);
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static void shutdown() {
        // Close caches and connection pools
        getSessionFactory().close();
    }
}

这篇关于休眠错误-org.hibernate.MappingException:未知实体:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:27