<?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>Mission Data Blog &#187; hibernate</title>
	<atom:link href="http://www.missiondata.com/blog/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.missiondata.com/blog</link>
	<description>Louisville-based Web Development &#38; Software Engineering</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:58:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Hibernate and your Getters and Setters</title>
		<link>http://www.missiondata.com/blog/software-development/67/hibernate-and-your-getters-and-setters/</link>
		<comments>http://www.missiondata.com/blog/software-development/67/hibernate-and-your-getters-and-setters/#comments</comments>
		<pubDate>Sun, 11 Mar 2007 18:33:25 +0000</pubDate>
		<dc:creator>darrend</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.missiondata.com/blog/uncategorized/67/hibernate-and-your-getters-and-setters/</guid>
		<description><![CDATA[When you&#8217;re using Hibernate and are mapping to properties, keep your getters and setters as simple and self-contained as possible. The receiver being initialized may not have any other properties set, and the value being passed may not be fully initialized yet, either. If you don&#8217;t respect these two possibilities, then you will get bit [...]]]></description>
			<content:encoded><![CDATA[<p>When you&#8217;re using Hibernate and are mapping to properties, keep your getters and setters as simple and self-contained as possible. The receiver being initialized may not have any other properties set, and the value being passed may not be fully initialized yet, either. </p>
<p>If you don&#8217;t respect these two possibilities, then you will get bit in the butt alot, when you least expect it. To be fair, these situations can happen whether you&#8217;re using Hibernate or not, but when we first started using the framework we made lots of assumptions.</p>
<p>Here is a completely ridiculous example that violates the above restrictions:<br />
<span id="more-67"></span></p>
<pre><code>public class User
{
  private boolean isSurnameLast;
  private Name name;  

  private String fullName;
  private String initials;

  public boolean setSurnameLast(boolean surnameLast)
  {
    this.isSurnameLast=surnameLast;
  }

  public void setName(Name name)
  {
    this.name = name;
    String lastName;
    String firstName;
    initials = new StringBuffer(name.getSurName().substring(0,1)).append(".").append(name.getGivenName().substring(0,1)).toString();
    if(isSurnameLast)
    {
      lastName = name.getSurName();
      firstName = name.getGivenName();
    }
    else
    {
      lastName = name.getGivenName();
      firstName = name.getSurName();
    }
    fullName = new StringBuffer(firstName).append(" ").append(lastName).toString();
  }
  // more getters and setters blah blah blah
}</code></pre>
<p>(Ignore the lame &#8220;business logic&#8221; here&#8230;)</p>
<p>So you look in your database and you see that all the Users database entries have names and properly set surnameLast values, and all the Names database entries have a surName and a givenName. Sure the code is sloppy, but this is gonna work fine! </p>
<p>Nope, this <code>setName</code> is loaded with problems.</p>
<p>First, you&#8217;ll notice as your app runs that somehow <code>setName</code> is being called and passed a null. What?! Why! Durn that Hibernate, it is trying to trick you! No matter, we&#8217;ll catch it with a null check. You&#8217;ll shield your eyes as to when and why Hibernate or cglib or whatever is calling setters on an instance that isn&#8217;t even in the database, but at least it won&#8217;t blow up. This is the first bad smell you&#8217;ve blown through, you sloppy panicky codemonkey:</p>
<pre><code>  public void setName(Name name)
  {
    if(name!=null) // hahah
   {
       this.name = name;
      ....</code></pre>
<p>Then you&#8217;ll start realizing that no matter what the database says, sometimes you are getting the surname first in the full name! &#8220;Smith Joan!&#8221;, you&#8217;ll cry, but &#8220;it&#8217;s true for Joan Smith in the database! Why?&#8221; Well, sometimes Hibernate is calling <code>setName</code> before it calls <code>setSurnameLast</code>. It doesn&#8217;t matter which you have mapped first in the <tt>User.hbm.xml</tt> file, or which field you have first in the code. You can&#8217;t depend on other properties of User being initialized in the Hibernate setters.</p>
<p>Now, you&#8217;re also going to get NullPointerExceptions thrown out of this code, because sometimes that Name instance you&#8217;re being passed hasn&#8217;t had <em>its</em> properties initialized by Hibernate yet. Talk about something that seems sneaky and evil but is totally above board: Hibernate has created the new Name instance and has passed that reference to other instances, but hasn&#8217;t fully populated the Name instance yet.</p>
<p>That means this line will blow sky-high sometimes:</p>
<pre><code>initials = new StringBuffer(name.getSurName().substring(0,1)).append(".").append(name.getGivenName().substring(0,1)).toString();</code></pre>
<p>The sane thing to do here?</p>
<pre><code>  public void setName(Name name)
  {
    this.name = name;
  }</code></pre>
<p>So if you&#8217;re using Hibernate, go take a look at your classes and mappings! You may very well be doing something dangerous.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/software-development/67/hibernate-and-your-getters-and-setters/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Using the Hibernate API to Inspect Mapped Classes</title>
		<link>http://www.missiondata.com/blog/software-development/60/using-the-hibernate-api-to-inspect-mapped-classes/</link>
		<comments>http://www.missiondata.com/blog/software-development/60/using-the-hibernate-api-to-inspect-mapped-classes/#comments</comments>
		<pubDate>Thu, 03 Aug 2006 02:21:11 +0000</pubDate>
		<dc:creator>Rich Rodriguez</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[Utilities]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=60</guid>
		<description><![CDATA[For my current project we needed to audit the property setters Hibernate was using on our objects to make sure that any logic in them was not overly state dependent. More about this issue in Hibernate is available here. We have fairly rich object models, and a lot of methods, including setters, are never used [...]]]></description>
			<content:encoded><![CDATA[<p>For my current project we needed to audit the property setters Hibernate was using on our objects to make sure that any logic in them was not overly state dependent. More about this issue in Hibernate is available <a title="Hibernate Forums" href="http://forum.hibernate.org/viewtopic.php?t=937664">here</a>. We have fairly rich object models, and a lot of methods, including setters, are never used by Hibernate. We wanted a report of the setters actually used by Hibernate to limit the amount of code we had to examine.</p>
<p>The Hibernate API allows you a lot of access to its configuration object model, and this is ideal for finding out how Hibernate is interacting with your code. I wrote a small class to do this inspection. The method below is run after a Hibernate Configuration object named creatively as &#8220;configuration&#8221; has been built with mapping files:</p>
<pre><code>public Map findSetters() throws MappingException
{
  Map classToSetters = new HashMap();
  Iterator classMappingIterator =   configuration.getClassMappings();

  while(classMappingIterator.hasNext())
  {
    PersistentClass persistentClass = (PersistentClass)classMappingIterator.next();
    Class mappedClass = persistentClass.getMappedClass();
    Iterator propertyIt = persistentClass.getPropertyIterator();
    List classSetters = new LinkedList();

    classToSetters.put(mappedClass, classSetters);

    while(propertyIt.hasNext())
    {
      Property property = (Property)propertyIt.next();
      Setter setter = property.getSetter(mappedClass);

      classSetters.add(setter.getMethodName());
    }
  }
  return classToSetters;
}</code></pre>
<p>I have uploaded <a id="p61" onmousedown="selectLink(61);" href="http://blogs.missiondata.com/wp-content/uploads/2006/08/HibernateTools.tar.gz">a Java project</a> that contains the full HibernateInspector class, as well as some sample classes and mappings. Un-tar it, and run</p>
<pre><code class="console">ant -Dhibernate.home="path to hibernate 3" </code></pre>
<p>to build and run the example.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/software-development/60/using-the-hibernate-api-to-inspect-mapped-classes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

