UserType _2

<class name="RecordLog"> 
    <id name="id"> 
        <generator class="native"/> 
    </id> 
       <property name="description"/> 
       <property name="interval" type="TimeIntervalType"> 
           <column name="LOWER_LIMIT"/> 
           <column name="INCLUDES_LOWER_LIMIT"/> 
           <column name="UPPER_LIMIT"/> 
           <column name="INCLUDES_UPPER_LIMIT"/> 
       </property> 
</class> 


public class RecordLog extends Entity {  
    private String description;  
    private TimeInterval interval;  
    //getters and setters......  
}

<class name="RecordLog"> 
    <id name="id"> 
        <generator class="native"/> 
    </id> 
       <property name="description"/> 
       <property name="interval" type="TimeIntervalType"> 
           <column name="LOWER_LIMIT"/> 
           <column name="INCLUDES_LOWER_LIMIT"/> 
           <column name="UPPER_LIMIT"/> 
           <column name="INCLUDES_UPPER_LIMIT"/> 
       </property> 
</class> 

assertEquals(nov02, loaded.getInterval().lowerLimit());  
    assertEquals(nov03, loaded.getInterval().upperLimit());  

public class TimeIntervalType implements UserType {

private static final int[] SQL_TYPES = new int[] { Hibernate.TIMESTAMP.sqlType(), Hibernate.BOOLEAN.sqlType(), Hibernate.TIMESTAMP.sqlType(),
Hibernate.BOOLEAN.sqlType() };

public int[] sqlTypes() {
return SQL_TYPES;
}

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

public boolean equals(Object x, Object y) throws HibernateException {
if (x == y)
return true;
if (x == null || y == null)
return false;
return ((TimeInterval) x).compareTo((TimeInterval) y) == 0;
}

public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
TimePoint lower = TimePoint.from((Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[0]));
boolean lowerIncluded = ((Boolean) Hibernate.BOOLEAN.nullSafeGet(rs, names[1])).booleanValue();
TimePoint upper = TimePoint.from((Timestamp) Hibernate.TIMESTAMP.nullSafeGet(rs, names[2]));
boolean upperIncluded = ((Boolean) Hibernate.BOOLEAN.nullSafeGet(rs, names[3])).booleanValue();
return new TimeInterval(lower, lowerIncluded, upper, upperIncluded);
}

public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
TimeInterval interval = (TimeInterval) value;
Hibernate.TIMESTAMP.nullSafeSet(st, new Timestamp(((TimePoint) interval.lowerLimit()).asJavaUtilDate().getTime()), index);
Hibernate.BOOLEAN.nullSafeSet(st, new Boolean(interval.includesLowerLimit()), index + 1);
Hibernate.TIMESTAMP.nullSafeSet(st, new Timestamp(((TimePoint) interval.upperLimit()).asJavaUtilDate().getTime()), index + 2);
Hibernate.BOOLEAN.nullSafeSet(st, new Boolean(interval.includesUpperLimit()), index + 3);
}

public Object deepCopy(Object value) throws HibernateException {
if (value == null)
return null;
TimeInterval interval = (TimeInterval) value;
return new TimeInterval((TimePoint) interval.lowerLimit(), interval.includesLowerLimit(), (TimePoint) interval.upperLimit(), interval
.includesUpperLimit());
}

public boolean isMutable() {
return true;
}

}

你可能感兴趣的:(sql,Hibernate)