2017-04-17 51 views
1

我是Hibernate中的新成員。我成功檢索單列和完整的表,但是當我嘗試在表中的特定列它給我錯誤。 以下是錯誤休眠:無法檢索多個字段值

[Ljava.lang.Object;@2c7d121c, [Ljava.lang.Object;@65aa6596, [Ljava.lang.Object;@67389cb8, [Ljava.lang.Object;@419a20a6 

這裏是實體:MovieRatings.java

package com.hibernate.entity; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.Table; 

@Entity 
@Table(name = "movie_ratings") 
public class MovieRatings { 
    @Id 
    @GeneratedValue(strategy=GenerationType.IDENTITY) 
    @Column(name = "id") 
    private int id; 
    @Column(name = "movie_id") 
    private int user_id; 
    @Column(name = "user_id") 
    private int movie_id; 
    @Column(name = "rating") 
    private int rating; 

    public MovieRatings() 
    { 

    } 

    public int getId() { 
     return id; 
    } 

    public void setId(int id) { 
     this.id = id; 
    } 
    public int getUser_id() { 
     return user_id; 
    } 
    public void setUser_id(int user_id) { 
     this.user_id = user_id; 
    } 

    public int getMovie_id() { 
     return movie_id; 
    } 

    public void setMovie_id(int movie_id) { 
     this.movie_id = movie_id; 
    } 

    public int getRating() { 
     return rating; 
    } 

    public void setRating(int rating) { 
     this.rating = rating; 
    } 

    @Override 
    public String toString() { 
     return "MovieRatings [id=" + id + ", user_id=" + user_id + ", movie_id=" + movie_id + ", rating=" + rating 
       + "]"; 
    } 

} 

這裏的主要功能是

package com.hibernate.getdata; 
import java.util.List; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import com.hibernate.entity.MovieRatings;; 

public class GetMovieInfo { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     SessionFactory factory = new Configuration().configure().addAnnotatedClass(MovieRatings.class).buildSessionFactory(); 
     Session session = factory.getCurrentSession(); 
     try { 
      session.beginTransaction(); 
      String hql="select E.user_id, E.rating from MovieRatings as E"; 
      System.out.println(hql); 
      @SuppressWarnings({ "deprecation", "unchecked" })   
      List<MovieRatings> theRatings = session.createQuery(hql).list(); 
      System.out.println(theRatings); 
      /*for(MovieRatings tempRatings:theRatings) 
      { 
       System.out.println(tempRatings.getUser_id()); 
       System.out.println(tempRatings.getRating()); 
      }*/ 
      session.getTransaction().commit(); 
     } catch (Exception e) { 
      // TODO: handle exception 
     } 

    } 

}

+0

這是不是一個錯誤,但陣列輸出。 – davidxxx

回答

2

我成功地檢索單列和完整的表,但wh en 有史以來我想在表中的特定列它給我錯誤。

使用Hibernate(真還爲JPA),當您在查詢中特定的列(S),而不是實體本身選擇的Querylist()方法返回與此結構的對象:List<Object[]>

列表中的每個元素對應一行,每個元素是一個數組,其中包含select使用的順序中的所選值。
所以,你必須遍歷列表中檢索值:

List<Object[]> result = (List<Object[]>) query.list(); 
for(Object[] current : result){ 
    Integer userId = (String)current[0]; 
    Float rating = (Float) current[1]; 
    ..... 
} 
0

嘗試使用這樣的:

try { 
      session.beginTransaction(); 
      String hql="select E.user_id, E.rating from MovieRatings as E"; 
      System.out.println(hql); 
      @SuppressWarnings({ "deprecation", "unchecked" })  
      Query query = session.createSQLQuery(hql); 
      query.setResultTransformer(Transformers.aliasToBean(GetMovieInfo.class)); 
      List<MovieRatings> theRatings = query.list(); 
       System.out.println(theRatings); 
       session.getTransaction().commit(); 
      } catch (Exception e) { 
       // TODO: handle exception 
      }