命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType

Posted

技术标签:

【中文标题】命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType【英文标题】:Named type [com.go_task.entity.User@5b4d25e7] did not implement BasicType nor UserType 【发布时间】:2020-09-25 14:45:29 【问题描述】:

我有 2 个模型。一个是用户模型,另一个是任务模型。我正在尝试获得“一对多”的双向关系。但即使在我尝试从 Test.java 类创建用户之前这样做。我遇到了一个例外。

我该如何解决这个问题?

用户.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 类

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>

当我尝试运行 Test.java 类时,出现以下异常:

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

【问题讨论】:

【参考方案1】:

添加@JoinColumn 与连接列名和引用列名。

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

    // ...

    @ManyToOne
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    private User user;
    
    // ...

【讨论】:

我在答案中犯了一些错误,现在更新了。在您的情况下,@Id 注释已被复制,变量和吸气剂都有。删除一个,然后重试。我在本地试了一下,效果很好。【参考方案2】:

我在getTasks() 方法之前添加了@OneToMany 注释,在getUser() 方法之前添加了@ManyToOne 注释。之后它解决了我的问题。它现在正在工作。但不清楚异常。

还有一件事,我使用的是 Hibernate Version 6 alpha!

【讨论】:

以上是关于命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType的主要内容,如果未能解决你的问题,请参考以下文章

命名类型和未命名类型和基础类型

命名和未命名类型

为啥要将类型放在未命名的命名空间中?

命名空间“System.Configuration”中不存在类型或命名空间名称“ConfigurationManager”

命名空间“System.Configuration”中不存在类型或命名空间名称“ConfigurationMannger”,求帮助,谢谢

“Model”是“命名空间”,但此处被当做“类型”来使用,是啥原因导致的哦,这段代码我写