休眠-按ID查询的简单访存将不起作用,但按对象查询却可以

时间:2018-09-12 08:13:01

标签: java sql hibernate spring-boot

我在JPARepository中有此方法:

@Query("SELECT e FROM Request e JOIN FETCH e.products JOIN FETCH e.productiveShifts WHERE e.id = :id ")
Request getRequestById(@Param("id") int id);

这是请求的Pojo:

import com.jfoenix.controls.datamodels.treetable.RecursiveTreeObject;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.Hibernate;

import javax.persistence.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

@Entity
@Getter
@Setter
@NoArgsConstructor
@Table(name = "iutp_requests")
public class Request {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @ManyToOne
    private Customer customer;

    @ManyToOne
    private User user;

    private Date creationDate = new Date();

    @ElementCollection
    private Set<ProductGood> products;

    @ElementCollection
    private Set<ProductiveShift> productiveShifts;

    @Override
    public String toString(){
        if(id == 0)
            return "Ungespeicherter Auftrag";
        return "#" + id + " - " + customer.getSurname() + " " + customer.getName() + " vom " + new SimpleDateFormat("dd.MM.yyyy").format(creationDate);
    }

}

现在,当我尝试使用getRequestById时,查询返回null。但是我的表包含具有给定ID的请求。那么为什么我的请求仍然返回null?我的语法错误吗?

编辑:

相比之下:

  @Query("SELECT e FROM Request e JOIN FETCH e.products JOIN FETCH e.productiveShifts WHERE e.customer = (:customer) ")
    List<Request> getRequestsByCustomer(@Param("customer") Customer customer);

此请求有效。

0 个答案:

没有答案