<?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>Xebia Blog &#187; Priyanshu Goyal</title>
	<atom:link href="http://blog.xebia.com/author/priyanshu/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.xebia.com</link>
	<description>Software development done right!</description>
	<lastBuildDate>Wed, 01 Feb 2012 00:30:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Spring 2.5 – Heavily Auto-Wired</title>
		<link>http://blog.xebia.com/2007/11/22/328/</link>
		<comments>http://blog.xebia.com/2007/11/22/328/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 06:27:06 +0000</pubDate>
		<dc:creator>Priyanshu Goyal</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Spring]]></category>

	<!-- AutoMeta Start -->
	<category></category>
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://blog.xebia.com/2007/11/22/328/</guid>
		<description><![CDATA[Concept to auto-wire relationships among spring enabled beans has always been there. The idea with auto-wiring is to get away from the tedious task of specifying and more importantly maintaining explicit wiring. Originally it was supported to be done by name, by type, by constructors or auto-detect and then you had the option to auto-wire [...]]]></description>
			<content:encoded><![CDATA[<p>Concept to auto-wire relationships among spring enabled beans has always been there. The idea with auto-wiring is to get away from the tedious task of specifying and more importantly maintaining explicit wiring. Originally it was supported to be done by name, by type, by constructors or auto-detect and then you had the option to auto-wire all or specific beans within a context. But now with Spring 2.5 the auto wiring concept has taken a whole new meaning and so is the debate if we really want to do auto-wiring.</p>
<p>Spring 2.5 has a new @Autowire annotation.  @Autowire annotation let us do much fine grained auto-wiring then was possible before and also it make us much more explicit then was possible in pre Spring 2.5 times<br />
<span id="more-328"></span><br />
Lets consider few examples<br />
#1 </p>
<pre class="brush: java; title: ; notranslate">@Autowired
public void init(AccountDao accountDao, CustomerDao customerDao) {
	this.accountDao = accountDao;
	this.customerDao = customerDao;
}</pre>
<p>Do not need to have setters with one parameter to inject dependencies, any method with any name and any number of parameters will do. Can be applied to fields and constructors as well and obviously to the favorite setters <img src='http://blog.xebia.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>#2</p>
<pre class="brush: java; title: ; notranslate">@Autowired
private BaseDao[] daos;</pre>
<p>or</p>
<pre class="brush: java; title: ; notranslate">@Autowired
private Set&lt;BaseDao&gt; daos;</pre>
<p>Create an array or collection with all the possible beans available in the context..</p>
<p>#3</p>
<pre class="brush: java; title: ; notranslate">@Autowired(required=false)
private AccountDao accountDao = new AccountDao();</pre>
<p>Find it do it, if not leave it.</p>
<p>Moving on we can even fine control this by using another annotation @Qualifier. Again lets see some more examples<br />
#4</p>
<pre class="brush: java; title: ; notranslate">@Autowired
@Qualifier(&quot;bankService&quot;)
private BankService bkService;</pre>
<p>We are doing by-name auto-wiring by providing a bean name within the annotation. This might help by letting you declare the name of the property different from the name of the bean.</p>
<p>#5 – Example of custom qualifier annotations to take care of the case when we have more then one implementation which we want to auto-wire<br />
Define an annotation like this</p>
<pre class="brush: java; title: ; notranslate">@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Qualifier
public @interface Category {
    String value();
}</pre>
<p>Custom qualifier on autowired fields can be applied like this</p>
<pre class="brush: java; title: ; notranslate">public class BookList {
    @Autowired
    @Category(&quot;Technical&quot;)
    private Book technicalBook;

    @Autowired
    @Category(&quot;Management&quot;)
    private Book managementBook;

}</pre>
<p>and then do the bean definitions like this</p>
<pre class="brush: xml; title: ; notranslate">&lt;bean name=&quot;book&quot; class=&quot;example.Book&quot;&gt;
    &lt;qualifier type=&quot;Category&quot; value=&quot;Technical&quot;/&gt;
    &lt;!-- This implementation specific properties --&gt;
&lt;/bean&gt;
&lt;bean name=&quot;book&quot; class=&quot;example.Book&quot;&gt;
    &lt;qualifier type=&quot;Category&quot; value=&quot;Management&quot;/&gt;
    &lt;!-- This implementation specific properties --&gt;
&lt;/bean&gt;</pre>
<p>And if this is not enough we can always create a custom auto-wire configurer or may be use the new @Resource annotation<br />
for further auto-wiring stuff.</p>
<p>That is that for auto-wiring but Spring 2.5 introduces a concept of auto-detection of spring components on the classpath where we do not have to even define the beans (with the name and bean class) in the context. Spring does a component scanning on the classpath (which can be configured using filters) by detecting classes which are stereotyped and then registering them as spring enabled beans. </p>
<p>Yes it provides a lot of flexibility and a much greater control as can be seen from all of the above examples but still I feel it should be done with a lot of caution and I-am-going-to-be-consistent attitude.</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin-left: 10px;"><g:plusone size="small" count="1" href="http://blog.xebia.com/2007/11/22/328/"></g:plusone></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.xebia.com%2F2007%2F11%2F22%2F328%2F&amp;title=Spring%202.5%20%E2%80%93%20Heavily%20Auto-Wired" id="wpa2a_2"><img src="http://blog.xebia.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.xebia.com/2007/11/22/328/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Hibernate component (value object) inheritance mapping</title>
		<link>http://blog.xebia.com/2007/09/25/hibernate-component-value-object-inheritance-mapping/</link>
		<comments>http://blog.xebia.com/2007/09/25/hibernate-component-value-object-inheritance-mapping/#comments</comments>
		<pubDate>Tue, 25 Sep 2007 10:58:04 +0000</pubDate>
		<dc:creator>Priyanshu Goyal</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Hibernate]]></category>

	<!-- AutoMeta Start -->
	<category>pricespecification</category>
	<category>preparedstatement</category>
	<category>pricespecificationtype</category>
	<category>pricespectype</category>
	<category>price_spec_type</category>
	<category>price</category>
	<category>enum</category>
	<category>inheritance</category>
	<!-- AutoMeta End -->
	
		<guid isPermaLink="false">http://blog.xebia.com/2007/09/25/hibernate-component-value-object-inheritance-mapping/</guid>
		<description><![CDATA[Entity inheritance mapping in Hibernate can easily be done by following one of the three strategies and many a times instance of which subclass has to be created is identified by the value of the discriminator column (particularly in the case of table per class hierarchy). For such mappings there is already an inherent support [...]]]></description>
			<content:encoded><![CDATA[<p>Entity inheritance mapping in Hibernate can easily be done by following one of the three strategies and many a times instance of which subclass has to be created is identified by the value of the discriminator column (particularly in the case of table per class hierarchy). For such mappings there is already an inherent support in Hibernate.</p>
<p>So this is good if we want to create a sub class of a specific type for each row but what about if we want to create an instance of a specific class for a value of a column within that row. So in a way some type of component inheritance mapping. This is required if I really do not want to convert a value object (component) into an entity as that might mean sacrifice on the part of good model design as I am loosing the distinction between a component and an entity.</p>
<p>A workaround for the moment could be to somehow use a UserType to create instance of such objects based upon the value stored in the column. The concept of discriminator column can still help us. Let’s see how by taking up the following example</p>
<p><span id="more-294"></span></p>
<p>Suppose we have a catalog of books where each book has a price specification depending upon the currency of where the book was originally published. Each price specification is modeled by a separate class which has a property amount. Assume here that these classes have a different behavior depending upon the currency for calculation of sales tax and stuff (otherwise we can simply create a class and do the mapping using component or one-to-one).</p>
<p>So the book table looks something like this</p>
<pre class="brush: sql; title: ; notranslate">CREATE TABLE BOOK (
    ID NUMBER(19,0) NOT NULL,
    PRICE_SPEC_TYPE VARCHAR2(3) NOT NULL,
    PRICE_AMOUNT    NUMBER(19,0) NOT NULL,
    …
);</pre>
<p>And book class like this</p>
<pre class="brush: java; title: ; notranslate">public class Book {
    private long id;
    private PriceSpecification priceSpecification;
    …
}</pre>
<p>And one of the price specifications may be</p>
<pre class="brush: java; title: ; notranslate">public class USDollarSpecification implement PriceSpecification {
    private long amount;
    public void setAmount(long amount) {
        this.amount = amount;
    }
    public long getMaximumRetailPrice() {
    }
    …
}</pre>
<p>Now this can be obviously mapped using a UserType but to make it much cleaner lets use an Enum. Some sample code of PriceSpecificationUserType looks something like this.</p>
<pre class="brush: java; title: ; notranslate">public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
    String type = rs.getString(names[0]);
    long amount = rs.getLong(names[1]);
    PriceSpecificationType priceSpecType = priceSpecificationType.valueOf(type);
    PriceSpecification specification = priceSpecType.getNewInstance();
    specification.setAmount();
    return specification;
}
</pre>
<p>Here we create an instance of a price specification from the value in PRICE_SPEC_TYPE column using an Enum for price specification types.</p>
<pre class="brush: java; title: ; notranslate">public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) throws HibernateException, SQLException {
    if (value == null) {
        preparedStatement.setNull(index, Types.VARCHAR);
        preparedStatement.setNull(index + 1, Types.NUMERIC);
    } else {
        PriceSpecification priceSpecification = (PriceSpecification) value;
        String className = priceSpecification.getClass().getName();
        PriceSpecificationType[] priceSpecTypes = PriceSpecificationType.values();
        for (int i = 0; i &lt; priceSpecTypes.length; i++) {
            PriceSpecificationType priceSpecType = priceSpecTypes[i];
            if (priceSpecType.getClassName().equals(className)) {
                preparedStatement.setString(index, priceSpecType.name());
                break;
            }
        }
        preparedStatement.setInt(index + 1, priceSpecification.getAmount());
    }
}</pre>
<p>And similarly we set the price specification in PRICE_SPEC_TYPE using the Enum for price specification types.</p>
<p>Here is how Enum looks like</p>
<pre class="brush: java; title: ; notranslate">public enum PriceSpecificationType {
    US_DOLLAR {
        public String getClassName() {
            return USDollarSpecification.class.getName();
        }

        @Override
        public PriceSpecification getNewInstance() {
            return new USDollarSpecification();
        }
    },
    …

    public abstract PriceSpecification getNewInstance();

    public abstract String getClassName();
}
</pre>
<p>And finally let’s see the mapping for this</p>
<pre class="brush: xml; title: ; notranslate">&lt;typedef name=&quot;PriceSpecification&quot; class=&quot;PriceSpecificationUserType&quot;/&gt;

