java - 即使我声明了映射类,未知实体也会 hibernate

标签 java mysql hibernate intellij-idea

我尝试使用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>

session 工厂创建:

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

你知道问题出在哪里吗?

最佳答案

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

然后你只需要 'mapping class=""' 标签。 (不是你的 hibernate 配置文件中的 'mapping resource=""' 标签)。

关于java - 即使我声明了映射类,未知实体也会 hibernate ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34616973/

相关文章:

java - CursorAdapter 检索数据时无提示地失败

php - 我的命令 UPDATE 在框架 FaaPz/Slim-PDO 上不起作用

MySQL - 从另一个表中获取不匹配的项目

mysql - 本地化现有数据库

java - 使用 Hikari CP 检测到明显的连接泄漏

java - hibernate 和 JPA : how to make a foreign key constraint on a String

java - 点击按钮后隐藏java中的框架

java - 从 Maven 设置 TestNG 的详细级别

java - 循环中的变量对象属性

java - oracle模式下h2数据库jar升级后select语句失败