Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9080747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:08:43+00:00 2026-06-16T20:08:43+00:00

I need to store an object using Hibernate, but this object use an enum.

  • 0

I need to store an object using Hibernate, but this object use an enum. I can store, but when I tried to retrieve it again, this fails with this error: “Studies is not mapped [FROM Studies]”.
I tried with a lot of solutions in internet but nothing works. I use MySQL database

This is the enum:

public enum StudyStatus {

    Created("Created"), Started("Started"), Closed("Closed");

    private final String value;

    StudyStatus(String value){
        this.value = value;
    }



    public static StudyStatus fromValue(int value){
        for (StudyStatus status : values()) {  
            if (status.value.equals(value)) {  
                return status;  
            }  
        }  
        throw new IllegalArgumentException("Invalid status: " + value);  
    }

    public String toValue(){
        return value;
    }

}

This is the EnumUserType class

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

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.AbstractStandardBasicType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.StringType;
import org.hibernate.usertype.EnhancedUserType;
import org.hibernate.usertype.ParameterizedType;

public abstract class AbstractEnumUserType<E extends Enum<E>, V> implements
    EnhancedUserType, ParameterizedType {

public static int DEAFAULT_SQL_TYPE = Types.INTEGER;

private PreparedStatementSetter psSetter;

private AbstractStandardBasicType<?> basicType;

protected abstract Class<E> getEnumClass();

protected abstract Class<V> getValueClass();

protected abstract E convertEnum(V rawValue);

protected abstract V convertSqlValue(E enumValue);

protected int getSqlType() {
    int sqlType = Types.OTHER;
    switch (getValueClass().getName()) {
    case "java.lang.String":
        sqlType = Types.VARCHAR;
        break;
    case "java.lang.Integer":
        sqlType = Types.INTEGER;
        break;
    default:
        break;
    }

    return sqlType;
}

// ////////////////////////////

@Override
public int[] sqlTypes() {
    return new int[] { getSqlType() };
}

@Override
public Class<?> returnedClass() {
    return getEnumClass();
}

@Override
public boolean equals(Object x, Object y) throws HibernateException {
    return (x == y);
}

@Override
public int hashCode(Object x) throws HibernateException {
    return (x == null) ? 0 : x.hashCode();
}

@Override
public Object nullSafeGet(ResultSet rs, String[] names,
        SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
    Object rawValue = basicType.nullSafeGet(rs, names[0], session, owner);
    Object enumValue = (rawValue == null) ? null
            : convertEnum((V) rawValue);
    return enumValue;
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index,
        SessionImplementor session) throws HibernateException, SQLException {
    if (value == null) {
        st.setNull(index, Types.VARCHAR);
    } else {
        psSetter.set(st, convertSqlValue((E) value), index);
    }
}

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

@Override
public boolean isMutable() {
    return false;
}

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

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

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

@Override
public void setParameterValues(Properties parameters) {
    // Initialize Method
    initBasicType();
    initPreparedStatementSetter();
}

@Override
public String objectToSQLString(Object value) {
    return '\'' + ((Enum<?>) value).name() + '\'';
}

@Override
public String toXMLString(Object value) {
    return ((Enum<?>) value).name();
}

@Override
public Object fromXMLString(String xmlValue) {
    // TODO
    throw new IllegalAccessError();
    // return Enum.valueOf(, xmlValue);
}

protected void initBasicType() {

    switch (getSqlType()) {
    case Types.VARCHAR:
        basicType = StringType.INSTANCE;
        break;
    case Types.INTEGER:
        basicType = IntegerType.INSTANCE;
        break;
    default:
        break;
    }
}

protected void initPreparedStatementSetter() {
    // TODO
    switch (getSqlType()) {
    case Types.VARCHAR:
        psSetter = new StringPreparedStatementSetter();
        break;
    case Types.INTEGER:
        psSetter = new IntPreparedStatementSetter();
    default:
        break;
    }
}

private static interface PreparedStatementSetter {
    void set(PreparedStatement st, Object value, int index)
            throws SQLException;
}

private static class StringPreparedStatementSetter implements
        PreparedStatementSetter {
    @Override
    public void set(PreparedStatement st, Object value, int index) {
        try {
            st.setString(index, (String) value);
        } catch (SQLException e) {
        }
    }
}

private static class IntPreparedStatementSetter implements
        PreparedStatementSetter {
    @Override
    public void set(PreparedStatement st, Object value, int index) {
        try {
            st.setInt(index, (Integer) value);
        } catch (SQLException e) {
        }
    }
}

}

