javahibernatejpahibernate-onetomany

Named type [com.go_task.entity.User@5b4d25e7] did not implement BasicType nor UserType


I have 2 models. One is the User model and another is the Task model. I'm trying to obtain "one to many" bidirectional relations. But even before doing that when I'm trying to create a user from Test.java class. I'm having an exception.

Named type [com.go_task.entity.User@5b4d25e7] did not implement BasicType nor UserType

How can I fix this?

User.java

package com.go_task.entity;


import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Entity
@Table(name = "users")
public class User implements Serializable {

    @Id
    @GeneratedValue
    private int id;

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

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

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

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Task> tasks = new ArrayList<>();

    public User() {}

    public User(int id, String name, String email, String password) {
        this.id = id;
        this.name = name;
        this.email = email;
        this.password = password;
    }

    public User(String name, String email, String password) {
        this.name = name;
        this.email = email;
        this.password = password;
    }

    @Id
    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 String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public List<Task> getTasks() {
        return tasks;
    }

    public void setTasks(List<Task> tasks) {
        this.tasks = tasks;
    }

    public void addTask(Task task) {
        tasks.add(task);
        task.setUser(this);
    }

    public void removeTask(Task task) {
        tasks.remove(task);
        task.setUser(this);
    }
}

Task.java

package com.go_task.entity;


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


@Entity
@Table(name = "tasks")
public class Task implements Serializable {

    @Id
    @GeneratedValue
    private int id;

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

    @ManyToOne
    private User user;

    public Task() {}

    public Task(int id, String title) {
        this.id = id;
        this.title = title;
    }

    public Task(String title) {
        this.title = title;
    }

    @Id
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}

HibernateUtil.java class

package com.go_task.database;

import com.go_task.entity.Task;
import com.go_task.entity.User;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

public class HibernateUtil {

    private static SessionFactory sessionFactory = null;

    public static SessionFactory getSessionFactory() {
        if (sessionFactory == null) {
            try {
                Configuration configuration = new Configuration();
                configuration.configure("db/hibernate.cfg.xml");
                configuration.addAnnotatedClass(User.class).addAnnotatedClass(Task.class);

                ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
                        .applySettings(configuration.getProperties()).build();
                System.out.println("Hibernate Java Config serviceRegistry created");
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
                System.out.println(sessionFactory);
                return sessionFactory;

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sessionFactory;
    }
}

Test.java

package com.go_task.dao;

import com.go_task.database.HibernateUtil;
import com.go_task.entity.User;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class Test {

    public static void main(String[] args) {
        User user1 = new User("Name", "email", "pass");

        Transaction transaction = null;

        try (Session session = HibernateUtil.getSessionFactory().openSession()) {
            transaction = session.beginTransaction();
            session.save(user1);
            transaction.commit();
        } catch (Exception exception) {
            if (transaction != null) transaction.rollback();
            exception.getStackTrace();
        }
    }
}

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/otm_dm</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">false</property>
    </session-factory>
</hibernate-configuration>

When I'm trying to run Test.java class, I'm having the exception below:

java.lang.IllegalArgumentException: Named type [com.go_task.entity.User@5b4d25e7] did not implement BasicType nor UserType
    at org.hibernate.boot.model.TypeDefinition.createReusableResolution(TypeDefinition.java:213)
    at org.hibernate.boot.model.TypeDefinition.resolve(TypeDefinition.java:113)
    at org.hibernate.mapping.BasicValue.interpretExplicitlyNamedType(BasicValue.java:382)
    at org.hibernate.mapping.BasicValue.resolve(BasicValue.java:168)
    at org.hibernate.mapping.BasicValue.getType(BasicValue.java:155)
    at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:453)
    at org.hibernate.mapping.Property.isValid(Property.java:223)
    at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:624)
    at org.hibernate.mapping.RootClass.validate(RootClass.java:267)
    at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:353)
    at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:452)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:730)
    at com.go_task.database.HibernateUtil.getSessionFactory(HibernateUtil.java:29)
    at com.go_task.dao.Test.main(Test.java:15)

Process finished with exit code 0

Solution

  • I have added @OneToMany annotation before the getTasks() method and @ManyToOne annotation before getUser() method. After that it solved my problem. It's working now. But not clear about the exception though.

    One more thing, I have used Hibernate Version 6 alpha!