Archive for the ‘Technical’ Category
JTL goes open-source
Posted on September 6th, 2008 in EN, Java, Tools | No Comments »
JTL, the Java Tools Language (pronounced “Gee-tel”), is a high-level query language for selecting program elements, designed to serve the development of source code software tools for Java.
It is both simple and powerful: you may use it for various source code tasks, from just finding all classes with print() method to executing an extremely complex refactoring transformation.
JTL is very easy to use, its intuitive query-by-example syntax makes it really easy to write JTL queries. For example,
public static double method(int, int);
matches all static methods that receive two int parameters and return a
classWithFactory := is T, class { no public constructor; public static T method; }
matches all factory classes - classes having no public constructor, but instance factory method.
Additional JTL examples may be found in JTL Language Tour. The examples above come from this excellent manual too.
The JTL was initially described in the JTL - The Java Tools Language paper and developed at the Computer Science department at the Technion. But now, after a years of development, JTL goes open source at sourceforge. There is no blog or RSS feed there yet, but I hope those will appear soon.
Hibernate annotations: The many-to-many association with composite key
Posted on July 19th, 2008 in EN, Java, Tools | 50 Comments »
Also many-to-many associations are usually referenced as a bad design solution, they are widely used in almost all modern database-centric applications (especially in those built around the existing legacy database). The very common scenario is a case when you not only need to handle a many-to-many association but also hold some additional property on the association.
In a J2EE world, Hibernate still remains the most popular ORM tool (or the JPA provider, if you wish). But implementing such a scenario with Hibernate Annotations is not so simple as you might imagine.
First of all, in the good old spirit of the Hibernate community, there is almost no documentation about many-to-many associations and composite keys (two paragraphs stating that there is @ManyToMany and @EmbeddedId annotations are not considered a documentation).
And the second hope of every open-source tool consumer, the world wide web community, provides almost no help: everybody are referencing the same post by Marcel Panse, written in April 2006. Though Marsel’s post is very clean and descriptive, it become outdated and the solution provided there is simply not working with the latest versions of Hibernate Annotations.
So I had to reinvent the wheel by my own, based on Marsel’s sample. Below is the solution working with Hibernate Annotations 3.3.1.GA.
The database part is the same: we have three tables (item, product and product_item), two POJO classes, and two classes for a many-to-many association and its primary key. The main difference from Marsel’s solution is that I’m not using any kind of “fake” properties on ProductItem in order to reference Item and Product, but just a plain transient properties delegating to ProductItemPk.
@Entity @Table(name = "item") public class Item { private Integer id; private String name; private List<ProductItem> productItems = new LinkedList<ProductItem>(); public Item() { } @Id @GenericGenerator(name = "generator", strategy = "increment") @GeneratedValue(generator = "generator") @Column(name = "item_id", nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "name") public String getName() { return this.name; } public void setName(String name) { this.name = name; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.item") public List<ProductItem> getProductItems() { return this.productItems; } public void setProductItems(List<ProductItem> productItems) { this.productItems = productItems; } }
@Entity @Table(name = "product") public class Product { private Integer id; private String name; private List<ProductItem> productItems = new LinkedList<ProductItem>(); public Product() { } @Id @GenericGenerator(name = "generator", strategy = "increment") @GeneratedValue(generator = "generator") @Column(name = "product_id", nullable = false) public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } @Column(name = "name") public String getName() { return this.name; } public void setName(String name) { this.name = name; } @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.product") public List<ProductItem> getProductItems() { return this.productItems; } public void setProductItems(List<ProductItem> productItems) { this.productItems = productItems; } }
Note the two important points: ProductItem has two transient properties for Product and Item (since an unidirectional relationships will be meaningless here) and a use of @AssociationOverrides annotation to specify the database columns
@Entity @Table(name = "product_item") @AssociationOverrides({ @AssociationOverride(name = "pk.item", joinColumns = @JoinColumn(name = "item_id")), @AssociationOverride(name = "pk.product", joinColumns = @JoinColumn(name = "product_id")) }) public class ProductItem { private ProductItemPk pk = new ProductItemPk(); @EmbeddedId private ProductItemPk getPk() { return pk; } private void setPk(ProductItemPk pk) { this.pk = pk; } @Transient public Item getItem() { return getPk().getItem(); } public void setItem(Item item) { getPk().setItem(item); } @Transient public Product getProduct() { return getPk().getProduct(); } public void setProduct(Product product) { getPk().setProduct(product); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ProductItem that = (ProductItem) o; if (getPk() != null ? !getPk().equals(that.getPk()) : that.getPk() != null) return false; return true; } public int hashCode() { return (getPk() != null ? getPk().hashCode() : 0); } }
@Embeddable public class ProductItemPk implements Serializable { private Item item; private Product product; @ManyToOne public Item getItem() { return item; } public void setItem(Item item) { this.item = item; } @ManyToOne public Product getProduct() { return product; } public void setProduct(Product product) { this.product = product; } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; ProductItemPk that = (ProductItemPk) o; if (item != null ? !item.equals(that.item) : that.item != null) return false; if (product != null ? !product.equals(that.product) : that.product != null) return false; return true; } public int hashCode() { int result; result = (item != null ? item.hashCode() : 0); result = 31 * result + (product != null ? product.hashCode() : 0); return result; } }
Hope this helps.
Good coders, maybe. Awful product managers, definetely.
Posted on April 3rd, 2008 in EN, Technical | No Comments »
The discussion is probably one of the best examples to the simple rule: programmers should never ever be allowed to lead a product design.
The rule of thumb
Posted on January 3rd, 2008 in EN, Java, Tools | 1 Comment »
If you may configure something using Spring — never ever try to configure the same thing with Hibernate.
Sorry, Gavin.
Is the modern software getting more and more humanlike?
Posted on December 20th, 2007 in EN, Tools | 2 Comments »
The colleague of mine just got this from his IntelliJ IDEA 7.0.2:

At least it apologizes…
Good old IE bug?
Posted on November 12th, 2007 in Bugs, EN | 4 Comments »
May someone explain me why the following HTML code is causing IE6 SP2 crash?
<html xmlns="http://www.w3.org/1999/xhtml"> <body> <table> <tr> <td> <table style="table-layout:fixed"> <col width="20"/> </table> </td> </tr> </table> </body> </html>
Bonus: CodeColorizer
Playing around with IntelliJ IDEA 7
Posted on October 30th, 2007 in EN, Java, Tools | 1 Comment »
There is something charming in those small things IDE developers make for us.

Mainsoft makes one more step forward to developing Grasshopper community
Posted on July 11th, 2007 in EN, Grasshopper | 2 Comments »
This week Mainsoft introduced a new source of information for all Grasshopper users: «The Grasshoppers» blog.
Although currently there is only one person actually blogging there, this is a promising step forward to developing a rich community of both Grasshopper users and developers.
By the way, it worth to take a look at full Grasshopper bloggers list.
Mono and CafePress
Posted on June 26th, 2007 in EN, Mono | 5 Comments »
Since CafePress is still lacking any kind of Mono project stuff, I decided to make one of my own.

There is nothing better than starting you morning with a good cup of Mono…






