mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-30 21:08:30 +08:00
#800698 : fixed enum in path handling for Hibernate
This commit is contained in:
parent
9fcf0bce76
commit
c497cece3c
@ -77,4 +77,10 @@ public class HQLTemplates extends JPQLTemplates{
|
||||
return "1";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEnumInPathSupported() {
|
||||
// related : http://opensource.atlassian.com/projects/hibernate/browse/HHH-5159
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -17,6 +17,8 @@ import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
|
||||
import com.mysema.query.JoinExpression;
|
||||
import com.mysema.query.JoinType;
|
||||
@ -276,6 +278,16 @@ public class JPQLSerializer extends SerializerBase<JPQLSerializer> {
|
||||
|
||||
if (operator.equals(Ops.IN)){
|
||||
if (args.get(1) instanceof Path){
|
||||
if (!templates.isEnumInPathSupported() && args.get(0) instanceof Constant && Enum.class.isAssignableFrom(args.get(0).getType())) {
|
||||
Enumerated enumerated = ((Path)args.get(1)).getAnnotatedElement().getAnnotation(Enumerated.class);
|
||||
Enum constant = (Enum)((Constant)args.get(0)).getConstant();
|
||||
if (enumerated == null || enumerated.value() == EnumType.ORDINAL) {
|
||||
args = Arrays.asList(new ConstantImpl<Integer>(constant.ordinal()), args.get(1));
|
||||
} else {
|
||||
args = Arrays.asList(new ConstantImpl<String>(constant.name()), args.get(1));
|
||||
}
|
||||
}
|
||||
|
||||
super.visitOperation(type, JPQLTemplates.MEMBER_OF, args);
|
||||
}else{
|
||||
super.visitOperation(type, operator, args);
|
||||
|
||||
@ -115,6 +115,10 @@ public class JPQLTemplates extends Templates {
|
||||
// TODO : get rid of this when Hibernate supports type(alias)
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isEnumInPathSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getExistsProjection(){
|
||||
|
||||
@ -29,6 +29,8 @@ import com.mysema.query.jpa.JPQLQuery;
|
||||
import com.mysema.query.jpa.JPQLSubQuery;
|
||||
import com.mysema.query.jpa.domain.Cat;
|
||||
import com.mysema.query.jpa.domain.DomesticCat;
|
||||
import com.mysema.query.jpa.domain.Employee;
|
||||
import com.mysema.query.jpa.domain.JobFunction;
|
||||
import com.mysema.query.jpa.domain.QCat;
|
||||
import com.mysema.query.jpa.domain.QEmployee;
|
||||
import com.mysema.query.jpa.domain.QShow;
|
||||
@ -165,6 +167,10 @@ public abstract class AbstractStandardTest {
|
||||
show.acts.put("a","A");
|
||||
show.acts.put("b","B");
|
||||
save(show);
|
||||
|
||||
Employee employee = new Employee();
|
||||
employee.jobFunctions.add(JobFunction.CODER);
|
||||
save(employee);
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -489,5 +495,12 @@ public abstract class AbstractStandardTest {
|
||||
query.innerJoin(QEmployee.employee.user, QUser.user);
|
||||
query.list(QEmployee.employee);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Enum_In() {
|
||||
JPQLQuery query = query();
|
||||
query.from(QEmployee.employee).where(QEmployee.employee.jobFunctions.contains(JobFunction.CODER));
|
||||
assertEquals(1l, query.count());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
*/
|
||||
package com.mysema.query._derby;
|
||||
|
||||
import org.junit.Ignore;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.mysema.query.AbstractJPATest;
|
||||
@ -18,9 +19,10 @@ import com.mysema.testutil.JPATestRunner;
|
||||
* @author tiwe
|
||||
*
|
||||
*/
|
||||
@Ignore
|
||||
@RunWith(JPATestRunner.class)
|
||||
@JPAConfig("derby-eclipselink")
|
||||
public abstract class DerbyJPAEclipseLinkTest extends AbstractJPATest{
|
||||
public class DerbyJPAEclipseLinkTest extends AbstractJPATest{
|
||||
|
||||
@Override
|
||||
protected JPQLTemplates getTemplates(){
|
||||
|
||||
@ -5,7 +5,15 @@
|
||||
*/
|
||||
package com.mysema.query.jpa.domain;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.FetchType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.OneToOne;
|
||||
@ -16,13 +24,19 @@ import javax.persistence.OneToOne;
|
||||
@Entity
|
||||
public class Employee {
|
||||
@ManyToOne
|
||||
Company company;
|
||||
public Company company;
|
||||
|
||||
@OneToOne
|
||||
User user;
|
||||
public User user;
|
||||
|
||||
String firstName, lastName;
|
||||
public String firstName, lastName;
|
||||
|
||||
@Id
|
||||
int id;
|
||||
public int id;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "jobfunction")
|
||||
@ElementCollection (fetch = FetchType.EAGER)
|
||||
public Collection<JobFunction> jobFunctions = new HashSet<JobFunction>();
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,7 @@
|
||||
package com.mysema.query.jpa.domain;
|
||||
|
||||
public enum JobFunction {
|
||||
|
||||
MANAGER, CODER, CONSULTANT, CONTROLLER;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user