HHH-9605: Test for querying a collection of enums

This commit is contained in:
obr 2015-03-15 20:02:21 +01:00 committed by Steve Ebersole
parent c271e03bcc
commit 2c8ded5ff3
2 changed files with 63 additions and 1 deletions

View File

@ -1,11 +1,17 @@
package org.hibernate.test.annotations.enumerated;
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.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.annotations.Formula;
import org.hibernate.annotations.Type;
@ -59,6 +65,12 @@ public class EntityEnum {
@Enumerated(EnumType.STRING)
private Trimmed formula;
@Enumerated(EnumType.STRING)
@ElementCollection(targetClass = Common.class, fetch = FetchType.LAZY)
@JoinTable(name = "set_enum", joinColumns = { @JoinColumn(name = "entity_id") })
@Column(name = "common_enum", nullable = false)
private Set<Common> set = new HashSet<Common>();
public long getId() {
return id;
}
@ -122,4 +134,12 @@ public class EntityEnum {
public void setFormula(Trimmed formula) {
this.formula = formula;
}
public Set<Common> getSet() {
return set;
}
public void setSet(Set<Common> set) {
this.set = set;
}
}

View File

@ -16,6 +16,8 @@ import org.hibernate.mapping.PersistentClass;
import org.hibernate.type.EnumType;
import org.hibernate.type.Type;
import org.junit.Test;
import org.hibernate.testing.SkipForDialect;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
@ -23,7 +25,6 @@ import org.hibernate.test.annotations.enumerated.EntityEnum.Common;
import org.hibernate.test.annotations.enumerated.EntityEnum.FirstLetter;
import org.hibernate.test.annotations.enumerated.EntityEnum.LastNumber;
import org.hibernate.test.annotations.enumerated.EntityEnum.Trimmed;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
@ -402,6 +403,47 @@ public class EnumeratedTypeTest extends BaseNonConfigCoreFunctionalTestCase {
s.close();
}
@Test
@TestForIssue(jiraKey = "HHH-9605")
public void testSet() throws SQLException {
// **************
Session session = openSession();
session.getTransaction().begin();
// persist
EntityEnum entityEnum = new EntityEnum();
entityEnum.setString( Common.B2 );
entityEnum.getSet().add( Common.B2 );
Serializable id = session.save( entityEnum );
session.getTransaction().commit();
session.close();
session = openSession();
session.getTransaction().begin();
String sql = "select e from EntityEnum e where :param in elements( e.set ) ";
Query queryObject = session.createQuery( sql );
queryObject.setParameter( "param", Common.B2 );
// ensure EnumType can do #fromName with the trimming
List<EntityEnum> resultList = queryObject.list();
assertEquals( resultList.size(), 1 );
entityEnum = resultList.get( 0 );
assertEquals( id, entityEnum.getId() );
assertEquals( Common.B2, entityEnum.getSet().iterator().next() );
// delete
assertEquals( 1, session.createSQLQuery( "DELETE FROM set_enum" ).executeUpdate() );
assertEquals( 1, session.createSQLQuery( "DELETE FROM EntityEnum" ).executeUpdate() );
session.getTransaction().commit();
session.close();
}
@Override
protected Class[] getAnnotatedClasses() {
return new Class[] { EntityEnum.class };