3

我一直堅持這一段時間,無法取得進展。問題是當DB中的限制性字段是FK時,如何使用Hibernate的createCriteria休眠FK字段上的createCriteria不適用於我

這裏有2個表:帳戶和* cf_account_type *,它表示限定符(客戶端,僱員等)

CREATE TABLE account (
     usern character varying(30) NOT NULL, 
     cf_account_type character varying(30) NOT NULL 
    ); 
    ALTER TABLE ONLY account 
     ADD CONSTRAINT pk_account366 PRIMARY KEY (usern); 

    CREATE TABLE cf_account_type (
     cf_account_type character varying(30) NOT NULL 
    ); 
    ALTER TABLE ONLY cf_account_type 
     ADD CONSTRAINT pk_cf_account_type373 PRIMARY KEY (cf_account_type); 

    ALTER TABLE ONLY account 
     ADD CONSTRAINT fk_account465 FOREIGN KEY (cf_account_type) 
     REFERENCES cf_account_type(cf_account_type); 

溶液休眠文檔建議被發現 here和看起來像這樣:

Cat cat = new Cat(); 
    cat.setSex('F'); 
    cat.setColor(Color.BLACK); 
    List results = session.createCriteria(Cat.class) 
     .add(Example.create(cat)) 
     .list(); 

然而,顏色不OBJ但純文本字段。所以,問題在於我的代碼帳戶後退所有行,似乎沒有考慮到限制

Session session = SF.getSession(); 
    Transaction tx = session.beginTransaction(); 
    //creating an example instance 
    Account instance = new Account(); 
    instance.setCfAccountType(cfAccountType); 

    //fetching from db 
    List<Account> result = (List<Account>)session. 
      createCriteria(Account.class). 
        add(Example.create(instance)).list(); 
    tx.commit(); 

其他信息:

的DB是PostgreSQL的9.1.2和它正在通過JDBC4(PostgreSQL相關9.1-902.jdbc4.jar)

從hibernate.cfg.xml中通氣管連接文件

<property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
    <property name="hibernate.connection.password">CENSORED</property> 
    <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/CENSORED</property> 
    <property name="hibernate.connection.username">postgres</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQL82Dialect</property> 

    <property name="hibernate.bytecode.use_reflection_optimizer">false</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.search.autoregister_listeners">false</property> 
    <property name="hibernate.connection.autocommit">true</property> 
    <property name="hibernate.show_sql">false</property> 

    <property name="hibernate.hbm2ddl.auto">update</property> 

回答

3

the documentation

版本屬性,標識符和as社會被忽略。通過 默認值,排除空值屬性。

(重點煤礦)

你需要使用常規的標準限制:

session.createCriteria(Account.class, "account") 
     .add(Restrictions.eq("account.cfAccountType", cfAccountType) 
     .list(); 
2

documentation報價QBE的用法:

版本屬性,標識符和關聯被忽略。通過 默認值,排除空值屬性。

雖然,這則說明放置的標準在相關對象的示例:

List results = session.createCriteria(Cat.class) 
    .add(Example.create(cat)) 
    .createCriteria("mate") 
     .add(Example.create(cat.getMate())) 
    .list(); 

它應用到你的情況:

List<Account> result = (List<Account>) session.createCriteria(Account.class) 
       .add(Example.create(instance)) 
       .createCriteria("ofAccountType") 
        .add(Example.create(instance.getOfAccountType())) 
        .list();