Entity注释
@Entity
@BatchSize(size = 5) //批处理
@Where(clause = "1=1") //默认查询条件
@FilterDef(name = "minLength", parameters = ...{@ParamDef(name = "minLength", type = "integer")}) //过滤器
@Filters(...{
@Filter(name = "betweenLength"),
@Filter(name = "minLength", condition = ":minLength <= length")
})
@org.hibernate.annotations.Table(appliesTo = "Forest",
indexes = ...{@Index(name = "idx", columnNames = ...{"name", "length"})}) //建立索引
@Proxy(lazy = false) //延迟加载
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY) //cache
public class ...
过滤器注释的使用
s.enableFilter( "betweenLength" ).setParameter( "minLength", 5 ).setParameter( "maxLength", 50 );
count = ( (Long) s.createQuery( "select count(*) from Forest" ).iterate().next() ).intValue();
assertEquals( 1, count );
s.disableFilter( "betweenLength" );
s.enableFilter( "minLength" ).setParameter( "minLength", 5 );
count = ( (Long) s.createQuery( "select count(*) from Forest" ).iterate().next() ).longValue();
assertEquals( 2l, count );
s.disableFilter( "minLength" );
Entity注释主要是添加在class的上面
Type注释
@Type(type = "text")
public String getLongDescription() ...{
return longDescription;
}
@Type(type = "caster") //取值时转为小写
public String getSmallText() ...{
return smallText;
}
@Type(type = "caster", parameters = ...{@Parameter(name = "cast", value = "upper")}) //取值时转为大写
public String getBigText() ...{
return bigText;
}
@Lob //二进制字段
public Country getCountry() ...{
return country;
}
@Formula("maxAltitude * 1000") //公式(数据库中并不存在这个字段)
public long getMaxAltitudeInMilimeter() ...{
return maxAltitudeInMilimeter;
}
@Type(type = "org.hibernate.test.annotations.entity.MonetaryAmountUserType") //CompositeType
@Columns(columns = ...{
@Column(name = "r_amount"), //数据库字段
@Column(name = "r_currency") //数据库字段
})
CompositeType实现类
//$Id: MonetaryAmountUserType.java 11282 2007-03-14 22:05:59Z epbernard $
package org.hibernate.test.annotations.entity;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Currency;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
/** *//**
* @author Emmanuel Bernard
*/
public class MonetaryAmountUserType implements CompositeUserType ...{
public String[] getPropertyNames() ...{
return new String[]...{"amount", "currency"};
}
public Type[] getPropertyTypes() ...{
return new Type[]...{Hibernate.BIG_DECIMAL, Hibernate.CURRENCY};
}
public Object getPropertyValue(Object component, int property) throws HibernateException ...{
MonetaryAmount ma = (MonetaryAmount) component;
return property == 0 ? (Object) ma.getAmount() : (Object) ma.getCurrency();
}
public void setPropertyValue(Object component, int property, Object value)
throws HibernateException ...{
MonetaryAmount ma = (MonetaryAmount) component;
if ( property == 0 ) ...{
ma.setAmount( (BigDecimal) value );
}
else ...{
ma.setCurrency( (Currency) value );
}
}
public Class returnedClass() ...{
return MonetaryAmount.class;
}
public boolean equals(Object x, Object y) throws HibernateException ...{
if ( x == y ) return true;
if ( x == null || y == null ) return false;
MonetaryAmount mx = (MonetaryAmount) x;
MonetaryAmount my = (MonetaryAmount) y;
return mx.getAmount().equals( my.getAmount() ) &&
mx.getCurrency().equals( my.getCurrency() );
}
public int hashCode(Object x) throws HibernateException ...{
return ( (MonetaryAmount) x ).getAmount().hashCode();
}
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
throws HibernateException, SQLException ...{
BigDecimal amt = (BigDecimal) Hibernate.BIG_DECIMAL.nullSafeGet( rs, names[0] );
Currency cur = (Currency) Hibernate.CURRENCY.nullSafeGet( rs, names[1] );
if ( amt == null ) return null;
return new MonetaryAmount( amt, cur );
}
public void nullSafeSet(
PreparedStatement st, Object value, int index,
SessionImplementor session
) throws HibernateException, SQLException ...{
MonetaryAmount ma = (MonetaryAmount) value;
BigDecimal amt = ma == null ? null : ma.getAmount();
Currency cur = ma == null ? null : ma.getCurrency();
Hibernate.BIG_DECIMAL.nullSafeSet( st, amt, index );
Hibernate.CURRENCY.nullSafeSet( st, cur, index + 1 );
}
public Object deepCopy(Object value) throws HibernateException ...{
MonetaryAmount ma = (MonetaryAmount) value;
return new MonetaryAmount( ma.getAmount(), ma.getCurrency() );
}
public boolean isMutable() ...{
return true;
}
public Serializable disassemble(Object value, SessionImplementor session)
throws HibernateException ...{
return (Serializable) deepCopy( value );
}
public Object assemble(Serializable cached, SessionImplementor session, Object owner)
throws HibernateException ...{
return deepCopy( cached );
}
public Object replace(Object original, Object target, SessionImplementor session, Object owner)
throws HibernateException ...{
return deepCopy( original ); //TODO: improve
}
}