对象枚举返回始终为空值

时间:2016-01-27 19:56:48

标签: java hibernate postgresql enums

return (List<TypeActionCommerciale>) query.list();

当我选择全部时,我不明白为什么列表中的变量type总是null enter image description here

我的PostgreSql表: enter image description here

HibernateDaoFactory:

public class HibernateDaoFactory extends DaoFactory {
    public HibernateDaoFactory() {
        super();
    }
...
    @Override
    public TypeActionCommercialeDAO getTypeActionCommercialeDAO() {
        return new HibernateTypeActionCommercialeDAO();
    }
...

HibernateTypeActionCommercialeDAO:

public class HibernateTypeActionCommercialeDAO extends HibernateDAO implements TypeActionCommercialeDAO {

    public List<TypeActionCommerciale> requestAll() throws PersistenceException {
        Query query = createQuery("from TypeActionCommercialeImpl");
        return (List<TypeActionCommerciale>) query.list();
    }
...

TypeActionCommercialeDAO:

public interface TypeActionCommercialeDAO extends DAO {
    public List<TypeActionCommerciale> requestAll() throws PersistenceException;
...

TypeActionCommerciale:

public interface TypeActionCommerciale {

    public Integer getId();
    public void setId(Integer id);
    public Integer getVersion();
    public void setVersion(Integer id);

    public String getLibelle();
    public void setLibelle(String libelle);

    public String getCode();
    public void setCode(String code);

    public TypeActionCommercialeEnum getType();
    public void setType(TypeActionCommercialeEnum type);

    public List<DetailActionCommerciale> getValeurParDefaut();
    public void setValeurParDefaut(List<DetailActionCommerciale> valeurParDefaut);
    public void addToValeurParDefaut(DetailActionCommerciale valeurParDefaut);



    // ----------------
    // OPERATIONS
    // ----------------

    public boolean isPPFCP();
    public boolean isAutresServices();

}

TypeActionCommercialeEnum:

public enum TypeActionCommercialeEnum {
    OPERATION_DE_MISE_EN_AVANT, OPERATIONS_PARTICULIERES_DE_COMMUNICATION_ET_DE_TRADE_MARKETING, INFORMATION_DES_CLIENTS_ET_OU_PUBLICITE_SUR_LE_LIEU_DE_VENTE, OPERATIONS_DE_LANCEMENT_DE_NOUVEAUX_PRODUITS_AVEC_MISE_EN_AVANT, PUBLICITE_DIRECTE_CONSOMMATEURS, CONVENTION_TRIANNUELLE, MISE_A_DISPOSITION_DU_LABO, NIP;
}

TypeActionCommercialeImpl:

class TypeActionCommercialeImpl implements TypeActionCommerciale {

    // ----------------
    // ATTRIBUTES
    // ----------------

    private Integer id;
    private Integer version;

    /**  */
    private String libelle;
    /**  */
    private String code;
    /**  */
    private TypeActionCommercialeEnum type;

    // ----------------
    // ASSOCIATIONS
    // ----------------
    private List<DetailActionCommerciale> valeurParDefaut = (List<DetailActionCommerciale>)new ArrayList<DetailActionCommerciale>();





    // ----------------
    // ACCESSORS
    // ----------------

    public Integer getId() {
        return id;
    }

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

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public String getLibelle() {
        return libelle;
    }
    public void setLibelle(String libelle) {
        this.libelle = libelle;
    }

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }

    public TypeActionCommercialeEnum getType() {
        return type;
    }
    public void setType(TypeActionCommercialeEnum type) {
        this.type = type;
    }

    // ----------------
    // ASSOCIATIONS ACCESSORS
    // ----------------

    public List<DetailActionCommerciale> getValeurParDefaut() {
        if (valeurParDefaut == null) {
            valeurParDefaut = new ArrayList<DetailActionCommerciale>();
        }
        return valeurParDefaut;
    }


    public void setValeurParDefaut(List<DetailActionCommerciale> valeurParDefaut) {
        this.valeurParDefaut = valeurParDefaut;
    }

    public void addToValeurParDefaut(DetailActionCommerciale valeurParDefaut) {
        if (valeurParDefaut != null)
            getValeurParDefaut().add(valeurParDefaut);
    }






    // ----------------
    // OPERATIONS
    // ----------------


    // Start of user code for TypeActionCommercialeImpl
    public boolean isAutresServices() {
        switch (this.getType()) {
            case NIP:
            case CONVENTION_TRIANNUELLE:
            case MISE_A_DISPOSITION_DU_LABO:
                return true;
        }
        return false;
    }

    public boolean isPPFCP() {
        return !isAutresServices();
    }
    // End of user code for TypeActionCommercialeImpl


    @Override
    public String toString() {
        String res =  "TypeActionCommerciale [ID=" + getId() + "]";
        // Start of user code toString()
        // End of user code toString()
        return res;
    }

}

映射:

<many-to-one name="typeActionCommerciale" class="com.app.metier.impl.TypeActionCommercialeImpl" column="TYPEACTIONCOMMERCIALEID" access="field"  cascade="persist,merge,save-update" />

<class name="com.app.metier.impl.TypeActionCommercialeImpl"
    table="typeActionCommerciale" discriminator-value="com.app.metier.TypeActionCommerciale"
    proxy="com.app.metier.TypeActionCommerciale"
    optimistic-lock="version" dynamic-update="true" dynamic-insert="true">
    <id name="id" column="id">
        <generator class="native" />
    </id>
    <discriminator column="discriminator" type="string" />
    <!-- <version name="version" type="integer" unsaved-value="null"></version> -->

    <property name="libelle" length="255" />
    <property name="code" length="255" />
    <property name="type"
        type="com.app.persistence.hibernate.userType.TypeActionCommercialeEnumUserType" />

    <list name="ValeurParDefaut" cascade="all" lazy="true">
        <key column="VALEURPARDEFAUT_TYPEACTIONCOMMERCIALEID" />
        <index column="List_Index_ValeurParDefaut" />
        <one-to-many
            class="com.app.metier.impl.DetailActionCommercialeImpl" />
    </list>
</class>

TypeActionCommercialeEnumUserType:

public class TypeActionCommercialeEnumUserType extends EnumUserType<TypeActionCommercialeEnum> {
    public TypeActionCommercialeEnumUserType() {
        super(TypeActionCommercialeEnum.class);
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了我的pb我有一个班级public class EnumUserType<E extends Enum<E>> implements UserType,我实现了这个方法:

@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner)
        throws HibernateException, SQLException {
     String name = rs.getString(names[0]);  
        Object result = null;  
        if (!rs.wasNull()) {  
            result = Enum.valueOf(clazz, name);  
        }  
        return result;
}

@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session)
        throws HibernateException, SQLException {
    if (null == value) {  
        st.setNull(index, Types.VARCHAR);  
    } else {  
        st.setString(index, ((Enum<E>) value).name());  
    }  

}