java.lang.非法参数异常:无法找到persister(Hibernate 6.0.0,No-xml)

xyhw6mcr  于 2023-03-28  发布在  Java
关注(0)|答案(2)|浏览(135)

我在这个网站上看过fimiliar问题。但是没有什么对我有用。我试着在没有xml的情况下配置Hibernate。我想创建一个表 * animal * 并持久化一条记录。

    • 堆栈跟踪异常**
java.lang.IllegalArgumentException: Unable to locate persister: gameCenter.Animal
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:735)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:716)
at gameCenter.Test.saveAnimal(Test.java:50)
at gameCenter.Test.main(Test.java:21)
    • 测试类**
package gameCenter;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import java.util.Properties;

public class Test {
    public static void main(String[] args) {

    Animal animal = new Animal("Dog");

    saveAnimal(animal);
}

public static SessionFactory getSessionFactory() {
    Properties settings = new Properties();
    settings.put(Environment.DRIVER, "org.postgresql.Driver");
    settings.put(Environment.URL, "jdbc:postgresql://localhost:5432/ineffable");
    settings.put(Environment.USER, "postgres");
    settings.put(Environment.PASS, "ewqe3121");
    settings.put(Environment.DIALECT, "org.hibernate.dialect.PostgreSQLDialect");
    settings.put(Environment.SHOW_SQL, "true");
    settings.put(Environment.HBM2DDL_AUTO, "create");

    Configuration configuration = new Configuration();
    configuration.setProperties(settings);
    configuration.addAnnotatedClass(Animal.class);

    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
            .applySettings(configuration.getProperties()).build();

    return configuration.buildSessionFactory(serviceRegistry);
}

public static void saveAnimal(Animal animal) {
    Transaction transaction = null;

    try(Session session = getSessionFactory().openSession()) {

        transaction = session.beginTransaction();

            session.persist(animal);
            transaction.commit();

        } catch (Exception e) {
            System.out.println("Message: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
    • Animal类(实体)**
package gameCenter;

import javax.persistence.*;
import java.io.Serializable;

@Entity
@Table(name = "animal", schema = "public")
class Animal implements Serializable {

    @Id
    @GeneratedValue(strategy =  GenerationType.IDENTITY)
    private int id;

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

    public Animal(String one) {
        this.one = one;
    }

    public int getId() {
        return id;
    }

    public String getOne() {
        return one;
    }

    public void setOne(String one) {
        this.one = one;
    }

}

    • 依赖关系**
<dependencies>

    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>42.3.3</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>javax.persistence</groupId>
        <artifactId>persistence-api</artifactId>
        <version>1.0.2</version>
    </dependency>
</dependencies>

Postgres版本为14....................................................................................................................................................................................................................................................

njthzxwz

njthzxwz1#

我和你有同样的问题...
我不知道为什么,但我需要使用实体的默认构造函数,然后用它的访问器赋值。

HyltCommand c = new HyltCommand();
    c.setCommand("This is a test command");

试试这个,告诉我它是否有效:

Animal animal = new Animal();
animal.setType("Dog");
fumotvh3

fumotvh32#

您必须将软件包从javax.persistance.*更改为jakarta.persitance.*

相关问题