Showing posts with label Hibernate. Show all posts
Showing posts with label Hibernate. Show all posts

Friday, May 8, 2009

Hibernate Custom Type

Database has for example wrong state and can't save 1 or 0, 'Y' or 'N', 'YES' or 'NO'
So you need to save state as 1 and 2. Seems that database need refactoring... yes you are right but if you can't? Here is decision - custom type

package com.types;

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

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

public class OneTwoBooleanType implements UserType{

private final static int DB_TRUE = 1;
private final static int DB_FALSE = 2;

public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
}

public Object deepCopy(Object value) throws HibernateException {
return value;
}

public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
}

public boolean equals(Object x, Object y) throws HibernateException {
if(x==null){
return false;
}
return ((Boolean)x).equals(y);
}

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

public boolean isMutable() {
return false;
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
Number val = (Number) rs.getObject(names[0]);
if(val==null){
return false;
}
return val.intValue() == DB_TRUE ? true : false;
}

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
int dbValue;
if(value==null){
dbValue = DB_FALSE;
} else {
dbValue = (Boolean) value ? DB_TRUE : DB_FALSE;
}
st.setInt(index, dbValue);
}

public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}

public Class returnedClass() {
return Boolean.class;
}

public int[] sqlTypes() {
return new int[]{Types.SMALLINT};
}

}

And It's usage

@Column(name="INTERNAL_STATUS_ID")
@Type(type="com.types.OneTwoBooleanType")
private boolean isVisible;

Thursday, January 15, 2009

Hibernate Filters

Task:
Show feed executors with specified date



Libraries:


org.hibernate
hibernate-annotations
3.3.1.GA


org.hibernate
hibernate-commons-annotations
3.3.0.ga



So Feed Entity:

@Entity(name="FEED")
@FilterDef(name="status", parameters=@ParamDef( name="date", type="date" ) )
public class Feed {

@Id
@Column(name="FEED_ID")
private int id;

@Column(name="NAME")
private String name;

@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name = "FEED_ID")
@BatchSize(size=50)
@Fetch(FetchMode.SELECT)
@Filter(name="status", condition="BUSINESS_DATE=(select max(x.business_date) from FEED_RUN x where x.BUSINESS_DATE <= :date AND FEED_ID=x.FEED_ID)")
private List feedExecution;
}

Feed Runner Entity

@Entity(name = "FEED_RUN")
@Where(clause = "STATUS_ID <> 4")
public class FeedExecution {

@Id
@Column(name = "FEED_RUN_ID")
private int id;

@Column(name = "BUSINESS_DATE")
@Temporal(TemporalType.DATE)
private Date businessDate;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "STATUS_ID")
private Status status;

@Formula("decode((SELECT COUNT(*) FROM file_load fl WHERE feed_run_id = fl.feed_run_id)+(SELECT COUNT(*) FROM event_log ev WHERE feed_run_id = ev.feed_run_id)+(SELECT COUNT(*) FROM error_log er WHERE feed_run_id = er.feed_run_id), 0, 0, 1)")
private boolean executed;

}