snipt

Ctrl+h for KB shortcuts

Java

Hibernate Criteria And Or

1
2
3
4
5
6
Criteria criteria = getSession().createCriteria(clazz); 
Criterion rest1= Restrictions.and(Restrictions.eq("A", "X"), 
           Restrictions.in("B", Arrays.asList("X","Y")));
Criterion rest2= Restrictions.and(Restrictions.eq("A", "Y"), 
           Restrictions.eq("B", "Z"));
criteria.add(Restrictions.or(rest1, rest2));
https://snipt.net/embed/8aceb2963b54dee89d38c00f771b1477/
https://snipt.net/raw/8aceb2963b54dee89d38c00f771b1477/
8aceb2963b54dee89d38c00f771b1477
java
Java
6
2016-09-26T03:09:34
True
False
False
May 02, 2014 at 04:16 PM
/api/public/snipt/134696/
hibernate-criteria-and-or-d04f6474
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">Criteria</span> <span class="n">criteria</span> <span class="o">=</span> <span class="n">getSession</span><span class="o">().</span><span class="na">createCriteria</span><span class="o">(</span><span class="n">clazz</span><span class="o">);</span> </span><span id="L-2"><a name="L-2"></a><span class="n">Criterion</span> <span class="n">rest1</span><span class="o">=</span> <span class="n">Restrictions</span><span class="o">.</span><span class="na">and</span><span class="o">(</span><span class="n">Restrictions</span><span class="o">.</span><span class="na">eq</span><span class="o">(</span><span class="s">&quot;A&quot;</span><span class="o">,</span> <span class="s">&quot;X&quot;</span><span class="o">),</span> </span><span id="L-3"><a name="L-3"></a> <span class="n">Restrictions</span><span class="o">.</span><span class="na">in</span><span class="o">(</span><span class="s">&quot;B&quot;</span><span class="o">,</span> <span class="n">Arrays</span><span class="o">.</span><span class="na">asList</span><span class="o">(</span><span class="s">&quot;X&quot;</span><span class="o">,</span><span class="s">&quot;Y&quot;</span><span class="o">)));</span> </span><span id="L-4"><a name="L-4"></a><span class="n">Criterion</span> <span class="n">rest2</span><span class="o">=</span> <span class="n">Restrictions</span><span class="o">.</span><span class="na">and</span><span class="o">(</span><span class="n">Restrictions</span><span class="o">.</span><span class="na">eq</span><span class="o">(</span><span class="s">&quot;A&quot;</span><span class="o">,</span> <span class="s">&quot;Y&quot;</span><span class="o">),</span> </span><span id="L-5"><a name="L-5"></a> <span class="n">Restrictions</span><span class="o">.</span><span class="na">eq</span><span class="o">(</span><span class="s">&quot;B&quot;</span><span class="o">,</span> <span class="s">&quot;Z&quot;</span><span class="o">));</span> </span><span id="L-6"><a name="L-6"></a><span class="n">criteria</span><span class="o">.</span><span class="na">add</span><span class="o">(</span><span class="n">Restrictions</span><span class="o">.</span><span class="na">or</span><span class="o">(</span><span class="n">rest1</span><span class="o">,</span> <span class="n">rest2</span><span class="o">));</span> </span></pre></div> </td></tr></table>
criteria, hibernate, java

SQL

How to simulate NVL in HQL

1
2
3
4
5
The equivalent to the nvl command in HQL is the coalesce command. coalesce(a,b) will return a if a is not null, otherwise b.

So you would want something on the lines of:

from Table where col1 = coalesce(:par1, 'asdf')
https://snipt.net/embed/343625203bcc0de1c0b4c5c29793e281/
https://snipt.net/raw/343625203bcc0de1c0b4c5c29793e281/
343625203bcc0de1c0b4c5c29793e281
sql
SQL
7
2016-09-23T21:56:54
True
False
False
/api/public/snipt/22301/
how-to-simulate-nvl-in-hql
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">The</span> <span class="n">equivalent</span> <span class="k">to</span> <span class="n">the</span> <span class="n">nvl</span> <span class="n">command</span> <span class="k">in</span> <span class="n">HQL</span> <span class="k">is</span> <span class="n">the</span> <span class="n">coalesce</span> <span class="n">command</span><span class="p">.</span> <span class="n">coalesce</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span> <span class="n">will</span> <span class="k">return</span> <span class="n">a</span> <span class="n">if</span> <span class="n">a</span> <span class="k">is</span> <span class="k">not</span> <span class="k">null</span><span class="p">,</span> <span class="n">otherwise</span> <span class="n">b</span><span class="p">.</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">So</span> <span class="n">you</span> <span class="n">would</span> <span class="n">want</span> <span class="n">something</span> <span class="k">on</span> <span class="n">the</span> <span class="n">lines</span> <span class="k">of</span><span class="p">:</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="k">from</span> <span class="k">Table</span> <span class="k">where</span> <span class="n">col1</span> <span class="o">=</span> <span class="n">coalesce</span><span class="p">(:</span><span class="n">par1</span><span class="p">,</span> <span class="s1">&#39;asdf&#39;</span><span class="p">)</span> </span></pre></div> </td></tr></table>
hibernate, sql

Java

Distinct using criteria API

1
2
3
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

that code will be like the select distinct * from table of the native sql
https://snipt.net/embed/22d2af873b37c2551f9ce74b850d5ed8/
https://snipt.net/raw/22d2af873b37c2551f9ce74b850d5ed8/
22d2af873b37c2551f9ce74b850d5ed8
java
Java
3
2016-09-25T11:08:56
True
False
False
/api/public/snipt/19777/
distinct-usando-criteria-api
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">criteria</span><span class="o">.</span><span class="na">setResultTransformer</span><span class="o">(</span><span class="n">Criteria</span><span class="o">.</span><span class="na">DISTINCT_ROOT_ENTITY</span><span class="o">);</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">that</span> <span class="n">code</span> <span class="n">will</span> <span class="n">be</span> <span class="n">like</span> <span class="n">the</span> <span class="n">select</span> <span class="n">distinct</span> <span class="o">*</span> <span class="n">from</span> <span class="n">table</span> <span class="n">of</span> <span class="n">the</span> <span class="kd">native</span> <span class="n">sql</span> </span></pre></div> </td></tr></table>
hibernate, java

Bash

Force Hibernation on Macbook

1
2
3
# what to do next time I close the lid
alias hibernateon="sudo pmset -a hibernatemode 5"
alias hibernateoff="sudo pmset -a hibernatemode 0"
https://snipt.net/embed/132d0b33de14727fbee3c5a9e70a40cc/
https://snipt.net/raw/132d0b33de14727fbee3c5a9e70a40cc/
132d0b33de14727fbee3c5a9e70a40cc
bash
Bash
3
2016-09-24T19:07:06
True
False
False
/api/public/snipt/17215/
force-hibernation-on-macbook
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="c"># what to do next time I close the lid</span> </span><span id="L-2"><a name="L-2"></a><span class="nb">alias </span><span class="nv">hibernateon</span><span class="o">=</span><span class="s2">&quot;sudo pmset -a hibernatemode 5&quot;</span> </span><span id="L-3"><a name="L-3"></a><span class="nb">alias </span><span class="nv">hibernateoff</span><span class="o">=</span><span class="s2">&quot;sudo pmset -a hibernatemode 0&quot;</span> </span></pre></div> </td></tr></table>
hibernate, macos
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.