HHH-4699 test case
This commit is contained in:
parent
ad5dd18aac
commit
d91ed8942c
|
@ -1,16 +1,19 @@
|
||||||
package org.hibernate.test.annotations.enumerated;
|
package org.hibernate.test.annotations.enumerated;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.EnumType;
|
import javax.persistence.EnumType;
|
||||||
import javax.persistence.Enumerated;
|
import javax.persistence.Enumerated;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|
||||||
import org.hibernate.annotations.Type;
|
import org.hibernate.annotations.Type;
|
||||||
import org.hibernate.annotations.TypeDef;
|
import org.hibernate.annotations.TypeDef;
|
||||||
import org.hibernate.annotations.TypeDefs;
|
import org.hibernate.annotations.TypeDefs;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Janario Oliveira
|
* @author Janario Oliveira
|
||||||
|
* @author Brett Meyer
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@TypeDefs({ @TypeDef(typeClass = LastNumberType.class, defaultForType = EntityEnum.LastNumber.class) })
|
@TypeDefs({ @TypeDef(typeClass = LastNumberType.class, defaultForType = EntityEnum.LastNumber.class) })
|
||||||
|
@ -31,6 +34,11 @@ public class EntityEnum {
|
||||||
NUMBER_1, NUMBER_2, NUMBER_3
|
NUMBER_1, NUMBER_2, NUMBER_3
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum Trimmed {
|
||||||
|
|
||||||
|
A, B, C
|
||||||
|
}
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
private long id;
|
private long id;
|
||||||
|
@ -42,6 +50,9 @@ public class EntityEnum {
|
||||||
private LastNumber lastNumber;
|
private LastNumber lastNumber;
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private LastNumber explicitOverridingImplicit;
|
private LastNumber explicitOverridingImplicit;
|
||||||
|
@Column(columnDefinition = "char(5)")
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
private Trimmed trimmed;
|
||||||
|
|
||||||
public long getId() {
|
public long getId() {
|
||||||
return id;
|
return id;
|
||||||
|
@ -90,4 +101,12 @@ public class EntityEnum {
|
||||||
public void setExplicitOverridingImplicit(LastNumber explicitOverridingImplicit) {
|
public void setExplicitOverridingImplicit(LastNumber explicitOverridingImplicit) {
|
||||||
this.explicitOverridingImplicit = explicitOverridingImplicit;
|
this.explicitOverridingImplicit = explicitOverridingImplicit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Trimmed getTrimmed() {
|
||||||
|
return trimmed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTrimmed(Trimmed trimmed) {
|
||||||
|
this.trimmed = trimmed;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,28 @@
|
||||||
package org.hibernate.test.annotations.enumerated;
|
package org.hibernate.test.annotations.enumerated;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.sql.Statement;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.hibernate.Query;
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import org.hibernate.criterion.Restrictions;
|
import org.hibernate.criterion.Restrictions;
|
||||||
|
import org.hibernate.engine.spi.SessionImplementor;
|
||||||
import org.hibernate.mapping.PersistentClass;
|
import org.hibernate.mapping.PersistentClass;
|
||||||
|
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.hibernate.testing.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
import org.hibernate.type.EnumType;
|
import org.hibernate.type.EnumType;
|
||||||
import org.hibernate.type.Type;
|
import org.hibernate.type.Type;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.hibernate.test.annotations.enumerated.EntityEnum.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test type definition for enum
|
* Test type definition for enum
|
||||||
|
@ -328,6 +340,35 @@ public class EnumeratedTypeTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue(jiraKey = "HHH-4699")
|
||||||
|
public void testTrimmedEnum() throws SQLException {
|
||||||
|
// use native SQL to insert, forcing whitespace to occur
|
||||||
|
final Session s = openSession();
|
||||||
|
final Connection connection = ((SessionImplementor)s).connection();
|
||||||
|
final Statement statement = connection.createStatement();
|
||||||
|
statement.execute("insert into EntityEnum (id, trimmed) values(1, '" + Trimmed.A.name() + "')");
|
||||||
|
statement.execute("insert into EntityEnum (id, trimmed) values(2, '" + Trimmed.B.name() + "')");
|
||||||
|
|
||||||
|
s.getTransaction().begin();
|
||||||
|
|
||||||
|
// ensure EnumType can do #fromName with the trimming
|
||||||
|
List<EntityEnum> resultList = s.createQuery("select e from EntityEnum e").list();
|
||||||
|
assertEquals( resultList.size(), 2 );
|
||||||
|
assertEquals( resultList.get(0).getTrimmed(), Trimmed.A );
|
||||||
|
assertEquals( resultList.get(1).getTrimmed(), Trimmed.B );
|
||||||
|
|
||||||
|
// ensure querying works
|
||||||
|
final Query query = s.createQuery("select e from EntityEnum e where e.trimmed=?");
|
||||||
|
query.setParameter( 0, Trimmed.A );
|
||||||
|
resultList = query.list();
|
||||||
|
assertEquals( resultList.size(), 1 );
|
||||||
|
assertEquals( resultList.get(0).getTrimmed(), Trimmed.A );
|
||||||
|
|
||||||
|
s.getTransaction().rollback();
|
||||||
|
s.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class[] getAnnotatedClasses() {
|
protected Class[] getAnnotatedClasses() {
|
||||||
return new Class[] { EntityEnum.class };
|
return new Class[] { EntityEnum.class };
|
||||||
|
|
Loading…
Reference in New Issue