简体   繁体   English

Hibernate 连接提取问题“无法连接到基本类型的属性”

[英]Hibernate problems with join fetch “Cannot join to attribute of basic type”

I'm having problems with loading entities with a one-to-one relationship from a sqlite database.我在从 sqlite 数据库加载具有一对一关系的实体时遇到问题。 When I use a plain CriteriaQuery to load them everything works fine.当我使用普通的 CriteriaQuery 加载它们时,一切正常。 But I've read somwhere that for the performance it's better to join the two tables the data is coming from in the query so hibernate won't make 2 queries out of it.但是我在某处读到,为了提高性能,最好将数据来自查询的两个表连接起来,这样 hibernate 就不会从中进行 2 个查询。

I am getting a BasicPathUsageException: Cannot join to attribute of basic type when I am trying to create a Fetch thingy.我收到BasicPathUsageException: Cannot join to attribute of basic type当我尝试创建 Fetch 事物时。

This is the method I use to create the query:这是我用来创建查询的方法:

    private List<Task> loadTasks() {
        
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            
            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<Task> taskQuery = builder.createQuery(Task.class);
            Root<Task> taskTable = taskQuery.from(Task.class);

            Fetch<Task, TaskType> fetch = taskTable.fetch(TaskType_.ID, JoinType.LEFT);    //<- exception is pointing here
            taskQuery.where(builder.equal(taskTable.get(TaskType_.ID).get("status"), "RECEIVED"));
            
            List<Task> loadedTasks= session.createQuery(taskQuery).getResultList();
            session.getTransaction().commit();
            
            return loadedTasks;
            
        } catch (Exception e) {
            
            e.printStackTrace();
            return null;
        }
    }

This is the TaskType class:这是任务类型 class:

@Entity(name = "TaskType")
@Table(name = "task_types")
public class TaskType implements Serializable {
    
    private final SimpleIntegerProperty id = new SimpleIntegerProperty();
    private final SimpleStringProperty name = new SimpleStringProperty();
    
    @Id
    @Column(name = "task_type_id", unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id.get();
    }
    
    public void setId(int id) {
        this.id.set(id);
    }
    
    @Column(name = "task_type_name", unique = true)
    public String getName() {
        return name.get();
    }
    
    public void setName(String name) {
        this.name.set(name);
    }
    
    public SimpleIntegerProperty idProperty() {
        return id;
    }
    
    public SimpleStringProperty nameProperty() {
        return name;
    }

And this is the Task class which contains a task type object:这是包含任务类型 object 的任务 class:

@Entity(name = "Task")
@Table(name = "tasks")
public class Task implements Serializable {
    
    private final SimpleIntegerProperty id = new SimpleIntegerProperty();
    private final SimpleStringProperty name = new SimpleStringProperty();
    private TaskType type;
    
    @Id
    @Column(name = "task_id", unique = true)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id.get();
    }
    
    public void setId(int id) {
        this.id.set(id);
    }
    
    @Column(name = "task_name", unique = true)
    public String getName() {
        return name.get();
    }
    
    public void setName(String name) {
        this.name.set(name);
    }
    
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_type_id")
    public TaskType getType() {
        return type;
    }
    
    public void setType(TaskType type) {
        this.type = type;
    }
    
    public SimpleStringProperty nameProperty() {
        return name;
    }
    
    public SimpleIntegerProperty idProperty() {
        return id;
    }

How is this fetch() supposed to be used or how can I get the code to load all tasks including their task types?这个 fetch() 应该如何使用,或者我怎样才能让代码加载所有任务,包括它们的任务类型? Am I using some annotation wrong?我是否使用了错误的注释?

I tried to look the exception up but I could not find any solution or understand how to apply it to my situation.我试图查找异常,但找不到任何解决方案或了解如何将其应用于我的情况。

Thanks for any help!谢谢你的帮助!

The fetch method, just like the join methods, only work on associations. fetch方法,就像join方法一样,只对关联起作用。

You use the following instead您改用以下内容

taskTable.fetch(Task_.TYPE, JoinType.LEFT);
taskQuery.where(builder.equal(taskTable.get(Task_.TYPE).get("status"), "RECEIVED"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM