forked from Blazebit/blaze-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fallback to default Size if not Sized, fixes Blazebit#545
- Loading branch information
1 parent
f892e33
commit 7ef4de3
Showing
2 changed files
with
171 additions
and
8 deletions.
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
core/testsuite/src/test/hibernate/com/blazebit/persistence/testsuite/Issue545Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright 2014 - 2018 Blazebit. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.blazebit.persistence.testsuite; | ||
|
||
import com.blazebit.persistence.testsuite.tx.TxVoidWork; | ||
import org.hibernate.HibernateException; | ||
import org.hibernate.annotations.Formula; | ||
import org.hibernate.annotations.Type; | ||
import org.hibernate.annotations.TypeDef; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.usertype.UserType; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
import javax.persistence.EntityManager; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
import java.io.Serializable; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.sql.Types; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
/** | ||
* @author Jan-Willem Gmelig Meyling | ||
* @since 1.3.0 | ||
*/ | ||
public class Issue545Test extends AbstractCoreTest { | ||
|
||
@Override | ||
protected Class<?>[] getEntityClasses() { | ||
return new Class<?>[] { EntityWithCustomType.class }; | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
transactional(new TxVoidWork() { | ||
@Override | ||
public void work(EntityManager em) { | ||
EntityWithCustomType entityWithCustomType = new EntityWithCustomType(); | ||
entityWithCustomType.customValue = "custom"; | ||
em.persist(entityWithCustomType); | ||
em.flush(); | ||
em.clear(); | ||
} | ||
}); | ||
} | ||
|
||
@Test | ||
public void queryEntityWithCustomTypeTest() { | ||
transactional(new TxVoidWork() { | ||
@Override | ||
public void work(EntityManager em) { | ||
EntityWithCustomType singleResult = cbf.create(em, EntityWithCustomType.class) | ||
.where("transformedCustomValue").eq("CUSTOM") | ||
.getSingleResult(); | ||
|
||
assertNotNull(singleResult); | ||
} | ||
}); | ||
} | ||
|
||
@Entity | ||
@TypeDef(name = "CustomType", typeClass = CustomType.class) | ||
public static class EntityWithCustomType { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.AUTO) | ||
private Long id; | ||
|
||
@Column(name = "custom_value") | ||
private String customValue; | ||
|
||
@Formula("UPPER(custom_value)") | ||
@Type(type = "CustomType") | ||
private String transformedCustomValue; | ||
|
||
} | ||
|
||
public static class CustomType implements UserType { | ||
|
||
@Override | ||
public int[] sqlTypes() { | ||
return new int[] { Types.VARCHAR }; | ||
} | ||
|
||
@Override | ||
public Class returnedClass() { | ||
return String.class; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object x, Object y) throws HibernateException { | ||
return x.equals(y); | ||
} | ||
|
||
@Override | ||
public int hashCode(Object x) throws HibernateException { | ||
return x.hashCode(); | ||
} | ||
|
||
@Override | ||
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { | ||
return rs.getString(names[0]); | ||
} | ||
|
||
@Override | ||
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { | ||
st.setString(index, value.toString()); | ||
|
||
} | ||
|
||
@Override | ||
public Object deepCopy(Object value) throws HibernateException { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean isMutable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Serializable disassemble(Object value) throws HibernateException { | ||
return (Serializable) value; | ||
} | ||
|
||
@Override | ||
public Object assemble(Serializable cached, Object owner) throws HibernateException { | ||
return cached; | ||
} | ||
|
||
@Override | ||
public Object replace(Object original, Object target, Object owner) throws HibernateException { | ||
return original; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters