如何正确设置hibernate?

ffscu2ro  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(222)

我需要用一个数据库来配置hibernate,我是新手,不明白为什么会得到mappingexception:unknown entity:entity。logpassentity这里是我试图配置连接的方法

private static SessionFactory buildSessionFactoryLogin() {
    // Creating Configuration Instance & Passing Hibernate Configuration File
    Configuration configObj = new Configuration();
    configObj.configure("hibernate_log.cfg.xml");

    // Since Hibernate Version 4.x, ServiceRegistry Is Being Used
    ServiceRegistry serviceRegistryObj = new StandardServiceRegistryBuilder().applySettings(configObj.getProperties()).build();

    // Creating Hibernate SessionFactory Instance
    sessionFactoryLogin = configObj.buildSessionFactory(serviceRegistryObj);
    return sessionFactoryLogin;
}

下面是包含配置数据的xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- SQL Dialect -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

        <!-- Database Connection Settings -->
        <property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
        <property name="hibernate.connection.url">jdbc:postgresql://localhost:9000/postgres</property>
        <property name="hibernate.connection.username">postgres</property>
        <property name="hibernate.connection.password">lga852</property>
        <property name="show_sql">true</property>
        <property name="connection.autocommit">true</property>

        <!-- Specifying Session Context -->
        <property name="hibernate.current_session_context_class">org.hibernate.context.internal.ThreadLocalSessionContext</property>

        <!-- Mapping With Model Class Containing Annotations -->
        <mapping class="Entity.LogpassEntity" />
    </session-factory>
</hibernate-configuration>

下面是应该与数据库中的数据匹配的类

package Entity;

import javax.persistence.*;

@Entity
@Table(name = "logpass", schema = "public", catalog = "postgres")
public class LogpassEntity {
    @Id
    private String login;
    private String pass;

    @Basic
    @Column(name = "login")
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Basic
    @Column(name = "pass")
    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

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

        LogpassEntity that = (LogpassEntity) o;

        if (login != null ? !login.equals(that.login) : that.login != null) return false;
        if (pass != null ? !pass.equals(that.pass) : that.pass != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = login != null ? login.hashCode() : 0;
        result = 31 * result + (pass != null ? pass.hashCode() : 0);
        return result;
    }
}

我读到你只能用注解来配置配置,但是我没有找到任何具体的例子,我可以看到连接到数据库的属性,有人能解释问题是什么吗?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题