Question

I've been stuck with this for a while and cannot make progress. The question is how to use Hibernate's createCriteria when the restrictive field in DB is a FK.

Here are 2 tables: account and *cf_account_type*, which represents the qualifier (client, employee, etc)

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

The solution Hibernate documentation suggests is found here and looks like this:

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

However, sex and color are not objects but plain text fields. So, the problem is that my code returs all rows from account and doesn't seem to take into account the restriction.

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

Additional information:

The DB is PostgreSQL 9.1.2 and it is being connected via JDBC4 (postgresql-9.1-902.jdbc4.jar)

Outtake from hibernate.cfg.xml file

    <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>
Était-ce utile?

La solution

From the documentation:

Version properties, identifiers and associations are ignored. By default, null valued properties are excluded.

(emphasis mine)

You'll need to use a regular criteria restriction:

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

Autres conseils

Quoting usage of QBE from documentation:

Version properties, identifiers and associations are ignored. By default, null valued properties are excluded.

Although, it then shows an example of placing criteria upon associated objects:

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

Apply it to your case:

List<Account> result = (List<Account>) session.createCriteria(Account.class)
                .add(Example.create(instance))
                .createCriteria("ofAccountType")
                     .add(Example.create(instance.getOfAccountType()))
                     .list();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top