<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Our Geek Space &#187; jee</title>
	<atom:link href="http://blog.moove-it.com/tag/jee/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.moove-it.com</link>
	<description>be free to express yourself...</description>
	<lastBuildDate>Thu, 26 Jan 2012 19:30:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Auditing, History of database modifications with Hibernate &#8211; Envers Solution !</title>
		<link>http://blog.moove-it.com/auditing-history-of-database-modifications-with-hibernate-envers-solution/</link>
		<comments>http://blog.moove-it.com/auditing-history-of-database-modifications-with-hibernate-envers-solution/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 14:16:22 +0000</pubDate>
		<dc:creator>Martin Cabrera</dc:creator>
				<category><![CDATA[java]]></category>
		<category><![CDATA[jee]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[auditing]]></category>
		<category><![CDATA[envers]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java ee]]></category>
		<category><![CDATA[java enterprise]]></category>

		<guid isPermaLink="false">http://blog.moove-it.com/?p=408</guid>
		<description><![CDATA[Envers is one of the Hibernate&#8217;s project sponsored by Redhat. Works with Hibernate and Hibernate Entity Manager. Aims to enable easy auditing of persistent classes. For each audited entity you must annotate your persistent clases with @Audited, a table will be created, which will hold the history of changes made to the entity. The idea is very similar [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.moove-it.com/wp-content/uploads/2010/10/hibernatelogo.png" rel="lightbox[408]" title="hibernate-logo"><img class="alignleft size-full wp-image-409" title="hibernate-logo" src="http://blog.moove-it.com/wp-content/uploads/2010/10/hibernatelogo.png" alt="hibernate-logo" width="312" height="92" /></a></p>
<p><strong>Envers</strong> is one of the <strong>Hibernate&#8217;s</strong> project sponsored by Redhat. Works with Hibernate and Hibernate Entity Manager.</p>
<p>Aims to enable easy auditing of persistent classes. For each audited entity you must annotate your persistent clases with @Audited, a table will be created,  which will hold the history of changes made to the entity.</p>
<p>The idea is very similar to CVS or Subersion. Each transaction with updates, deletes or inserts made in the database with hibernate generate a new revision number. One transaction is one revision.</p>
<p>Envers provide a simple way to retrive your data of a revision using: revision number, date, queries with min and max function between others.</p>
<p>You can use Envers wherever Hibernate works: standalone, inside JBoss AS, with JBoss Seam or Spring.</p>
<p>It&#8217;s very easy &#8230;</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@Entity</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@Audited</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">public class Usuario {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>public enum Rol{</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>EMPRESA, ADMINISTRADOR,MESA_ENTRADA;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span></div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>private Long id;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>private String usuario;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>private String contrasenia;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>private Rol rol;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span></div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>@Id</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>@GeneratedValue</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>public Long getId() {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>return id;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>}</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>public void setId(Long id) {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>this.id = id;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 64px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>}</div>
<pre>@Entity @Audited
public class User {
<span style="white-space: pre;">	</span>private Long id;

        @Id @GeneratedValue
        public Long getId() { return id; }
        public void setId(Long id) { this.id = id; }

        // other fields and methods
        ...
}</pre>
<h3>Envers configuration</h3>
<p><span style="font-family: 'Lucida Grande', Geneva, Verdana, Arial, sans-serif; line-height: 18px; font-size: 12px; color: #333333;">When configuring your Hibernate (<code class="literal" style="font-size: 0,9em; font-family: verdana, helvetica, sans-serif; white-space: nowrap;">persistence.xml</code> if you are using JPA, <code class="literal" style="font-size: 0,9em; font-family: verdana, helvetica, sans-serif; white-space: nowrap;">hibernate.cfg.xml</code> or other if you are using Hibernate directly), add the following event listeners:</span></p>
<p><span style="font-family: 'Lucida Grande', Geneva, Verdana, Arial, sans-serif; line-height: 18px; font-size: 12px; color: #333333;"> </span></p>
<pre>&lt;persistence-unit ...&gt;
&lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;
&lt;class&gt;...&lt;/class&gt;
&lt;properties&gt;
   &lt;property name="hibernate.dialect" ... /&gt;
   &lt;!-- other hibernate properties --&gt;

   &lt;property name="hibernate.ejb.event.post-insert"
             value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener" /&gt;
   &lt;property name="hibernate.ejb.event.post-update"
             value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener" /&gt;
   &lt;property name="hibernate.ejb.event.post-delete"
             value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener" /&gt;
   &lt;property name="hibernate.ejb.event.pre-collection-update"
             value="org.hibernate.envers.event.AuditEventListener" /&gt;
   &lt;property name="hibernate.ejb.event.pre-collection-remove"
             value="org.hibernate.envers.event.AuditEventListener" /&gt;
   &lt;property name="hibernate.ejb.event.post-collection-recreate"
             value="org.hibernate.envers.event.AuditEventListener" /&gt;
&lt;/properties&gt;
&lt;/persistence-unit&gt;</pre>
<p>Then, annotate your persistent class with @Audited (like the User example)</p>
<p>And that&#8217;s it!</p>
<h3>Queries</h3>
<p><strong>Find audited data</strong></p>
<p>You can access the audit (history) of an entity using the <a id="quickstart" style="text-decoration: none;"><code class="literal" style="font-size: 0,9em; font-family: verdana, helvetica, sans-serif; white-space: nowrap;">AuditReader</code></a> interface, which you can obtain when having an open EntityManager.</p>
<pre>AuditReader reader = AuditReaderFactory.get(entityManager); 
User oldUser = reader.find(User.class, userId, revision)</pre>
<p><strong>Search a revision number</strong></p>
<p>the whole documentation is in the Web site:  <a href="http://docs.jboss.org/envers/docs/index.html#queries">http://docs.jboss.org/envers/docs/index.html#queries</a></p>
<p>To ilustrate one example I show the way to search the max number revision of one Entity audited.</p>
<pre>Number revision = (Number) reader.createQuery().forRevisionsOfEntity(User.class, false, true)
.addProjection(AuditEntity.revisionNumber().max())
.add(AuditEntity.id().eq(userId))
.getSingleResult();</pre>
<p>We use Envers in a few projects. Works fine and very quickly, we recomended &#8230; no dubt !  Envers</p>
<p>Web site: <a href="http://www.jboss.org/envers">www.jboss.org/envers</a></p>
<p>Envers documentation: <a href="http://docs.jboss.org/envers/docs/index.html">http://docs.jboss.org/envers/docs/index.html</a></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.moove-it.com/auditing-history-of-database-modifications-with-hibernate-envers-solution/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JPA @OneToMany &#8211; I want a foreign key. I don&#8217;t want a Join table</title>
		<link>http://blog.moove-it.com/jpa-onetomany-i-need-fk-child-table/</link>
		<comments>http://blog.moove-it.com/jpa-onetomany-i-need-fk-child-table/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 19:21:28 +0000</pubDate>
		<dc:creator>Ariel Ludueña</dc:creator>
				<category><![CDATA[jee]]></category>
		<category><![CDATA[jpa]]></category>
		<category><![CDATA[moove-it]]></category>
		<category><![CDATA[@OneToMany]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java enterprise]]></category>

		<guid isPermaLink="false">http://blog.moove-it.com/?p=364</guid>
		<description><![CDATA[Suppose you have this two entities: PhoneList and Phone. PhoneList have a @OneToMany to Phone. (I need only a unidirectional relationship) Something like: @OneToMany(cascade=CascadeType.ALmappedBy=&#8221;phoneList&#8221;) public List&#60;Phone&#62; getPhones() { return phones; @OneToMany(cascade=CascadeType.ALL) public List&#60;Phone&#62; getPhones() { return phones; } The phone entity doesn&#8217;t have nothing special. If you deploy those entities your JPA provider will create [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you have this two entities: PhoneList and Phone.</p>
<p>PhoneList have a @OneToMany to Phone. (I need only a unidirectional relationship)</p>
<p>Something like:</p>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;">@OneToMany(cascade=CascadeType.ALmappedBy=&#8221;phoneList&#8221;)</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>public List&lt;Phone&gt; getPhones() {</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span>return phones;</div>
<div id="_mcePaste" style="position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;"><span style="white-space: pre;"> </span></div>
<pre>@OneToMany(cascade=CascadeType.ALL)
public List&lt;Phone&gt; getPhones() {
   return phones;
}</pre>
<p>The phone entity doesn&#8217;t have nothing special.</p>
<p>If you deploy those entities your JPA provider will create 3 tables. One table for each entity and also a &#8220;JoinTable&#8221;. But, why? This kind of relationship can be done by 2 tables only and I don&#8217;t want a third table. I want a PHONE table with an FK column to PHONELIST table.</p>
<p>How can I do that?</p>
<p><strong>Two options:</strong></p>
<ol>
<li><strong>Make your relationship bidirectional</strong></li>
<li><strong>Add an @JoinColumn in the @OneToMany attribute</strong></li>
</ol>
<h2><strong>Option number 1 &#8211; Make your relationship bidirectional</strong></h2>
<p>The problem is that &#8220;I don&#8217;t need a bidirectional relationship&#8221;! &#8230; Ok, but, suppose that you are more interested in solve this and move forward to a &#8220;real issue&#8221;.</p>
<p>To make this relationship bidirectional, you must add in the Phone entity the reference to the PhoneList entity with the @ManyToOne annotation. Something like:</p>
<pre>@Entity
public class Phone {

private Long id;
private String phoneNumber;
private PhoneList phoneList;

@ManyToOne
public PhoneList getPhoneList() {
   return phoneList;
}
...</pre>
<p>Also you have to add to the relationship @OneToMany in the PhoneList entity the mapped by attribute. Something like this:</p>
<pre>@Entity
public class PhoneList {

	private Long id;
	private String name;
	private String description;
	private List&lt;Phone&gt; phones;

	@Id @GeneratedValue
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@OneToMany(cascade=CascadeType.ALL, mappedBy="phoneList")
	public List&lt;Phone&gt; getPhones() {
		return phones;
	}</pre>
<h2><strong>Option number 2 &#8211; Add @JoinColumn in the @OneToMany attribute</strong></h2>
<p>If you don&#8217;t want to have a bidirectional relationship, you have to use @JoinColumn like this:</p>
<pre>@Entity
public class PhoneList {

	private Long id;
	private String name;
	private String description;
	private List phones;

	@Id @GeneratedValue
	public Long getId() {
	  return id;
	}

	public void setId(Long id) {
          this.id = id;
	}

	@OneToMany(cascade=CascadeType.ALL)
	@JoinColumn(name="phone_list_id", referencedColumnName="id")
        public List getPhones() {
	  return phones;
	}</pre>
<p>and the Phone entity remain without references</p>
<pre>@Entity
public class Phone {

private Long id;
private String phoneNumber;

...</pre>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.moove-it.com/jpa-onetomany-i-need-fk-child-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Steps to include lightweight cache in java applications &#8211; WhirlyCache</title>
		<link>http://blog.moove-it.com/steps-to-include-lightweight-cache-in-java-applications-whirlycache/</link>
		<comments>http://blog.moove-it.com/steps-to-include-lightweight-cache-in-java-applications-whirlycache/#comments</comments>
		<pubDate>Thu, 28 May 2009 21:34:12 +0000</pubDate>
		<dc:creator>Martin Cabrera</dc:creator>
				<category><![CDATA[jee]]></category>
		<category><![CDATA[whirlycache]]></category>
		<category><![CDATA[j2ee]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java cache]]></category>
		<category><![CDATA[java enterprise]]></category>

		<guid isPermaLink="false">http://blog.moove-it.com/?p=188</guid>
		<description><![CDATA[Data caching is a very important consideration for JEE applications. A classic problem for any application is to detect and solve the recurrent calls for optimizing the applications. Some cases can be: database calls business logic calls remote method invocations Data caching limits the number of remote invocations in distributed applications and improves performance of [...]]]></description>
			<content:encoded><![CDATA[<p style="margin-bottom: 0cm;">Data caching is a very important consideration for JEE applications.</p>
<p>A classic problem for any application is to detect and solve the recurrent calls for optimizing the applications. Some cases can be:</p>
<ul>
<li>database calls</li>
<li>business logic calls</li>
<li>remote method invocations</li>
</ul>
<p>Data caching limits the number of remote invocations in distributed applications and improves performance of web applications by reducing the number of calls.</p>
<p>Several solutions exist to include a framework for implementing a cache.</p>
<ul>
<li><a href="http://www.opensymphony.com/oscache/" target="_blank">OsCache</a></li>
<li><a href="http://swarmcache.sourceforge.net/" target="_blank">SwarmCache</a></li>
<li><a href="http://www.jofti.com/" target="_blank">jofti</a></li>
<li><a href="http://labs.jboss.com/jbosscache/" target="_blank">jboss cache</a></li>
<li><a href="https://whirlycache.dev.java.net/" target="_blank">WhirlyCache</a></li>
<li><a href="http://ehcache.sourceforge.net/" target="_blank">EhCache</a></li>
<li><a href="http://cache4j.sourceforge.net/" target="_blank">Cache4J</a></li>
</ul>
<p>Check out this <a href="http://www.developer.com/img/2007/09/Table.htm" target="_blank">interesting comparative table.</a></p>
<h2>WhirlyCache (<a href="https://whirlycache.dev.java.net" target="_blank">https://whirlycache.dev.java.net</a>)</h2>
<p>I use whirlycache in two projects and works really good. I recommend it to incorporate in a simple lightweight Web application<br />
Whirlycache is a fast, configurable in-memory object cache for Java. It can be used, for example, to speed up a website or an application by caching objects that would otherwise have to be created by querying a database or by another expensive procedure. From the testing that we have done, it appears to be faster than any other Java cache that we have been able to inspect.</p>
<h2 style="margin-bottom: 0cm;">Steps to include WhirlyCache</h2>
<p><strong>(Step I)</strong> Donwload whirlycache and include it in your project -&gt; <a href="https://whirlycache.dev.java.net/files/documents/1995/34601/whirlycache-1.0.1.zip" target="_blank">https://whirlycache.dev.java.net/files/documents/1995/34601/whirlycache-1.0.1.zip</a></p>
<p><strong>(Step II)</strong> Create a cache instance</p>
<pre>CacheConfiguration cc = new CacheConfiguration();

cc.setName("KBCache");

cc.setBackend("com.whirlycott.cache.impl.ConcurrentHashMapImpl");

cc.setTunerSleepTime(60);

cc.setPolicy("com.whirlycott.cache.policy.LFUMaintenancePolicy");

cc.setMaxSize(10000);

Cache c = CacheManager.getInstance().createCache(cc);</pre>
<p><strong>(Step III)</strong> Use cache instance</p>
<p style="margin-bottom: 0cm;">
<pre>c.store(key, object);
c.remove(key);
c.retrieve(key);</pre>
<p style="margin-bottom: 0cm;">Example of java class (is a jboss seam entity class) &#8211; <span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: #000000;"><span style="font-weight: normal;"><a href="http://blog.moove-it.com/wp-content/uploads/2009/05/stockcache.java">StockCache.java</a></span></span></span></span></p>
<p style="margin-bottom: 0cm;"><span style="font-family: Monospace;"><span style="font-size: x-small;"><span style="color: #000000;"><span style="font-weight: normal;"><br />
</span></span></span></span></p>
<!-- PHP 5.x -->]]></content:encoded>
			<wfw:commentRss>http://blog.moove-it.com/steps-to-include-lightweight-cache-in-java-applications-whirlycache/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

