I have the following error:
Exception in thread "main" java.lang.IllegalArgumentException: Unable to locate persister: org.hibernate.tutorial.annotations.Persona.
I was looking at the documentation but I can't find a way to solve it and it's hard for me to orient myself in it. I am using the latest version of Hibernate 6.0.0.Final which is giving me some headaches. I had to replace the method .save()
with .persistence()
because the first one was deprecated and also change the way I initialize Hibernate. Being a beginner, I don't know if I made any mistakes in these changes I made reading the documentation.
Main class:
package org.hibernate.tutorial.annotations;
import java.util.Date;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
public class AnnotationsIllustrationTest {
public static void main(String[] args) {
SessionFactory sessionFactory;
// A SessionFactory is set up once for an application!
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
.configure() // configures settings from hibernate.cfg.xml
.build();
sessionFactory = new MetadataSources( registry ).buildMetadata().buildSessionFactory();
Session session = sessionFactory.openSession();
try {
Persona persona1 = new Persona ("a","b","c",54);
session.beginTransaction();
session.persist(persona1);
session.getTransaction().commit();
System.out.println("registro insertado");
} finally {
sessionFactory.close();
}
}
}
Entity
package org.hibernate.tutorial.annotations;
import org.hibernate.annotations.Table;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity (name ="persona")
@Table(appliesTo = "persona" )
public class Persona {
@Id
@Column(name = "id_persona")
int id;
@Column(name = "nombre")
String nombre;
@Column(name = "apellido")
String apellido;
@Column(name = "mail")
String mail;
@Column(name = "telefono")
int telefono;
public Persona(String nombre, String apellido, String mail, int telefono) {
super();
this.nombre = nombre;
this.apellido = apellido;
this.mail = mail;
this.telefono = telefono;
}
public Persona() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApellido() {
return apellido;
}
public void setApellido(String apellido) {
this.apellido = apellido;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public int getTelefono() {
return telefono;
}
public void setTelefono(int telefono) {
this.telefono = telefono;
}
@Override
public String toString() {
return "Persona [id=" + id + ", nombre=" + nombre + ", apellido=" + apellido + ", mail=" + mail + ", telefono="
+ telefono + "]";
}
}
Setting
<?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>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/test</property>
<property name="connection.username">root</property>
<property name="connection.password">admin</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="hibernate.show_sql">true</property>
</session-factory>
</hibernate-configuration>