【问题标题】:Unknown entity hibernate even if I declare mapping class即使我声明映射类,未知实体也会休眠
【发布时间】:2016-01-05 16:49:55
【问题描述】:

我尝试使用hibernate连接Mysql。我生成了一个支持 Intellij JPA 的实体,我收到了 Unknown entity: com.UsersEntity,即使我在 hibernate.cfg.xml 中声明了映射。

实体:

package com;
import javax.persistence.*;
@Entity
@Table(name = "users", schema = "", catalog = "protein_tracker")

public class UsersEntity {
    private int id;
    private String name;
    private int total;
    private int goal;

    @Id
    @Column(name = "id", nullable = false, insertable = true, updatable = true)
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Basic
    @Column(name = "name", nullable = false, insertable = true, updatable = true, length = 45)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "total", nullable = false, insertable = true, updatable = true)
    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    @Basic
    @Column(name = "goal", nullable = false, insertable = true, updatable = true)
    public int getGoal() {
        return goal;
    }

    public void setGoal(int goal) {
        this.goal = goal;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UsersEntity that = (UsersEntity) o;

        if (id != that.id) return false;
        if (total != that.total) return false;
        if (goal != that.goal) return false;
        if (name != null ? !name.equals(that.name) : that.name != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id;
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + total;
        result = 31 * result + goal;
        return result;
    }
}

配置文件:

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/protein_tracker</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.default_schema">protein_tracker</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">AmPiramide69</property>
        <mapping class="com.UsersEntity" />
        <mapping resource="com/UsersEntity.hbm.xml"/>
        <!-- DB schema will be updated if needed -->
        <!-- <property name="hbm2ddl.auto">update</property> -->
    </session-factory>
</hibernate-configuration>

会话工厂创建:

package com.simpleprogrammer;

import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtilities {
    private static SessionFactory sessionFactory;
    private static ServiceRegistry serviceRegistry;

    static {
        try {
            Configuration configuration = new Configuration().configure();

            serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        }
        catch (HibernateException exeption) {
            exeption.printStackTrace();
            System.out.println("Problem creating session factory!");
        }
    }

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

我该如何使用它:

package com.simpleprogrammer;

import com.UsersEntity;
import org.hibernate.Session;

public class Main {

    public static void main(String[] args) {
        Session session = HibernateUtilities.getSessionFactory().openSession();
        session.beginTransaction();

        UsersEntity usersEntity = new UsersEntity();

        usersEntity.setName("Name");
        usersEntity.setTotal(20);
        session.save(usersEntity);

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

你知道是什么问题吗?

【问题讨论】:

    标签: java mysql hibernate intellij-idea


    【解决方案1】:

    在构建 SessionFactory 时尝试使用 AnnotationConfiguration() 而不是 Configuration()。

    那么你只需要'mapping class=""'标签。 (不是你的休眠配置文件中的'mapping resource=""'标签)。

    【讨论】:

      猜你喜欢
      • 2017-01-22
      • 2019-12-03
      • 2020-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-09
      • 1970-01-01
      • 2014-04-23
      相关资源
      最近更新 更多