The class with the enum

import java.util.ArrayList;

import ateam.capi.common.enums.StudyStatus;

public class Study {

private String id;
private String name;
private StudyStatus status;
private ArrayList<User> pollsters;
private Questionnaire actualQuestionnaire;

public Questionnaire getActualQuestionnaire() {
    return actualQuestionnaire;
}

public void setActualQuestionnaire(Questionnaire actualQuestionnaire) {
    this.actualQuestionnaire = actualQuestionnaire;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public StudyStatus getStatus() {
    return status;
}

public void setStatus(StudyStatus status) {
    this.status = status;
}

public ArrayList<User> getPollsters() {
    return pollsters;
}

public void setPollsters(ArrayList<User> pollsters) {
    this.pollsters = pollsters;
}

}

This is the XML to map the Study class

<hibernate-mapping package="ateam.capi.common.beans">
    <class name="Study" table="Studies">
        <id name="id" column="id"></id>
        <property name="name"/>
        <property name="status">
             <type name="ateam.capi.capipersistence.utils.EnumUserType">
                <param name="enumClassName">
                    ateam.capi.common.enums.StudyStatus
                </param>
             </type>
        </property>
    </class>
</hibernate-mapping>

Study DAO class

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ateam.capi.capipersistence.utils.HibernateUtil;
import ateam.capi.common.beans.Questionnaire;
import ateam.capi.common.beans.Study;

public class DAO_Study {

private Session session;
private Transaction tx;

public void saveStudy(Study study) throws HibernateException{
    try{
        initOperations();
        session.save(study);
        tx.commit();
    } catch (HibernateException ex){
        handleException(ex);
        throw ex;
    } finally{
        if (session!=null){
            session.close();
        }
    }
}

public void deleteStudy(Study study) throws HibernateException{ 
    try{ 
        initOperations(); 
        this.session.delete(study); 
        this.tx.commit(); 
    } catch (HibernateException ex){ 
        handleException(ex); 
        throw ex; 
    } finally{ 
        if (session!=null){
            session.close();
        } 
    }
}

public List<Study> getStudiesList() throws HibernateException{ 
    List<Study> studiesList = null;  
    try{ 
        initOperations(); 
        String hql = "FROM Studies";
        Query query = session.createQuery(hql);
        studiesList = query.list();
    } catch (HibernateException ex){ 
        handleException(ex); 
        throw ex; 
    } finally{ 
        if (session!=null){
            session.close();
        } 
    } 
    return studiesList; 
}

private void initOperations() throws HibernateException{
    HibernateUtil.createSession();
    this.session = HibernateUtil.getSessionFactory().openSession();
    this.tx = this.session.beginTransaction();
}

private void handleException(HibernateException ex) throws HibernateException{
    this.tx.rollback();
    System.out.println(ex.getStackTrace());
    throw ex;
}

}

I use Java7 with hibernate 4.1.8, I found other solutions but dont work in java7

Any Idea?
Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:08:44+00:00Added an answer on June 16, 2026 at 8:08 pm

    Shouldn’t your query look like from study instead of from studies? Studies is the table not the defined entity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to store this object into the internal storage memory of the phone,
I have a program in which I need to store a Class object into
I need to store the user who is creating a specific object in the
I´ve a Javascript object with all the data that I need to store on
I need to create a class that will store a unique object elements. I
What i need to do, is to store a preference stucture in an object.
User Story: Action for Facebook that has open graph object. For this I need
I need store just 10 arrays in my app, which I can change from
I imagine this pertains to Hibernate only (I'm just now beginning to use these
I've been using Hibernate to store a parent-child relationship using @OneToMany with an @JoinColumn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.