не получается внести данный из таблицы с помощью hibernate
я в hibernateRunner пытаюсь ввести данный в таблицу:
import java.time.LocalDate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.*;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import package1.*;
public class HibernateRunner{
public static void main(String[] args) {
Configuration configuration = new Configuration();
// configuration.setPhysicalNamingStrategy(new CamelCaseToUnderscoresNamingStrategy());
configuration.registerTypeOverride(new JsonBinaryType());
configuration.addAttributeConverter(new BirthdayConverter());
configuration.addAnnotatedClass(User.class);
configuration.configure();
try ( SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession()){
session.beginTransaction();
User user = User.builder()
.username("[email protected]")
.firstname("stroka")
.lastname("Magomedov")
.birthDate(new Birthday(LocalDate.of(2010, 11, 20)))
.role(Role.ADMIN)
.build();
//session.saveOrUpdate(user);
//session.delete(user);
session.save(user);
session.getTransaction().commit();
}
}
}
мой entity-class:
import java.time.LocalDate;
import jakarta.persistence.*;
import lombok.*;
import package1.Birthday;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "users")
public class User {
@Id
private String username;
private String firstname;
private String lastname;
@Column(name = "birth_date")
private Birthday birthDate;
@Enumerated(EnumType.STRING)
private Role role;
}
Role:
public enum Role {
USER, ADMIN
}
Birthday:
package package1;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public record Birthday(LocalDate birthDate) {
public long getAge() {
return ChronoUnit.YEARS.between(birthDate, LocalDate.now());
}
}
BirthdayConverter:
package package1;
import java.util.Optional;
import java.sql.Date;
import package1.Birthday;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;
@Converter(autoApply = true)
public class BirthdayConverter implements AttributeConverter<Birthday, Date>{
@Override
public Birthday convertToEntityAttribute(Date dbData) {
return Optional.ofNullable(dbData)
.map(Date::toLocalDate)
.map(Birthday::new)
.orElse(null);
}
@Override
public Date convertToDatabaseColumn(Birthday attribute) {
return Optional.ofNullable(attribute)
.map(Birthday::birthDate)
.map(Date::valueOf)
.orElse(null);
}
}
я спросил у gpt и она говорит что проблема в pom.xml:
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.crunchify.tutorials</groupId>
<artifactId>CrunchifySpringMVCFramework</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CrunchifySpringMVC</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.7.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.32</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.13</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.21.1</version>
</dependency>
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>6.4.4.Final</version>
<type>pom</type>
</dependency>
А сама ошибка вылезает при компиляции:
апр. 23, 2024 9:13:01 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate ORM core version 6.4.4.Final
Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/type/descriptor/java/JavaTypeDescriptor
at HibernateRunner.main(HibernateRunner.java:15)
Caused by: java.lang.ClassNotFoundException: org.hibernate.type.descriptor.java.JavaTypeDescriptor
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:526)
... 1 more