Hello good fellas.I dont' really know whether i should post here or in springframework forum.Here is the problem.
i have all my class inheriting from one class and use union-subclass in all my mapping and all works fine so far.So i was trying to map java enum to a varchar field suggested by the book
Quote:
Manning Java persistence 2nd edition
and use an helper class SpringEnumUserType that implements both EnhancedUserType and ParametirizedType interfaces. So after i added my typedef element and build i have then this error
Quote:
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Cannot resolve reference to bean 'transactionManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [META-INF/myconfig.xml]: Cannot resolve reference to bean 'sessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [META-INF/myconfig.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
here is the mapping file
Code:
<hibernate-mapping package="com.projects.tryprojects.model">
<union-subclass extends="SuperEntityImpl" name="MarkRequestImpl" table="Mark">
<property name="student_name"/>
<property name="subject"/>
<property name="program"/>
<!--<property name="exam_type" type="exam_type"/>-->
<typedef class="com.projects.tryprojects.util.StringEnumUserType" name="distinction">
<param name="enumClassname">com.projects.tryprojects.model.Distinction</param>
</typedef>
<!--<typedef class="com.projects.tryprojects.util.StringEnumUserType" name="exam_type">
<param name="enumClassname">com.projects.tryprojects.model.ExamType</param>
</typedef>-->
<property name="distinction" type="disctinction"/>
</union-subclass>
</hibernate-mapping>
i commented this one of if because i thought i cannot declare 2 in the same file.
StringEnumUserType from the book is like so
Code:
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.hibernate.*;
import org.hibernate.util.ReflectHelper;
import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.ParameterizedType;
/**
* A generic UserType that handles String-based JDK 5.0 Enums.
*
* @author Gavin King
*/
public class StringEnumUserType implements EnhancedUserType, ParameterizedType {
private Class<Enum> enumClass;
public void setParameterValues(Properties parameters) {
String enumClassName = parameters.getProperty("enumClassname");
try {
enumClass = ReflectHelper.classForName(enumClassName);
}
catch (ClassNotFoundException cnfe) {
throw new HibernateException("Enum class not found", cnfe);
}
}
public Class returnedClass() {
return enumClass;
}
public int[] sqlTypes() {
return new int[] { Hibernate.STRING.sqlType() };
}
public boolean isMutable() {
return false;
}
public Object deepCopy(Object value) {
return value;
}
public Serializable disassemble(Object value) {
return (Enum) value;
}
public Object replace(Object original, Object target, Object owner) {
return original;
}
public Object assemble(Serializable cached, Object owner) {
return cached;
}
public boolean equals(Object x, Object y) {
return x==y;
}
public int hashCode(Object x) {
return x.hashCode();
}
public Object fromXMLString(String xmlValue) {
return Enum.valueOf(enumClass, xmlValue);
}
public String objectToSQLString(Object value) {
return '\'' + ( (Enum) value ).name() + '\'';
}
public String toXMLString(Object value) {
return ( (Enum) value ).name();
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws SQLException {
String name = rs.getString( names[0] );
return rs.wasNull() ? null : Enum.valueOf(enumClass, name);
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws SQLException {
if (value==null) {
st.setNull(index, Hibernate.STRING.sqlType());
}
else {
st.setString( index, ( (Enum) value ).name() );
}
}
}
my enums are just
Code:
public enum Distinction {POOR, AVERAGE,GOOD,EXCELENT}
same thing for the ExamType
So good people what i'm not doing right? and how to solve this.thanks a lot for reading