命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType
Named type [com.go_task.entity.User@5b4d25e7] did not implement BasicType nor UserType
我有 2 个模型。一个是用户模型,另一个是任务模型。我正在尝试获得“一对多”的双向关系。但即使在我尝试从 Test.java class 创建用户之前,我有一个例外。
命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType
我该如何解决这个问题?
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>
当我尝试 运行 Test.java class 时,出现以下异常:
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
用连接列名和引用列名添加 @JoinColumn
。
@Entity
@Table(name = "tasks")
public class Task implements Serializable {
// ...
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
// ...
}
我在 getTasks() 方法之前添加了 @OneToMany 注释,在 getUser() 方法之前添加了 @ManyToOne 注释。之后它解决了我的问题。它现在正在工作。但不清楚例外情况。
还有一点,我已经使用了 Hibernate Version 6 alpha!
我有 2 个模型。一个是用户模型,另一个是任务模型。我正在尝试获得“一对多”的双向关系。但即使在我尝试从 Test.java class 创建用户之前,我有一个例外。
命名类型 [com.go_task.entity.User@5b4d25e7] 没有实现 BasicType 和 UserType
我该如何解决这个问题?
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>
当我尝试 运行 Test.java class 时,出现以下异常:
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
用连接列名和引用列名添加 @JoinColumn
。
@Entity
@Table(name = "tasks")
public class Task implements Serializable {
// ...
@ManyToOne
@JoinColumn(name = "user_id", referencedColumnName = "id")
private User user;
// ...
}
我在 getTasks() 方法之前添加了 @OneToMany 注释,在 getUser() 方法之前添加了 @ManyToOne 注释。之后它解决了我的问题。它现在正在工作。但不清楚例外情况。
还有一点,我已经使用了 Hibernate Version 6 alpha!