Hibernate -无法使用XML配置持久化实体

4zcjmb1e  于 2023-04-06  发布在  其他
关注(0)|答案(1)|浏览(115)

我尝试使用Hibernate和XML配置持久化一个实体,但我无法这样做。错误消息是...

Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate persister: entity.Users
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:735)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:716)
    at app.main(app.java:12)

下面是我的代码:
app.java:

import entity.User;
import org.hibernate.Session;

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

        User user = new User();
        user.setName("Joe");
        user.setGoal((short) 250);
        session.persist(user);

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

HibernateUtil.java:

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

public class HibernateUtilities {
    private static SessionFactory sessionFactory;
    private static StandardServiceRegistryBuilder builder;

    static {
        try {
            Configuration configuration = new Configuration().configure();
            builder = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
            sessionFactory = configuration.buildSessionFactory(builder.build());
        } catch (HibernateException e) {
            System.out.println("Problem creating session factory!");
        }
    }

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

Users.java:

package entity;

public class User {
    private int id;
    private String name;
    private int total;
    private short goal;

    public User() {
        // no-parameter constructor
    }

    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 int getTotal() {
        return total;
    }

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

    public short getGoal() {
        return goal;
    }

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

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

        User user = (User) o;

        if (id != user.id) return false;
        if (total != user.total) return false;
        if (goal != user.goal) return false;
        if (name != null ? !name.equals(user.name) : user.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 + (int) goal;
        return result;
    }
}

hibernate.cfg.xml:

<?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="connection.url">jdbc:oracle:thin:@localhost:1521/orclpdb</property>
    <property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
      <property name="connection.username">PROTRACKER</property>
      <property name="connection.password">123</property>
      <mapping resource="Users.hbm.xml"/>

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hibernate.hbm2ddl.auto">update</property> -->
  </session-factory>
</hibernate-configuration>

Users.hbm.xml:

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

    <class name="entity.User" table="USERS" schema="PROTRACKER">
        <id name="id">
            <column name="ID" sql-type="number(9)" precision="9"/>
        </id>
        <property name="name">
            <column name="NAME" sql-type="varchar2(100)" length="100"/>
        </property>
        <property name="total">
            <column name="TOTAL" sql-type="number(6)" precision="6"/>
        </property>
        <property name="goal">
            <column name="GOAL" sql-type="number(4)" precision="4"/>
        </property>
    </class>
</hibernate-mapping>

完整的错误日志...我正在使用IntelliJ:

"C:\Program Files\Java\jdk-11.0.10\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2\lib\idea_rt.jar=63088:C:\Program Files\JetBrains\IntelliJ IDEA 2022.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\mumin\IdeaProjects\Hibernate\_2_Hibernate_Demo\target\classes;C:\Users\mumin\.m2\repository\org\hibernate\orm\hibernate-core\6.0.0.Final\hibernate-core-6.0.0.Final.jar;C:\Users\mumin\.m2\repository\jakarta\persistence\jakarta.persistence-api\3.0.0\jakarta.persistence-api-3.0.0.jar;C:\Users\mumin\.m2\repository\jakarta\transaction\jakarta.transaction-api\2.0.0\jakarta.transaction-api-2.0.0.jar;C:\Users\mumin\.m2\repository\org\jboss\logging\jboss-logging\3.4.3.Final\jboss-logging-3.4.3.Final.jar;C:\Users\mumin\.m2\repository\org\jboss\jandex\2.4.2.Final\jandex-2.4.2.Final.jar;C:\Users\mumin\.m2\repository\com\fasterxml\classmate\1.5.1\classmate-1.5.1.jar;C:\Users\mumin\.m2\repository\org\hibernate\common\hibernate-commons-annotations\6.0.0.CR1\hibernate-commons-annotations-6.0.0.CR1.jar;C:\Users\mumin\.m2\repository\net\bytebuddy\byte-buddy\1.12.8\byte-buddy-1.12.8.jar;C:\Users\mumin\.m2\repository\jakarta\activation\jakarta.activation-api\2.0.1\jakarta.activation-api-2.0.1.jar;C:\Users\mumin\.m2\repository\jakarta\xml\bind\jakarta.xml.bind-api\3.0.1\jakarta.xml.bind-api-3.0.1.jar;C:\Users\mumin\.m2\repository\jakarta\inject\jakarta.inject-api\2.0.0\jakarta.inject-api-2.0.0.jar;C:\Users\mumin\.m2\repository\org\antlr\antlr4-runtime\4.9.1\antlr4-runtime-4.9.1.jar;C:\Users\mumin\.m2\repository\org\glassfish\jaxb\jaxb-runtime\3.0.2\jaxb-runtime-3.0.2.jar;C:\Users\mumin\.m2\repository\com\sun\activation\jakarta.activation\2.0.1\jakarta.activation-2.0.1.jar;C:\Users\mumin\.m2\repository\org\glassfish\jaxb\jaxb-core\3.0.2\jaxb-core-3.0.2.jar;C:\Users\mumin\.m2\repository\org\glassfish\jaxb\txw2\3.0.2\txw2-3.0.2.jar;C:\Users\mumin\.m2\repository\com\sun\istack\istack-commons-runtime\4.0.1\istack-commons-runtime-4.0.1.jar;C:\Users\mumin\.m2\repository\com\oracle\database\jdbc\ojdbc8\21.9.0.0\ojdbc8-21.9.0.0.jar app
Mar 06, 2023 11:47:59 AM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.0.0.Final
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using built-in connection pool (not intended for production use)
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: Loaded JDBC driver class: oracle.jdbc.OracleDriver
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001012: Connecting with JDBC URL [jdbc:oracle:thin:@localhost:1521/orclpdb]
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {password=****, user=PROTRACKER}
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl$PooledConnections <init>
INFO: HHH10001115: Connection pool size: 20 (min=1)
Mar 06, 2023 11:48:01 AM org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl logSelectedDialect
INFO: HHH000400: Using dialect: org.hibernate.dialect.OracleDialect
Mar 06, 2023 11:48:01 AM org.hibernate.engine.transaction.jta.platform.internal.JtaPlatformInitiator initiateService
INFO: HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate persister: entity.Users
    at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:735)
    at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:716)
    at app.main(app.java:12)

Process finished with exit code 1

我已经尝试了我能做的一切...提前感谢您的时间和支持。

jogvjijk

jogvjijk1#

添加
configuration.addAnnotatedClass(User.class);
在类-“Hibernate实用程序”

相关问题