ошибка в аннотации @Type
я создал в таблице поле типа jsonb и у меня выводит ошибку в entity-классе:
import java.time.LocalDate;
import org.hibernate.annotations.Type;
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;
@Type(type = "jsonb")
@Column(columnDefinition = "jsonb")
private String info;
@Column(name = "birth_date")
private Birthday birthDate;
@Enumerated(EnumType.STRING)
private Role role;
}
мой main класс:
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")
.info("""
"name": "Ivan",
"id": 25
""")
.birthDate(new Birthday(LocalDate.of(2010, 11, 20))) .role(Role.ADMIN) .build();
session.delete(user);
session.get(User.class, user);
session.getTransaction().commit();
}
}
}
Role: public enum Role { USER, ADMIN }
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);
}
}
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());
}
}
JsonType:
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
public class JsonType implements UserType{
@Override
public int getSqlType() {
return 0;
}
@Override
public Class returnedClass() {
return null;
}
@Override
public boolean equals(Object x, Object y) {
return false;
}
@Override
public int hashCode(Object x) {
return 0;
}
@Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner)
throws SQLException {
return null;
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
throws SQLException {
}
@Override
public Object deepCopy(Object value) {
return null;
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Serializable disassemble(Object value) {
return null;
}
@Override
public Object assemble(Serializable cached, Object owner) {
return null;
}
}
А сами ошибки:
1.Description Resource Path Location Type
The annotation @Type must define the attribute value User.java /CrunchifySpringMVCFramework/src/main/java line 25 Java Problem
2.Description Resource Path Location Type
The attribute type is undefined for the annotation type Type User.java /CrunchifySpringMVCFramework/src/main/java line 25 Java Problem