Skip to content

Instantly share code, notes, and snippets.

@bmchild
bmchild / noNullCheck.java
Created July 11, 2012 15:24
Using commons lang to see if two objects are equals without a null check
new EqualsBuilder().append(obj1, obj2).isEquals()
@bmchild
bmchild / MyReflectionTestUtils.java
Created July 11, 2012 15:45
Use reflection to compare getters on two objects
/**
*
*/
package com.mypackage.utilities;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;
@bmchild
bmchild / initBinder.java
Created July 17, 2012 15:21
Useful Spring MVC InitBinders
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
// bind empty strings as null
binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
}
@bmchild
bmchild / method.java
Created July 18, 2012 16:49
Checks if any number of objects are null
/**
* checks through a bunch of objects for a null value
*
* @param o1
* @param objects
* @return true if any object is null, false otherwise
*/
public static boolean hasNullValue(Object o1, Object... objects) {
if(o1 == null) {
@bmchild
bmchild / ExpandableCustomDateEditor.java
Created July 26, 2012 21:06
SpringMVC ExpandableDateEditor for multiple dateformatters
/**
*
*/
package com.bmchild.spring;
import java.beans.PropertyEditorSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
@bmchild
bmchild / file.java
Created August 7, 2012 16:55
Foreign Key to a Compsite Primary Key in Hibernate
/**
*http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e2177
*/
@Entity
public class Parent implements Serializable {
@Id
public ParentPk id;
public int age;
@OneToMany(cascade=CascadeType.ALL)
@bmchild
bmchild / example1.java
Created August 30, 2012 12:23
Example Spring MVC 3 Exception Handlers
@Controller
public class SimpleController {
// other controller method omitted
@ExceptionHandler(IOException.class)
public String handleIOException(IOException ex, HttpServletRequest request) {
return ClassUtils.getShortName(ex.getClass());
}
}
@bmchild
bmchild / index.sql
Created September 13, 2012 16:25
Sql Server 2008 index
CREATE UNIQUE NONCLUSTERED INDEX my_index_name ON my_table (my_column) where my_column = 'Y';
@ElementCollection
@CollectionTable(name = "my_other_table", joinColumns = @JoinColumn(name = "fk_column"))
@Column(name = "my_column")
private List<String> strings;
@bmchild
bmchild / NotEquals.java
Created September 14, 2012 20:02
NotEquals Validator
/**
*
*/
package com.bmchild.validation.constraints;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.CONSTRUCTOR;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;