mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-27 21:01:15 +08:00
#789021 : added support for enums in queries
This commit is contained in:
parent
c4a05d3492
commit
6fdf826067
@ -42,7 +42,11 @@ public class MongodbSerializer implements Visitor<Object, Void> {
|
||||
|
||||
@Override
|
||||
public Object visit(Constant<?> expr, Void context) {
|
||||
return expr.getConstant();
|
||||
if (Enum.class.isAssignableFrom(expr.getType())) {
|
||||
return ((Enum<?>)expr.getConstant()).name();
|
||||
} else {
|
||||
return expr.getConstant();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -28,6 +28,7 @@ import com.mysema.query.mongodb.domain.Address;
|
||||
import com.mysema.query.mongodb.domain.City;
|
||||
import com.mysema.query.mongodb.domain.QUser;
|
||||
import com.mysema.query.mongodb.domain.User;
|
||||
import com.mysema.query.mongodb.domain.User.Gender;
|
||||
import com.mysema.query.mongodb.morphia.MorphiaQuery;
|
||||
import com.mysema.query.types.OrderSpecifier;
|
||||
import com.mysema.query.types.Predicate;
|
||||
@ -285,6 +286,16 @@ public class MongodbQueryTest {
|
||||
where(predicate.not()).count();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Enum_Eq() {
|
||||
assertQuery(user.gender.eq(Gender.MALE), u3, u4, u2, u1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Enum_Ne() {
|
||||
assertQuery(user.gender.ne(Gender.MALE));
|
||||
}
|
||||
|
||||
//TODO
|
||||
// - test dates
|
||||
@ -331,6 +342,7 @@ public class MongodbQueryTest {
|
||||
|
||||
private User addUser(String first, String last, int age, Address mainAddress, Address... addresses) {
|
||||
User user = new User(first, last, age, new Date());
|
||||
user.setGender(Gender.MALE);
|
||||
user.setMainAddress(mainAddress);
|
||||
for (Address address : addresses){
|
||||
user.addAddress(address);
|
||||
|
||||
@ -19,6 +19,8 @@ import com.google.code.morphia.annotations.Reference;
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
public enum Gender { MALE, FEMALE }
|
||||
|
||||
private @Id ObjectId id;
|
||||
|
||||
private String firstName;
|
||||
@ -27,14 +29,16 @@ public class User {
|
||||
|
||||
private Date created;
|
||||
|
||||
private Gender gender;
|
||||
|
||||
@Embedded
|
||||
private List<Address> addresses = new ArrayList<Address>();
|
||||
private final List<Address> addresses = new ArrayList<Address>();
|
||||
|
||||
@Embedded
|
||||
private Address mainAddress;
|
||||
|
||||
@Reference
|
||||
private List<User> friends = new ArrayList<User>();
|
||||
private final List<User> friends = new ArrayList<User>();
|
||||
|
||||
private int age;
|
||||
|
||||
@ -131,6 +135,14 @@ public class User {
|
||||
return friends;
|
||||
}
|
||||
|
||||
public Gender getGender() {
|
||||
return gender;
|
||||
}
|
||||
|
||||
public void setGender(Gender gender) {
|
||||
this.gender = gender;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user