Skip to content

Commit

Permalink
Merge pull request #11689 from simkam/usertypes
Browse files Browse the repository at this point in the history
[WFLY-11069] test Hibernate51CompatibilityTransformer transformations…
  • Loading branch information
kabir committed Sep 26, 2018
2 parents 2c71cf2 + 49196eb commit 6d7b521
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public abstract class AbstractVerifyHibernate51CompatibilityTestCase {
+ "<version name=\"version\" column=\"version\" type=\"org.jboss.as.test.compat.jpa.hibernate.transformer.IntegerUserVersionType\" />"
+ "<property name=\"firstName\" column=\"first_name\"/>" + "<property name=\"lastName\" column=\"last_name\"/>"
+ "<property name=\"address\"/>"
+ "<property name=\"remote\" type=\"org.jboss.as.test.compat.jpa.hibernate.transformer.BooleanAbstractStandardBasicType\"/>"
+ "<property name=\"foreigner\" type=\"org.jboss.as.test.compat.jpa.hibernate.transformer.BooleanType\"/>"
+ "</class>"
+ "<class name=\"org.jboss.as.test.compat.jpa.hibernate.transformer.Gene\" table=\"GENE\">"
+ "<id name=\"id\">" + "<generator class=\"assigned\"/>" + "</id>"
Expand Down Expand Up @@ -105,6 +107,8 @@ protected static JavaArchive getLib() {
lib.addClass(IntegerType.class);
lib.addClass(IntegerUserVersionType.class);
lib.addClass(BooleanSingleColumnType.class);
lib.addClass(BooleanAbstractStandardBasicType.class);
lib.addClass(BooleanType.class);
lib.addClasses(Gene.class);
lib.addClasses(State.class);
lib.addClasses(StateType.class);
Expand Down Expand Up @@ -137,6 +141,7 @@ public void testSimpleOperation() {
try {
Student s1 = sfsb.createStudent("MADHUMITA", "SADHUKHAN", "99 Purkynova REDHAT BRNO CZ", 1);
Student st = sfsb.getStudent(s1.getStudentId());
assertEquals("student remote attribute read from hibernate session should be true", true, st.getRemote());
assertEquals("name read from hibernate session is MADHUMITA", "MADHUMITA", st.getFirstName());
} finally {
sfsb.cleanup();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.test.compat.jpa.hibernate.transformer;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.AbstractStandardBasicType;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;

public class BooleanAbstractStandardBasicType extends AbstractStandardBasicType<Boolean> {

public BooleanAbstractStandardBasicType() {
this( org.hibernate.type.descriptor.sql.BooleanTypeDescriptor.INSTANCE, BooleanTypeDescriptor.INSTANCE );
}

public BooleanAbstractStandardBasicType(SqlTypeDescriptor sqlTypeDescriptor, JavaTypeDescriptor<Boolean> javaTypeDescriptor) {
super( sqlTypeDescriptor, javaTypeDescriptor);
}

private static void internalSessionImplementorUsingMethod(SessionImplementor session) {
session.isTransactionInProgress();
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) throws HibernateException, SQLException {
internalSessionImplementorUsingMethod(session);
if (value == null) {
st.setNull( index, Types.BOOLEAN );
}
else {
st.setBoolean( index, (Boolean) value );
}
}

@Override
public Object get(ResultSet rs, String name, SessionImplementor session) throws HibernateException, SQLException {
return super.get(rs, name, session);
}

@Override
public void set(PreparedStatement st, Boolean value, int index, SessionImplementor session) throws HibernateException, SQLException {
super.set(st, value, index, session);
}

@Override
public String getName() {
return BooleanAbstractStandardBasicType.class.getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.as.test.compat.jpa.hibernate.transformer;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.internal.util.collections.ArrayHelper;
import org.hibernate.type.ForeignKeyDirection;
import org.hibernate.type.Type;

public class BooleanType implements Type {
private final Size dictatedSize = new Size();
private static final Size DEFAULT_SIZE = new Size(19, 2, 255, Size.LobMultiplier.NONE);

@Override
public boolean isAssociationType() {
return false;
}

@Override
public boolean isCollectionType() {
return false;
}

@Override
public boolean isEntityType() {
return false;
}

@Override
public boolean isAnyType() {
return false;
}

@Override
public boolean isComponentType() {
return false;
}

@Override
public int getColumnSpan(Mapping mapping) throws MappingException {
return 1;
}

@Override
public int[] sqlTypes(Mapping mapping) throws MappingException {
return new int[]{Types.BOOLEAN};
}

@Override
public Size[] dictatedSizes(Mapping mapping) throws MappingException {
return new Size[]{dictatedSize};
}

@Override
public Size[] defaultSizes(Mapping mapping) throws MappingException {
return new Size[]{DEFAULT_SIZE};
}

@Override
public Class getReturnedClass() {
return org.hibernate.type.descriptor.java.BooleanTypeDescriptor.INSTANCE.getJavaTypeClass();
}

@Override
public boolean isSame(Object x, Object y) throws HibernateException {
return ((Boolean) x).equals((Boolean) y);
}

@Override
public boolean isEqual(Object x, Object y) throws HibernateException {
return ((Boolean) x).equals((Boolean) y);
}

@Override
public boolean isEqual(Object x, Object y, SessionFactoryImplementor factory) throws HibernateException {
return ((Boolean) x).equals((Boolean) y);
}

@Override
public int getHashCode(Object x) throws HibernateException {
return x.hashCode();
}

@Override
public int getHashCode(Object x, SessionFactoryImplementor factory) throws HibernateException {
return getHashCode(x);
}

@Override
public int compare(Object x, Object y) {
return ((Boolean) x).compareTo((Boolean) y);
}

@Override
public boolean isDirty(Object old, Object current, SessionImplementor session) throws HibernateException {
return !isSame(old, current);
}

@Override
public boolean isDirty(Object oldState, Object currentState, boolean[] checkable, SessionImplementor session) throws HibernateException {
return isDirty(oldState, currentState, session);
}

@Override
public boolean isModified(Object dbState, Object currentState, boolean[] checkable, SessionImplementor session) throws HibernateException {
return isSame(dbState, currentState);
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
return nullSafeGet(rs, names[0], session, owner);
}

@Override
public Object nullSafeGet(ResultSet rs, String name, SessionImplementor session, Object owner) throws HibernateException, SQLException {
session.isTransactionInProgress();
boolean result = rs.getBoolean(name);
return new Boolean(result);
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, boolean[] settable, SessionImplementor session) throws HibernateException, SQLException {
nullSafeSet(st, value, index, session);
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.BOOLEAN);
} else {
st.setBoolean(index, (Boolean) value);
}
}

@Override
public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException {
return org.hibernate.type.descriptor.java.BooleanTypeDescriptor.INSTANCE.extractLoggableRepresentation((Boolean) value);
}

@Override
public String getName() {
return BooleanType.class.getName();
}

@Override
public Object deepCopy(Object value, SessionFactoryImplementor factory) throws HibernateException {
return value;
}

@Override
public boolean isMutable() {
return false;
}

@Override
public Serializable disassemble(Object value, SessionImplementor session, Object owner) throws HibernateException {
return (Serializable) value;
}

@Override
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
return cached;
}

@Override
public void beforeAssemble(Serializable cached, SessionImplementor session) {

}

@Override
public Object hydrate(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
return null;
}

@Override
public Object resolve(Object value, SessionImplementor session, Object owner) throws HibernateException {
return value;
}

@Override
public Object semiResolve(Object value, SessionImplementor session, Object owner) throws HibernateException {
return value;
}

@Override
public Type getSemiResolvedType(SessionFactoryImplementor factory) {
return this;
}

@Override
public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache) throws HibernateException {
return original;
}

@Override
public Object replace(Object original, Object target, SessionImplementor session, Object owner, Map copyCache, ForeignKeyDirection foreignKeyDirection) throws HibernateException {
return replace(original, target, session, owner, copyCache);
}

@Override
public boolean[] toColumnNullness(Object value, Mapping mapping) {
return value == null ? ArrayHelper.FALSE : ArrayHelper.TRUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public Student createStudent(String firstName, String lastName, String address,
student.setAddress(address);
student.setFirstName(firstName);
student.setLastName(lastName);
student.setRemote(true);
student.setForeigner(true);

try {
Session session = sessionFactory.openSession();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class Student {
private String lastName;
// address of the student
private String address;

private Boolean remote;
private Boolean foreigner;
private Integer version;

/**
Expand Down Expand Up @@ -138,6 +139,22 @@ public void setVersion(Integer version) {
this.version = version;
}

public Boolean getRemote() {
return remote;
}

public void setRemote(Boolean remote) {
this.remote = remote;
}

public Boolean getForeigner() {
return foreigner;
}

public void setForeigner(Boolean foreigner) {
this.foreigner = foreigner;
}

/**
* Method used by the UI to clear information on the screen.
*
Expand Down

0 comments on commit 6d7b521

Please sign in to comment.