Posts Tagged 'Java'

How to work around when java.lang.Boolean is in the black list of LegacySerialiaytionPolicy

It took me a while to figure out that java.lang.Boolean is in the blacklist of the LegacySerializationPolicy class. Therefore when my RPC service tries to transfer java.lang.Boolean object, it throws Exception

"Type '*java.lang.Boolean*' was not included in the set of types which can be serialized 
by this SerializationPolicy or its Class object could not be loaded.
For security purposes, this type will not be serialized."

 

Look at the class com.google.gwt.user.server.rpc.impl.LegacySerialiaytionPolicy

  /**
   * Many JRE types would appear to be {@link Serializable} on the server.
   * However, clients would not see these types as being {@link Serializable}
   * due to mismatches between the GWT JRE emulation and the real JRE. As a
   * workaround, this blacklist specifies a list of problematic types which
   * should be seen as not implementing {@link Serializable} for the purpose
   * matching the client's expectations. Note that a type on this list may still
   * be serializable via a custom serializer.
   */
  private static final Class<?>[] JRE_BLACKLIST = {
      java.lang.ArrayStoreException.class, java.lang.AssertionError.class,
      java.lang.Boolean.class, java.lang.Byte.class, java.lang.Character.class,
      java.lang.Class.class, java.lang.ClassCastException.class,
      java.lang.Double.class, java.lang.Error.class, java.lang.Float.class,
      java.lang.IllegalArgumentException.class,
      java.lang.IllegalStateException.class,
      java.lang.IndexOutOfBoundsException.class, java.lang.Integer.class,
      java.lang.Long.class, java.lang.NegativeArraySizeException.class,
      java.lang.NullPointerException.class, java.lang.Number.class,
      java.lang.NumberFormatException.class, java.lang.Short.class,
      java.lang.StackTraceElement.class, java.lang.String.class,
      java.lang.StringBuffer.class,
      java.lang.StringIndexOutOfBoundsException.class,
      java.lang.UnsupportedOperationException.class, java.util.ArrayList.class,
      java.util.ConcurrentModificationException.class, java.util.Date.class,
      java.util.EmptyStackException.class, java.util.EventObject.class,
      java.util.HashMap.class, java.util.HashSet.class,
      java.util.MissingResourceException.class,
      java.util.NoSuchElementException.class, java.util.Stack.class,
      java.util.TooManyListenersException.class, java.util.Vector.class};

 

How to work around ?

Use a custom serializable class instead of java.lang.Boolean

import com.google.gwt.user.client.rpc.IsSerializable;

public class SerializedBoolean implements IsSerializable {

    private boolean value;

    public static final SerializedBoolean TRUE = new SerializedBoolean(true);
    public static final SerializedBoolean FALSE = new SerializedBoolean(false);

    public SerializedBoolean(){
        value = false;
    }

    public SerializedBoolean(boolean value){
        this.value = value;
    }

    public boolean isValue() {
        return value;
    }

    public void setValue(boolean value) {
        this.value = value;
    }

}
7 July 2010 at 05:55 - Comments

Hibernate Annotation: length-limited field is not auto truncated

Problem:

Let’s consider a table containing a length-limited field (e.g. COUNTRY_CODE is 2-char length).

CREATE TABLE  `country` (

	`COUNTRY_CODE` varchar(2),
	`COUNTRY_NAME` varchar(255),

) 

- The length value is already specified in Hibernate annotation.

@Column(name="COUNTRY_CODE", length=2)
private String countryCode;

- However when inserting a value length beyond the limit, this value is not automatically truncated, and Hibernate throws the following exception:

Caused by: java.sql.BatchUpdateException: Data truncation: Data too long for column 'COUNTRY_CODE' at row 1

Solution: Use custom SQL :

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.SQLInsert;

/**
* The persistent class for the country database table.
*
*/
@Entity
@Table(name="country")
@SQLInsert(sql="INSERT IGNORE INTO country (country_name, country_code) VALUES(?,upper(?))")
public class Country {

	@Id
	@Column(name="COUNTRY_CODE", length=2)
	private String countryCode;

	@Column(name="COUNTRY_NAME")
	private String countryName;

	//bi-directional many-to-one association to State
	@OneToMany(mappedBy="country")
	private List<State> states;

	//... standard getters and setters

}
25 January 2010 at 21:04 - Comments

Hibernate throws Exception “Simultaneously Fetch Multiple Bags”

Problem: Hibernate throws Exception “Simultaneously Fetch Multiple Bags”

I try the 3rd solution introduced in this entry, and it does work!

The third solution is to replace java.util.List and java.util.Collection with java.util.Set. We have to think about it – do we really need a List/Collection (a bag)? In many cases the only reason to use a List is that we are used to.

read more at http://jroller.com

28 December 2009 at 21:25 - Comments