Two tables:
CREATE TABLE `foo` (
`foo_id` bigint(20) not null auto_increment,
`name` varchar(32) not null,
`_deleted_` tinyint(1) default '0',
PRIMARY KEY (`foo_id`)
) ;
CREATE TABLE `bar` (
`bar_id` bigint(20) not null auto_increment,
`foo_id` bigint(20) not null,
`key` varchar(32) not null,
`value` varchar(125) not null,
`_deleted_` tinyint(1) default '0',
PRIMARY KEY (`bar_id`)
);
Table contents:
select * from foo;
+--------+-------+-----------+
| foo_id | name | _deleted_ |
+--------+-------+-----------+
| 1 | cat | 0 |
| 2 | dog | 0 |
| 3 | mouse | 0 |
| 4 | rat | 1 |
+--------+-------+-----------+
3 rows in set (0.00 sec)
select * from bar;
+--------+--------+-------+--------+-----------+
| bar_id | foo_id | key | value | _deleted_ |
+--------+--------+-------+--------+-----------+
| 1 | 1 | sound | meow | 0 |
| 2 | 1 | ears | pointy | 0 |
| 3 | 2 | sound | ruff | 0 |
| 4 | 2 | nose | long | 0 |
| 5 | 3 | sound | squeak | 0 |
| 6 | 3 | tail | long | 0 |
| 7 | 3 | legs | two | 1 |
+--------+--------+-------+--------+-----------+
6 rows in set (0.00 sec)
The query I want to create:
select f.foo_id, f.name, b.key, b.value from foo f, bar b
where f.foo_id = b.foo_id and f._deleted_ = 0 and b._deleted_ = 0;
+--------+-------+-------+--------+
| foo_id | name | key | value |
+--------+-------+-------+--------+
| 1 | cat | sound | meow |
| 1 | cat | ears | pointy |
| 2 | dog | sound | ruff |
| 2 | dog | nose | long |
| 3 | mouse | sound | squeak |
| 3 | mouse | tail | long |
+--------+-------+-------+--------+
6 rows in set (0.01 sec)
Foo class:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Entity(name = "foo")
public class Foo {
@Id
@Column(name = "foo_id", nullable = false, unique = true, columnDefinition = "bigint(20)")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long fooId;
private String name;
@Column(name = "_deleted_")
private Short deleted;
@OneToMany
@JoinTable(name="bar",
joinColumns=@JoinColumn(name="foo_id"))
private List<Bar> bars;
}
Bar class:
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Entity(name = "bar")
public class Bar {
@Id
@Column(name = "bar_id", nullable = false, unique = true, columnDefinition = "bigint(20)")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long barId;
private Long fooId;
private String key;
private String value;
@Column(name = "_deleted_")
private Short deleted;
}
Attempt to Join them:
protected Stream<Foo> getFoosWithBars() {
return this.jpaApi.withTransaction(entityManager -> {
final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<Foo> criteria = builder.createQuery(Foo.class);
Root<Foo> fromFoo = criteria.from(Foo.class);
Join<Foo, Bar> foobars = fromFoo.join("fooId");
List<Predicate> conditions = new ArrayList();
conditions.add(builder.notEqual(fromFoo.get("deleted"), 1));
# what goes here?
conditions.add(builder.notEqual(???Bar???.get("deleted"), 1));
TypedQuery<Foo> typedQuery = entityManager.createQuery(criteria
.select(fromFoo)
.where(conditions.toArray(new Predicate[] {})));
return typedQuery.getResultList().stream();
});
}