&lt;class name=”Book” table=”BOOK”
    &lt;property name=&quot;priceSpecification&quot; type=&quot;PriceSpecification&quot;&gt;
        &lt;column name=&quot;PRICE_SPEC_TYPE&quot;/&gt;
        &lt;column name=&quot;PRICE_AMOUNT&quot;/&gt;
    &lt;/property&gt;
&lt;/class&gt;</pre>
<p>Hibernate might add a support for this in one of the future releases going by this <a href="http://opensource.atlassian.com/projects/hibernate/browse/HHH-1152">http://opensource.atlassian.com/projects/hibernate/browse/HHH-1152</a> but for now this might be a workaround we have to live with</p>
<div name="googleone_share_1" style="position:relative;z-index:5;float: right; margin-left: 10px;"><g:plusone size="small" count="1" href="http://blog.xebia.com/2007/09/25/hibernate-component-value-object-inheritance-mapping/"></g:plusone></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fblog.xebia.com%2F2007%2F09%2F25%2Fhibernate-component-value-object-inheritance-mapping%2F&amp;title=Hibernate%20component%20%28value%20object%29%20inheritance%20mapping" id="wpa2a_4"><img src="http://blog.xebia.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://blog.xebia.com/2007/09/25/hibernate-component-value-object-inheritance-mapping/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  blog.xebia.com/author/priyanshu/feed/ ) in 0.49453 seconds, on Feb 9th, 2012 at 4:29 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 9th, 2012 at 5:29 pm UTC -->
