Skip to content

Commit

Permalink
Attempt to reproduce issue #3618
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Jan 6, 2024
1 parent fc88035 commit 91faacf
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import com.querydsl.jpa.domain.Location;
import com.querydsl.jpa.domain.QDomesticCat;
import com.querydsl.jpa.domain.QEmployee;
import com.querydsl.jpa.domain.QUser;
import com.querydsl.jpa.domain.UserRole;
import java.util.Arrays;
import org.junit.Test;

Expand Down Expand Up @@ -196,6 +198,24 @@ public void delete_with_subQuery() {
""");
}

@Test
public void where_in() {
QUser user = QUser.user;

JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);
QueryMetadata md = new DefaultQueryMetadata();
md.addJoin(JoinType.DEFAULT, user);
md.setProjection(user);
md.addWhere(user.roles.contains(UserRole.ADMIN));
serializer.serialize(md, false, null);
assertThat(serializer.toString())
.isEqualTo("""
select user
from User user
where ?1 in user.roles
""");
}

@Test
public void in() {
JPQLSerializer serializer = new JPQLSerializer(HQLTemplates.DEFAULT);
Expand Down
9 changes: 8 additions & 1 deletion querydsl-jpa/src/test/java/com/querydsl/jpa/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@
*/
package com.querydsl.jpa.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import java.util.Set;

/** The Class User. */
@Entity
@Table(name = "user_")
@Table(name = "userc")
public class User {
@ManyToOne Company company;

@Id long id;

String userName, firstName, lastName;

@Convert(converter = UserRolesConverter.class)
@Column(columnDefinition = "TEXT")
Set<UserRole> roles;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.querydsl.jpa.domain;

public enum UserRole {
ADMIN,
WRITER,
READER,
ANONYMOUS
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.querydsl.jpa.domain;

import jakarta.persistence.AttributeConverter;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class UserRolesConverter implements AttributeConverter<Set<UserRole>, String> {

@Override
public String convertToDatabaseColumn(Set<UserRole> roles) {
if (roles == null || roles.isEmpty()) return null;

return roles.stream().map(UserRole::name).collect(Collectors.joining(","));
}

@Override
public Set<UserRole> convertToEntityAttribute(String dbData) {
if (dbData == null || dbData.isEmpty()) {
return Set.of();
}

return Arrays.stream(dbData.split(",")).map(UserRole::valueOf).collect(Collectors.toSet());
}
}

0 comments on commit 91faacf

Please sign in to comment.