2

I wanna set lazy to "false" but only to one method at in runtime. Can I do?

this.getSession().createSQLQuery("select * from customers....")....

Attention: im using createSQLQuery not createCriteria.

CustomerMapping.xml here:

<hibernate-mapping>
<class name="com.example.entities.customers.Customer"
    table="CUSTOMERS">
    <id name="id" type="long">
        <column name="ID" />
        <generator class="sequence">
            <param name="sequence">seq_customers</param>
        </generator>
    </id>       
    <property name="name" type="String">
        <column name="NAME_C" />
    </property>
   <many-to-one name="address"
        class="com.example.entities.Address" fetch="select"
        cascade="all">
        <column name="ADDRESS_ID" />
    </many-to-one>
</class>
</hibernate-mapping>

I wanna set lazy to false for Address.

I have to do this because this method return a list of customers (with address) and when I iterate this list and print its very slow cause the lazy is setted true (by default).

user1750751
  • 275
  • 3
  • 15

1 Answers1

1

Is there a reason you are using SQL instead of HQL? I would stay away from SQL statements when using hibernate when possible.

I would implement it like this in HQL:

from Customer c
join fetch c.address

the join fetch makes Customers address no longer lazy.

jax
  • 37,735
  • 57
  • 182
  • 278
  • I using Sql because I have an UNION and HQL dont support Union clause. Is there other way to do that? – user1750751 Nov 11 '13 at 11:56
  • Yes, in fact you can even use native sql commands specific to you DB in HQL (although try not to do so where possible because it will limit the portability of your application to a different database) – jax Nov 12 '13 at 08:18
  • Install the Hibernate Plugin for Eclilpse - you can then interactively run and test your HQL queries. – jax Nov 12 '13 at 08:20
  • Actually maybe you can't do UNION after all: http://stackoverflow.com/questions/7182433/how-to-do-a-union-sql-statement-in-hql – jax Nov 12 '13 at 08:22
  • You can manually union two seperate queries in code as a workaround: http://www.experts-exchange.com/Programming/Languages/Java/J2EE/Q_22563113.html – jax Nov 12 '13 at 08:25