使用映射类时的Hibernate标准

时间:2012-10-24 23:01:47

标签: java hibernate

我创建了一个对象,它在我的数据库中映射了两个表,Dictionary表和Token表。表示这两个表之间的连接的对象(类)称为DictionaryToken。

这是班级:

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
 import javax.persistence.Id;
import javax.persistence.OneToMany;
 import javax.persistence.Table;

import org.apache.log4j.Logger;


@Entity
@Table(name="dictionary", catalog="emscribedxcode")

public class DictionaryToken {     private static Logger LOG = Logger.getLogger(DictionaryToken.class);

private Long _seq;
private String _code;
private String _acute;
private String _gender;
private String _codeType;
private String _papplydate;
private String _capplydate;
private Long _tokenLength;
private List <TokenDictionary> _token;
private int _type;
private String _system;
private String _physicalsystem;
/*
 * type of 0 is a straight line insert type of 1 is a language dictionary
 * entyr type of 2 is a multiple token entry
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "seq")
public Long getSeq() {
    return _seq;
}
public void setSeq(Long seq_) {
    _seq = seq_;
}

@Column(name = "code")
public String getCode() {
    return _code;
}
public void setCode(String code_) {
    _code = code_;
}

@Column(name = "acute")
public String getAcute() {
    return _acute;
}
public void setAcute(String acute_) {
    _acute = acute_;
}

@Column(name = "gender")
public String getGender() {
    return _gender;
}
public void setGender(String gender_) {
    _gender = gender_;
}

@Column(name = "codetype")
public String getCodeType() {
    return _codeType;
}
public void setCodeType(String codeType_) {
    _codeType = codeType_;
}

@Column(name = "papplydate")
public String getPapplydate() {
    return _papplydate;
}
public void setPapplydate(String papplydate_) {
    _papplydate = papplydate_;
}

@Column(name = "capplydate")
public String getCapplydate() {
    return _capplydate;
}
public void setCapplydate(String capplydate_) {
    _capplydate = capplydate_;
}


@Column(name = "token_length")
public Long getTokenLength() {
    return _tokenLength;
}
public void setTokenLength(Long tokenLength_) {
    _tokenLength = tokenLength_;
}
@OneToMany (mappedBy = "dictionarytoken", targetEntity = TokenDictionary.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
public List<TokenDictionary> get_token() {
    return _token;
}
public void set_token(List<TokenDictionary> _token) {
    this._token = _token;
}
public void addToToken(TokenDictionary token){
    this._token.add(token);
}
@Column(name = "type")
public int getType() {
    return _type;
}
public void setType(int _type) {
    this._type = _type;
}
@Column(name = "physicalsystem")
public String get_physicalsystem() {
    return _physicalsystem;
}
public void set_physicalsystem(String _physicalsystem) {
    this._physicalsystem = _physicalsystem;
}
@Column(name = "codingsystem")
public String get_system() {
    return _system;
}
public void set_system(String _system) {
    this._system = _system;
}



   }

这是我的问题。我可以使用具有此对象的服务执行查询,没有问题,除非我添加标准。这是检索条目的方法

public List<DictionaryToken> getDictionaryTokenEntries(String system) {
    Session session = null;
    List<DictionaryToken> dictonaries = new ArrayList<DictionaryToken>();
    try {
        session = HibernateUtils.beginTransaction("emscribedxcode");
           session.createCriteria(Dictionary.class).addOrder(Order.desc("codeType"))

        Criteria criteria = session.createCriteria(DictionaryToken.class);
                  /*******THIS IS THE PROBLEM STATEMENT*************************/
        if (system != null) {
            criteria.add(Restrictions.eq("codingsystem", system));
        }
                  /****************************************************************/
        // dictonaries = criteria.list();
        Order order = Order.asc("seq");
        criteria.addOrder(order);
        dictonaries = criteria.list();
        System.out.println("Dictionaryentries = " + dictonaries.size());

        // System.out.println("Dictionaries entries EVICT start...");
        // for(Dictionary dic : dictonaries){
        // session.evict(dic);
        // }
        // System.out.println("Dictionaries entries EVICT end");
    } catch (HibernateException e_) {
        e_.printStackTrace();
        NTEVENT_LOG.error("Error while getting List of Dictionary entries");
    } finally {
        if (session != null && session.isOpen()) {
            try {
                HibernateUtils.closeSessions();
            } catch (HibernateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return dictonaries;
}

当我添加条件时,出现以下错误:

org.hibernate.QueryException:无法解析属性:编码系统:com.artificialmed.domain.dictionary.model.DictionaryToken

我知道它与对象的本质有关,它实际上是我的字典类与基础表和我的令牌类和表之间的连接。

字段编码系统是我的字典类中的一个字段。我想我想使用别名,但我不知道在当前情况下如何做到这一点。任何帮助将不胜感激。

埃利奥特

1 个答案:

答案 0 :(得分:0)

这是一个新手问题。 Hibernate要求反映表的模型的getter和setter具有特定格式。 getter必须得+其中name是底层表中的fieldname。必须设置setter +其中name是基础表的fieldname。是的,Name的第一个字母必须大写。