【问题标题】:hibernate createCriteria on a FK field is not working for meFK 字段上的休眠 createCriteria 不适用于我
【发布时间】:2012-06-18 13:54:04
【问题描述】:

我已经被这个问题困扰了一段时间,无法取得进展。问题是当 DB 中的限制字段是 FK 时如何使用 Hibernate 的 createCriteria

这里有 2 个表:account 和 *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);

找到了 Hibernate 文档建议的解决方案 here 看起来像这样:

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

但是,sexcolor 不是对象,而是纯文本字段。所以,问题是我的代码account返回所有行,并且似乎没有考虑到限制

    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();

附加信息:

数据库是 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>

【问题讨论】:

    标签: hibernate foreign-key-relationship postgresql-9.1 query-by-example


    【解决方案1】:

    来自the documentation

    版本属性、标识符和关联被忽略。经过 默认情况下,空值属性被排除在外。

    (强调我的)

    您需要使用常规条件限制:

    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();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-22
        • 2020-12-29
        • 1970-01-01
        相关资源
        最近更新 更多