在列表hibernate中未获取@id字段的空值

时间:2015-06-25 22:23:33

标签: java oracle hibernate

@Entity
@Table(name = "CONFIG")
public class ConfigBean implements java.io.Serializable {

    private String FEATURE;
    private String SUB_FEATURE;
    private String STATUS;
    private String COMMENTS;
    private String HIERARCHY;
    private String SEARCHCRITERIA;
    private String RULE;
    private String CHECK1;
    private String CHECK2;
    private String CHECK3;
    private String CHECK4;
    private String CHECK5;

    public ConfigBean() {
    }

    public ConfigBean(String FEATURE, String SUB_FEATURE, String STATUS,
        String COMMENTS, String HIERARCHY, String SEARCHCRITERIA,
        String RULE, String CHECK1, String CHECK2, String CHECK3,
        String CHECK4, String CHECK5) {
        this.FEATURE = FEATURE;
        this.SUB_FEATURE = SUB_FEATURE;
        this.STATUS = STATUS;
        this.COMMENTS = COMMENTS;
        this.HIERARCHY = HIERARCHY;
        this.SEARCHCRITERIA = SEARCHCRITERIA;
        this.RULE = RULE;
        this.CHECK1 = CHECK1;
        this.CHECK2 = CHECK2;
        this.CHECK3 = CHECK3;
        this.CHECK4 = CHECK4;
        this.CHECK5 = CHECK5;
}

@Id
@Column(name = "FEATURE" ,  nullable = true)
public String getFEATURE() {
    return FEATURE;
}

public void setFEATURE(String fEATURE) {
    FEATURE = fEATURE;
}

@Column(name = "SUB_FEATURE")
public String getSUB_FEATURE() {
    return SUB_FEATURE;
}

public void setSUB_FEATURE(String sUB_FEATURE) {
    SUB_FEATURE = sUB_FEATURE;
}

@Column(name = "STATUS")
public String getSTATUS() {
    return STATUS;
}

public void setSTATUS(String sTATUS) {
    STATUS = sTATUS;
}

@Column(name = "COMMENTS")
public String getCOMMENTS() {
    return COMMENTS;
}

public void setCOMMENTS(String cOMMENTS) {
    COMMENTS = cOMMENTS;
}

@Column(name = "HIERARCHY")
public String getHIERARCHY() {
    return HIERARCHY;
}

public void setHIERARCHY(String hIERARCHY) {
    HIERARCHY = hIERARCHY;
}

@Column(name = "SEARCHCRITERIA")
public String getSEARCHCRITERIA() {
    return SEARCHCRITERIA;
}

public void setSEARCHCRITERIA(String sEARCHCRITERIA) {
    SEARCHCRITERIA = sEARCHCRITERIA;
}

@Column(name = "RULE")
public String getRULE() {
    return RULE;
}

public void setRULE(String rULE) {
    RULE = rULE;
}

@Column(name = "CHECK1")
public String getCHECK1() {
    return CHECK1;
}

public void setCHECK1(String cHECK1) {
    CHECK1 = cHECK1;
}

@Column(name = "CHECK2")
public String getCHECK2() {
    return CHECK2;
}

public void setCHECK2(String cHECK2) {
    CHECK2 = cHECK2;
}

@Column(name = "CHECK3")
public String getCHECK3() {
    return CHECK3;
}

public void setCHECK3(String cHECK3) {
    CHECK3 = cHECK3;
}

@Column(name = "CHECK4")
public String getCHECK4() {
    return CHECK4;
}

public void setCHECK4(String cHECK4) {
    CHECK4 = cHECK4;
}

@Column(name = "CHECK5")
public String getCHECK5() {
    return CHECK5;
}

public void setCHECK5(String cHECK5) {
    CHECK5 = cHECK5;
}

}

上面是我的映射类。 Feature列在数据库中有许多空值。

所以我得到了 enter image description here

我希望获取所有值[0..9]而不是[0,1,5,9]

下面是提取代码

public List<ConfigBean> fetchSearchCriteria() throws SQLException {
    criteriaDAO = new ConfigSearchCriteriaDAO();
    // get the checks to perform on the config file.
    List<ConfigBean> searchCriteriaList = criteriaDAO
            .selectRecordsTable("from ConfigBean");
    return searchCriteriaList;
}

public List selectRecordsTable(String queryString) {

    Session session = HibernateUtil.getSessionFactory().openSession();
    session.beginTransaction();
    Query query = session.createQuery(queryString);
    query.setMaxResults(10);
    List configList = query.list();
    session.getTransaction().commit();

    return configList;
}

从数据库中获取的列表忽略了@ID FEATURE值,因为其中一些值为null。

@id列是否必须为空? 或者还有其他方法来获取包含空值的行吗?

1 个答案:

答案 0 :(得分:1)

您在此处从基础 // convert Bitmap to String public static String BitMapToString(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); byte[] arr = baos.toByteArray(); imageData = Base64.encodeToString(arr, Base64.DEFAULT); return imageData; } // Convert String to Bitmap public static Bitmap StringToBitMap(String image) { try { byte[] encodeByte = Base64.decode(image, Base64.DEFAULT); Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length); return bitmap; } catch (Exception e) { e.getMessage(); return null; } } 表中获取数据,并使用@Id注释FEATURE;即要求hiberanate仅获取唯一记录。

您无法将null区分为“唯一”值,导致您的列表仅获取有效的唯一值。

只是为了检查 -

  1. 尝试在CONFIG列只有2个值在多个位置重复;您的列表中只会得到2个不同的值。

  2. 尝试通过java将值插入FEATURE,使用其中一个值为null;你将获得独特的约束声音异常。