2017-01-10 8 views
0

をキャストすることができ:ない私は、このスキーマを持つMySQLのテーブルを持っているJavaのエンティティにHQLクエリーから

 Field  | Type   | Null | Key | Default | Extra   | 
+-------------+--------------+------+-----+---------+----------------+ 
| id   | int(11)  | NO | PRI | NULL | auto_increment | 
| keyword  | varchar(100) | NO |  | NULL |    | 
| synonyms | varchar(500) | NO |  | NULL |    | 
| replacewith | tinyint(1) | YES |  | 0  |    | 
| isProtected | tinyint(1) | YES |  | 0  |    | 
| combined | tinyint(1) | YES |  | 0  |    | 
| stopword | tinyint(1) | YES |  | 0  |    | 
| oneway  | tinyint(1) | YES |  | 0  |    | 

今、私は次のようにそれのためのエンティティクラスを持っている:

@Entity 
@Table(name = "keyword_configuration", catalog = "search") 
public class KeywordConfiguration implements Serializable { 

    private static final long serialVersionUID = -3090932150501117578L; 
    private Integer id; 
    private String keyword; 
    private String synonymns; 
    private boolean replacewith; 
    private boolean isProtected; 
    private boolean combined; 
    private boolean stopword; 
    private boolean oneway; 

    @Id 
    @GeneratedValue(strategy = IDENTITY) 
    @Column(name = "id", unique = true, nullable = false) 
    public Integer getId() { 
     return id; 
    } 

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

    @Column(name = "keyword") 
    public String getKeyword() { 
     return keyword; 
    } 

    public void setKeyword(String keyword) { 
     this.keyword = keyword; 
    } 

    @Column(name = "synonyms") 
    public String getSynonymns() { 
     return synonymns; 
    } 

    public void setSynonymns(String synonymns) { 
     this.synonymns = synonymns; 
    } 

    @Column(name = "replacewith") 
    public boolean isReplacewith() { 
     return replacewith; 
    } 

    public void setReplacewith(boolean replacewith) { 
     this.replacewith = replacewith; 
    } 
    @Column(name = "isprotected") 
    public boolean isProtected() { 
     return isProtected; 
    } 

    public void setProtected(boolean isProtected) { 
     this.isProtected = isProtected; 
    } 

    @Column(name = "combined") 
    public boolean isCombined() { 
     return combined; 
    } 

    public void setCombined(boolean combined) { 
     this.combined = combined; 
    } 

    @Column(name = "stopword") 
    public boolean isStopword() { 
     return stopword; 
    } 

    public void setStopword(boolean stopword) { 
     this.stopword = stopword; 
    } 

    @Column(name = "oneway") 
    public boolean isOneway() { 
     return oneway; 
    } 

    public void setOneway(boolean oneway) { 
     this.oneway = oneway; 
    } 
} 
結果をフェッチするために私のHQLクエリは以下の通りです

:私は、クエリリストのサイズを印刷するとき

@Override 
@Transactional 
public List<KeywordConfigurationDTO> getConfigurationDetails(String keyword) { 
    String sQuery = " FROM " + KeywordConfiguration.class.getSimpleName() 
      + " WHERE keyword = :word"; 
    Query query = sessionFactory.getCurrentSession().createQuery(sQuery); 
    query.setParameter("word", keyword); 
    //System.out.println(query.toString()); 
    System.out.println(query.list().size() + "SIZE"); 
    return (List<KeywordConfigurationDTO>)query.list(); 
} 

今では右resulを与えますしかし、私がList<KeywordConfiguration>のオブジェクトにアクセスしようとすると、エラーを出すことなくアクセスしません。デバッグすることで、正しいオブジェクトがリストにあることがわかります。なぜこうなった?

+0

使用しているHibernateのバージョンは何ですか? – ujulu

答えて

0

KeywordConfigurationの代わりにKeywordConfigurationDTOを使用することはできません。だからあなたの方法はこのようなものになるでしょう。

@Override 
@Transactional 
public List<KeywordConfigurationDTO> getConfigurationDetails(String keyword) { 
    String sQuery = " FROM " + KeywordConfiguration.class.getSimpleName() 
      + " WHERE keyword = :word"; 
    Query query = sessionFactory.getCurrentSession().createQuery(sQuery); 
    query.setParameter("word", keyword); 
    //System.out.println(query.toString()); 
    System.out.println(query.list().size() + "SIZE"); 
    List<KeywordConfiguration> kcList = query.list(); 

    // You have to implement convertKcToKCDtoLit method to convert KeywordConfiguration to KeywordConfigurationDTO 
    List<KeywordConfigurationDTO> kcDtoList = convertKcToKCDtoLit(kcList); 

    return kcDtoList; 
} 
関連する問題