栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

弹簧数据加入规格

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

弹簧数据加入规格

编辑:好的,我在这里搞得一团糟,但我希望这次我更接近正确的答案。

考虑一下(id像John的1一样自动生成,等等):

INSERT INTO some_user (name) VALUES ('John');INSERT INTO some_user (name) VALUES ('Ariel');INSERT INTO some_user (name) VALUES ('Brian');INSERT INTO some_user (name) VALUES ('Kelly');INSERT INTO some_user (name) VALUES ('Tom');INSERT INTO some_user (name) VALUES ('Sonya');INSERT INTO product (owner_id,name) VALUES (1,'Nokia 3310');INSERT INTO product (owner_id,name) VALUES (2,'Sony Xperia Aqua');INSERT INTO product (owner_id,name) VALUES (3,'IPhone 4S');INSERT INTO product (owner_id,name) VALUES (1,'Xiaomi MI5');INSERT INTO product (owner_id,name) VALUES (3,'Samsung Galaxy S7');INSERT INTO product (owner_id,name) VALUES (3,'Sony Xperia Z3');INSERT INTO following_relationship (follower_id, owner_id) VALUES (4,1);INSERT INTO following_relationship (follower_id, owner_id) VALUES (5,1);INSERT INTO following_relationship (follower_id, owner_id) VALUES (4,2);INSERT INTO following_relationship (follower_id, owner_id) VALUES (6,2);INSERT INTO following_relationship (follower_id, owner_id) VALUES (6,3);INSERT INTO following_relationship (follower_id, owner_id) VALUES (1,3);

基于您提供的实体的简化版本,以及SomeUser Entity,例如:

@Entitypublic class FollowingRelationship {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Long id;@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)@JoinColumn(name = "owner_id")SomeUser owner;@ManyToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)@JoinColumn(name = "follower_id")SomeUser follower;...@Entitypublic class Product {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Long id;@ManyToOne()@JoinColumn(name = "owner_id")private SomeUser owner;@Columnprivate String name;...@Entitypublic class SomeUser {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Long id;@Columnprivate String name;@oneToMany(mappedBy = "owner")private Set<Product> products = new HashSet<Product>();@oneToMany(mappedBy = "owner")private Set<FollowingRelationship> ownedRelationships = new HashSet<FollowingRelationship>();@oneToMany(mappedBy = "follower")private Set<FollowingRelationship> followedRelationships = new HashSet<FollowingRelationship>();

我创建了像这样的规范:

public static Specification<Product> joinTest(SomeUser input) {    return new Specification<Product>() {        public Predicate toPredicate(Root<Product> root, CriteriaQuery<?> query, CriteriaBuilder cb) { Join<Product,SomeUser> userProd = root.join("owner"); Join<FollowingRelationship,Product> prodRelation = userProd.join("ownedRelationships"); return cb.equal(prodRelation.get("follower"), input);        }    };}

现在,当我们执行以下查询时:

SomeUser someUser = someUserRepository.findOne(Specifications.where(ProductSpecifications.userHasName("Kelly")));List<Product> thatProducts = productRepository.findAll(Specifications.where(ProductSpecifications.joinTest(someUser)));System.out.println(thatProducts.toString());

我们得到:

[Product [id=1, name=Nokia 3310], Product [id=4, name=Xiaomi MI5], Product [id=2, name=Sony Xperia Aqua]]

在我看来,这等效于:“从另一个用户关注的所有用户那里获取所有产品”-获取Kelly正在关注的所有用户的产品。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/447002.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号