Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何编写连接多个表的spring boot jpa规范_Java_Mysql_Spring Boot_Spring Data Jpa_Specifications - Fatal编程技术网

Java 如何编写连接多个表的spring boot jpa规范

Java 如何编写连接多个表的spring boot jpa规范,java,mysql,spring-boot,spring-data-jpa,specifications,Java,Mysql,Spring Boot,Spring Data Jpa,Specifications,我想使用spring引导规范编写以下查询 SELECT o.* from orders as o inner join user as u on o.user_id = u.id inner join user_group as ug on u.user_group_id = ug.id left join order_product op on o.id = op.order_id left join mobile_order_product mop o

我想使用spring引导规范编写以下查询

SELECT o.*
from orders as o
     inner join user as u on o.user_id = u.id
     inner join user_group as ug on u.user_group_id = ug.id
     left join order_product op on o.id = op.order_id
     left join mobile_order_product mop on op.id = mop.order_product_id
     left join mobile_device as md on mop.mobile_device_id = md.id
     left join tablet_order_product top on op.id = top.order_product_id
     left join tablet_device as td on top.tablet_device_id = td.id
where ug.id = 1
     and (md.imei = 123456789 or td.imei = 123456789)
我试着写下面这样的规范,但我找不到一种方法加入order\u product表

    public static Specification<Order> filterOrdersByGroupIdAndImei(int userGroupId, int imei) {
        return (root, query, cb) -> {
            Join<Object, User> user = root.join("user");
            Join<Object, UserGroup> userGroup = user.join("userGroup");
//            how to join order_product and other join tables

            Predicate equalPredicate = cb.equal(userGroup.get("id"), userGroupId);
            return cb.and(equalPredicate);
        };
    }
公共静态规范过滤器OrdersByGroupIDandiMei(int-userGroupId,int-imei){
返回(根、查询、cb)->{
Join user=root.Join(“用户”);
Join userGroup=user.Join(“userGroup”);
//如何联接order_产品和其他联接表
谓词equalPredicate=cb.equal(userGroup.get(“id”),userGroupId);
返回cb.和(等额预测);
};
}

我将在我自己的问题中给出答案

@Entity
@Table(name = "orders")
public class Order {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(referencedColumnName = "id", nullable = false)
    @JsonIgnore
    private User user;

    @OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
    private List<OrderProduct> orderProducts ;
}

@Entity
@Table(name = "order_product")
public class OrderProduct {

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

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(referencedColumnName = "id", nullable = false)
    @JsonIgnore
    private Order order;

    @OneToMany(mappedBy = "orderProduct", fetch = FetchType.LAZY)
    private List<MobileOrderProduct> mobileOrderProducts;


    @OneToMany(mappedBy = "orderProduct", fetch = FetchType.LAZY)
    private List<TabletOrderProduct> tabletOrderProducts;
}

@Entity
@Table(name = "mobile_order_product")
public class MobileOrderProduct {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String mobileCode;
    private String mobileNumber;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(referencedColumnName = "id", nullable = false)
    @JsonIgnore
    private MobileDevice mobileDevice;

    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnore
    @JoinColumn(referencedColumnName = "id", nullable = false)
    private OrderProduct orderProduct;
}

@Entity
@Table(name = "mobile_device")
public class MobileDevice {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String serialNumber;
    private String imei;

    @OneToMany(mappedBy = "mobileDevice", fetch = FetchType.LAZY)
    @JsonIgnore
    private List<MobileOrderProduct> mobileOrderProducts;
}
@实体
@表(name=“orders”)
公共阶级秩序{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(referencedColumnName=“id”,null=false)
@杰索尼奥雷
私人用户;
@OneToMany(mappedBy=“order”,fetch=FetchType.LAZY)
私人列表订购产品;
}
@实体
@表(name=“订单\产品”)
公共类订单产品{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(referencedColumnName=“id”,null=false)
@杰索尼奥雷
私人秩序;
@OneToMany(mappedBy=“orderProduct”,fetch=FetchType.LAZY)
私有列表移动订单产品;
@OneToMany(mappedBy=“orderProduct”,fetch=FetchType.LAZY)
私有列表TableToDerProducts;
}
@实体
@表(name=“移动\订单\产品”)
公共类MobileOrderProduct{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
专用字符串移动编码;
私有字符串移动枚举器;
@manytone(fetch=FetchType.LAZY)
@JoinColumn(referencedColumnName=“id”,null=false)
@杰索尼奥雷
私人移动设备移动设备;
@manytone(fetch=FetchType.LAZY)
@杰索尼奥雷
@JoinColumn(referencedColumnName=“id”,null=false)
私人订单产品订单产品;
}
@实体
@表(name=“移动设备”)
公共类移动设备{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
私有字符串序列号;
私有字符串imei;
@OneToMany(mappedBy=“mobileDevice”,fetch=FetchType.LAZY)
@杰索尼奥雷
私有列表移动订单产品;
}
这里我只包含了两个实体类,因为这样您就可以正确理解表结构

    public static Specification<Order> filterOrdersByGroupIdAndImei(int userGroupId, String imei) {
        return (root, query, cb) -> {
            List<Predicate> list = new ArrayList<Predicate>();
            Join<Order, User> user = root.join("user");
            Join<User, UserGroup> userGroup = user.join("userGroup");
            Join<Order, OrderProduct> orderProduct = root.join("orderProducts", JoinType.INNER);

            Join<OrderProduct, MobileDevice> mobileDevice = orderProduct
                    .join("mobileOrderProducts", JoinType.LEFT)
                    .join("mobileDevice", JoinType.LEFT);

            Join<OrderProduct, TabletDevice> tabletDevice = orderProduct
                    .join("tabletOrderProducts", JoinType.LEFT)
                    .join("tabletDevice", JoinType.LEFT);

            list.add(cb.equal(userGroup.get("id"), userGroupId));
            list.add(cb.or(cb.equal(mobileDevice.get("imei"), imei), cb.equal(tabletDevice.get("imei"), imei)));
            Predicate[] p = new Predicate[list.size()];
            return cb.and(list.toArray(p));

}
公共静态规范过滤器OrdersByGroupIDandiMei(int-userGroupId,字符串imei){
返回(根、查询、cb)->{
列表=新的ArrayList();
Join user=root.Join(“用户”);
Join userGroup=user.Join(“userGroup”);
Join orderProduct=root.Join(“orderProducts”,JoinType.INNER);
加入mobileDevice=orderProduct
.join(“mobileOrderProducts”,JoinType.LEFT)
.join(“移动设备”,JoinType.LEFT);
Join tabletDevice=orderProduct
.join(“tabletOrderProducts”,JoinType.LEFT)
.join(“tabletDevice”,jointType.LEFT);
添加(cb.equal(userGroup.get(“id”)、userGroupId));
添加(cb.or(cb.equal(mobileDevice.get(“imei”),imei),cb.equal(tabletDevice.get(“imei”),imei));
谓词[]p=新谓词[list.size()];
返回cb.and(list.toArray(p));
}

userGroup.join(“order\u product”)怎么办?它不起作用什么不起作用?