2016-10-30 25 views
0

我在stackoverflow上看到了幾乎所有的問題和答案,但所有答案都是一樣的。你應該使用類似Query query = session.createQuery("from theme").list(); 但是.list()被刪除,我不能再使用它了。Hibernate獲取數據.list()depricated和表未映射。該怎麼辦?

所以,第一個問題是,我應該怎麼用的,而不是.list(),爲什麼我得到這個exeption java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: theme is not mapped [from theme]

查詢方法

public ObservableList<String> getThemes(){ 
    ObservableList<String> obsList = FXCollections.observableArrayList(); 
    SessionFactory factory = new Configuration() 
          .configure("hibernate.cfg.xml") 
          .buildSessionFactory(); 
    Session session = factory.getCurrentSession(); 
    try { 
     session.beginTransaction(); 
     Query query = session.createQuery("from theme"); // why not mapped 
                 //cant use .list()? 
     session.getTransaction().commit(); 
     System.out.println("query " + query); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    }finally{ 
     session.close(); 
    } 
    return obsList;  // nothing at the moment 

的hibernate.cfg.xml

<!DOCTYPE hibernate-configuration PUBLIC 
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 
    <hibernate-configuration> 
    <session-factory> 
    <property name="connection.driver_class">org.sqlite.JDBC</property> 
    <!-- I know sqlite with an mysql dialect isnt the best, in the future it  
will be mySql--> <property 
name="connection.url">jdbc:sqlite:C:\\DokiDB\\database.db</property> 
    <property name="connection.username">progdm</property> 
    <property name="connection.password">release</property> 
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="connection.pool_size">1</property> 
    <property name="show_sql">true</property> 
    <property name="current_session_context_class">thread</property> 
</session-factory> 
</hibernate-configuration> 

映射類

@Entity 
@Table(name="theme") 

public class mapTheme { 
@Id 
@GeneratedValue(strategy=GenerationType.IDENTITY) 
@Column(name="theme_id") 

private int theme_id; 
@Column(name="theme_name") 

private String theme_name; 
public int getTheme_id() { 
    return theme_id; 
} 

public void setTheme_id(int theme_id) { 
    this.theme_id = theme_id; 
} 

public String getTheme_name() { 
    return theme_name; 
} 

public void setTheme_name(String theme_name) { 
    this.theme_name = theme_name; 
} 

public mapTheme(String theme_name) { 
    super(); 
    this.theme_name = theme_name; 
} 

public mapTheme(){ 
} 
} 

我該怎麼辦?

+0

[休眠錯誤 - QuerySyntaxException:用戶沒有被映射\ [來自用戶的\]的可能的複製(HTTP://計算器。 com/questions/9954590/hibernate-error-querysyntaxexception-users-is-not-mapped-from-users) –

+0

@XavierHammond這一個,是我發現的第一個答案之一,但它沒有幫助。他們正在使用.list()這是depricated,不應該再使用 – Blank

+0

好的解決了映射問題,但我仍然有問題,我不知道如何獲取我的值或數據。 – Blank

回答

0

查詢中使用的表名不是數據庫中表的名稱,它應該是實體類的名稱。 在這種情況下,你必須使用

Query query = session.createQuery("from mapTheme"); 

代替:

Query query = session.createQuery("from theme")