Monday, September 3, 2007

Hibernate: Association Mappings in Annotation (JPA style)

Unidirectional Association

Unidirectional Association with Join Table

Bidirectional Association

Bidirectional Association with Join Table

(*) hibernate document chapter

Reference:
- hibernate documentation
- EJB 3.0 Spec


Hibernate: Annotation many-to-many (join table)

Bidirectonal many-to-many (join table) association

Hibernate Doc (Chap 8.5.3)


::Relationship::
person(many) <-> address(many)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddresses();
address.getPeople();

::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  // mapping owner
  @ManyToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @ManyToMany(mappedBy="addresses")  // map info is in person class
  private Set<Person> people;
}



::Generated SQL::
- person.getAddresses();
select addresses0_.personId as personId1_, addresses0_.addressId as addressId1_, address1_.addressId as addressId3_0_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId where addresses0_.personId=?

- address.getPeople();
select people0_.addressId as addressId1_, people0_.personId as personId1_, person1_.personId as personId2_0_ from PersonAddress people0_ left outer join PERSON person1_ on people0_.personId=person1_.personId where people0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (join table)

Bidirectonal one-to-one (join table) association (very unusual)

Hibernate Doc (Chap 8.5.2)


::Relationship::
person(one)(mapping owner) <-> address(one)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddress();
address.getPerson();

::Annotation::



@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @OneToOne(optional=true)
  @JoinTable(name="PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }     
  )
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @OneToOne(optional=true, mappedBy="address") // pointing to Person's address field
  private Person person;   
}



::Generated SQL::
- person.getAddress();
select person0_.personId as personId2_2_, person0_1_.addressId as addressId3_2_, address1_.addressId as addressId4_0_, address1_1_.personId as personId3_0_, person2_.personId as personId2_1_, person2_1_.addressId as addressId3_1_ from PERSON person0_ left outer join PersonAddress person0_1_ on person0_.personId=person0_1_.personId left outer join ADDRESS address1_ on person0_1_.addressId=address1_.addressId left outer join PersonAddress address1_1_ on address1_.addressId=address1_1_.addressId left outer join PERSON person2_ on address1_1_.personId=person2_.personId left outer join PersonAddress person2_1_ on person2_.personId=person2_1_.personId where person0_.personId=?

- address.getPerson();
select address0_.addressId as addressId4_2_, address0_1_.personId as personId3_2_, person1_.personId as personId2_0_, person1_1_.addressId as addressId3_0_, address2_.addressId as addressId4_1_, address2_1_.personId as personId3_1_ from ADDRESS address0_ left outer join PersonAddress address0_1_ on address0_.addressId=address0_1_.addressId inner join PERSON person1_ on address0_1_.personId=person1_.personId left outer join PersonAddress person1_1_ on person1_.personId=person1_1_.personId left outer join ADDRESS address2_ on person1_1_.addressId=address2_.addressId left outer join PersonAddress address2_1_ on address2_.addressId=address2_1_.addressId where address0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-many/many-to-one (join table)

Bidirectonal one-to-many/many-to-one (join table) association

Hibernate Doc (Chap 8.5.1)


::Relationship::
person(one) <-> address(one)


::DB Schema::
person(personId)
personAddress(personId, addressId)
address(addressId)

::Java Operation::
person.getAddress();
address.getPerson();

::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @OneToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @ManyToOne(optional=true)
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="addressId")
    },
    inverseJoinColumns = {
      @JoinColumn(name="personId")
    }
  )
  private Person person;   
}



::Generated SQL::
- person.getAddresses();
select addresses0_.personId as personId2_, addresses0_.addressId as addressId2_, address1_.addressId as addressId3_0_, address1_1_.personId as personId4_0_, person2_.personId as personId2_1_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId left outer join PersonAddress address1_1_ on address1_.addressId=address1_1_.addressId left outer join PERSON person2_ on address1_1_.personId=person2_.personId where addresses0_.personId=?

- address.getPerson();
select address0_.addressId as addressId3_1_, address0_1_.personId as personId4_1_, person1_.personId as personId2_0_ from ADDRESS address0_ left outer join PersonAddress address0_1_ on address0_.addressId=address0_1_.addressId left outer join PERSON person1_ on address0_1_.personId=person1_.personId where address0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (primary-key)

Bidirectonal one-to-one (primary-key) association

Hibernate Doc (Chap 8.4.2)


::Relationship::
person(one) <-> address(one)


::DB Schema::
person(personId)
address(personId)


::Java Operation::
person.getAddress();
address.getPerson();


::Annotation::

@Entity
@Table(name="PERSON")
public class Person {
  
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="personId")
  private int id;
 
  @OneToOne
  @PrimaryKeyJoinColumn
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @Column(name = "personId")
  private int id;

  @OneToOne(mappedBy="address")  // inverse=true, pointnig Person's address field
  private Person person;   
}



::Generated SQL::
- person.getAddress();
select person0_.personId as personId2_2_, address1_.personId as personId3_0_, person2_.personId as personId2_1_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.personId=address1_.personId left outer join PERSON person2_ on address1_.personId=person2_.personId where person0_.personId=?

- address.getPerson();
select address0_.personId as personId3_2_, person1_.personId as personId2_0_, address2_.personId as personId3_1_ from ADDRESS address0_ left outer join PERSON person1_ on address0_.personId=person1_.personId left outer join ADDRESS address2_ on person1_.personId=address2_.personId where address0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (foreign-key)

Bidirectonal one-to-one (foreign-key) association

Hibernate Doc (Chap 8.4.2)


::Relationship::
person(one) <-> address(one)

::DB Schema::
person(id, addressId)
address(id)

::Java Operation::
person.getAddress();
address.getPerson();

::Annotation::

@Entity
@Table(name="PERSON")
public class Person {
   
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="personId")
  private int id;
 
  @ManyToOne
  @JoinColumn(name="addressId")     // inverse = false
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @OneToOne(mappedBy="address")  // inverse=true, pointnig Person's address field
  private Person person;   
}



::Generated SQL::
- person.getAddress();
select person0_.personId as personId2_1_, person0_.addressId as addressId2_1_, address1_.addressId as addressId3_0_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.addressId=address1_.addressId where person0_.personId=?

- address.getPerson();
select person0_.personId as personId2_1_, person0_.addressId as addressId2_1_, address1_.addressId as addressId3_0_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.addressId=address1_.addressId where person0_.addressId=?

[Association Mapping List]

Hibernate: Annotation one-to-many/many-to-one

Bidirectonal one-to-many/many-to-one association

Hibernate Doc (Chap 8.4.1)

::Relationship::
person(many) <-> address(one)
- person A and person B live in address AA

::DB Schema::
person(personId, addressId)
address(addressId)


::Java Operation::
person.getAddress();
address.getPeople();

::Annotation::

@Entity
@Table(name="PERSON")
public class Person {
   
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name="personId")
  private int id;
   
  @ManyToOne
  @JoinColumn(name="addressId")     // inverse = false
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;

  @OneToMany(mappedBy="address")  // pointing Person's address field
  @Column(name="personId")    // inverse=true
  private Set<Person> people;
}



::Generated SQL::

[Association Mapping List]

Hibernate: Annotation many-to-many (join table)

Unidirectonal many-to-many (join table)) association

Hibernate Doc (Chap 8.3.4)


::Relationship::
person(many) -> address(many)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddresses();

::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @ManyToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}  


@Entity
@Table(name = "ADDRESS")
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;
}



::Generated SQL::
- person.getAddresses();

select addresses0_.personId as personId1_, addresses0_.addressId as addressId1_, address1_.addressId as addressId3_0_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId where addresses0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-one (join table)

Unidirectonal one-to-one (join table) association

Hibernate Doc (Chap 8.3.3)

::Relationship::
person(one) -> address(one)

::DB Schema::
person(personId)
address(addressId)
personaddress(personId, addressId)

::Java Operation::
person.getAddress();

::Annotation::

@Entity
@Table(name = "PERSON")
@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @OneToOne(optional=true)
  @JoinTable(name="PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }     
  )
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;  
}



::Generated SQL::
- person.getAddress();

select person0_.personId as personId2_1_, person0_1_.addressId as addressId3_1_, address1_.addressId as addressId4_0_ from PERSON person0_ left outer join PersonAddress person0_1_ on person0_.personId=person0_1_.personId left outer join ADDRESS address1_ on person0_1_.addressId=address1_.addressId where person0_.personId=?

[Association Mapping List]

Hibernate: Annotation many-to-one (join table)

Hibernate Doc (Chap 8.3.2)



::Relationship::
person(many) -> address(one)

ex) person A and person B live in address AA

::DB Schema::
person (personId)
addres (addressId)
personAddress (personId, addressId)

::Java Operation::
person.getAddress();


::Annotation::

@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @ManyToOne(optional=true)
  @JoinTable(name="PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }     
  )
  private Address address;
}


@Entity
@Table(name = "ADDRESS")
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;
}



::Generated SQL::
- person.getAddress();

select person0_.personId as personId2_1_,
person0_1_.addressId as addressId3_1_,
address1_.addressId as addressId4_0_
from PERSON person0_ left outer join PersonAddress person0_1_ on person0_.personId=person0_1_.personId
left outer join ADDRESS address1_ on person0_1_.addressId=address1_.addressId
where person0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-many( join table)

Unidirectonal one-to-many (join table) association

Hibernate Doc (Chap 8.3.1)


::Relationship::
person(one) -> address(many)

::Java Operation::
person.getAddresses();


::Annotation::


@Entity
@Table(name = "PERSON")
public class Person {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "personId")
  private int id;

  @OneToMany
  @JoinTable(name = "PersonAddress",
    joinColumns = {
      @JoinColumn(name="personId", unique = true)           
    },
    inverseJoinColumns = {
      @JoinColumn(name="addressId")
    }
  )
  private Set<Address> addresses;
}


@Entity
@Table(name = "ADDRESS")
public class Address {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "addressId")
  private int id;
}



::Generated SQL::
- person.getAddresses();
select addresses0_.personId as personId1_, addresses0_.addressId as addressId1_, address1_.addressId as addressId3_0_ from PersonAddress addresses0_ left outer join ADDRESS address1_ on addresses0_.addressId=address1_.addressId where addresses0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-many (foreign-key)

Hibernate Doc (Chap 8.2.3)

::Relationship::
person(one) -> address(many)

::DB Schema::
person( personId )
address( addressId, personId )

::Java Operation::
person.getAddresses();

::Annotation::
@Entity
@Table(name="PERSON")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;

@OneToMany
@JoinColumn(name="personId") 
private Set <Address> addresses;
}

@Entity
@Table(name = "ADDRESS")
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
}

::Generated SQL (mysql5.0)::
[Association Mapping List]

Monday, July 2, 2007

Java Collection finder methods(spring & ruby style)

Using call-back interface(spring way), I wrote finder methods(block call method in ruby).
I hope this closure-style finding methods provide more human readable sourcecode instead of for-looping.

I've needed to search object(s) in List. In the first place, I repeated for-loops to get objects and check whether it matches criteria. Unfortunately, it was not clean-looking-sorucecode for me.

Unfortunately, only final variables are allowed in callback implementation.

Sample Usage(Junit style)

public class SearcherTest {

private List<Person> people;
private Person p1 = new Person("foo", 1);
private Person p2 = new Person("bar", 2);
private Person p3 = new Person("baz", 3);
private CollectionSearcher<Person> searcher;

@Before
public void setUp() {
people = new ArrayList<Person>();
people.add(p1);
people.add(p2);
people.add(p3);

searcher = new CollectionSearcher<Person>(people);
}


@Test
public void testFind() {
Person result = searcher.find(new SearchStrategy<Person>() {
public boolean isEqual(Person person) {
return person.getName().equals("bar");
}
});

assertSame(p2, result);
}

@Test
public void testFindAll() {
List<Person> result = searcher.findAll(new SearchStrategy<Person>() {
public boolean isEqual(Person person) {
return person.getName().startsWith("b");
}
});

assertEquals(2, result.size());
assertSame(p2, result.get(0));
assertSame(p3, result.get(1));
}

@Test
public void testCollect() {
List<Person> result = searcher.collect(new CollectStrategy<Person>() {
public Person compare(Person person) {
if (person.getId() > 2) {
return person;
}
return null;
}
});

assertEquals(1, result.size());
assertEquals(3, result.get(0).getId());
}

@Test
public void testIndexOf() {
int index = searcher.indexOf(new SearchStrategy<Person>() {
public boolean isEqual(Person person) {
return person.getName().equals("baz");
}
});

assertEquals(2, index);
}

@Test
public void testContains() {
boolean result = searcher.contains(new SearchStrategy<Person>() {
public boolean isEqual(Person person) {
return person.getName().equals("baz");
}
});
assertTrue(result);

result = searcher.contains(new SearchStrategy<Person>() {
public boolean isEqual(Person person) {
return person.getName().equals("foobar");
}
});
assertFalse(result);
}


static class Person {
private String name;
private int id;


public Person(String name, int id) {
this.name = name;
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getId() {
return id;
}

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



Implemenatation


public class CollectionSearcher<E> {
private Collection<E> collection;

public CollectionSearcher() {
}

public CollectionSearcher(Collection<E> collection) {
this.collection = collection;
}

/**
* return the first element that given strategy returns true
*
* @param strategy
* @return
*/
public E find(SearchStrategy<E> strategy) {
for (E entry : collection) {
if (strategy.isEqual(entry)) return entry;
}
return null;
}

/**
* returns all element that given strategy returns true
*
* @param strategy
* @return
*/
public List<E> findAll(SearchStrategy<E> strategy) {
List<E> results = new ArrayList<E>();
for (E entry : collection) {
if (strategy.isEqual(entry)) results.add(entry);
}
return results;
}

/**
* return list of elements that strategy returns
* if strategy returns null, then it won't be in the list
*
* @param strategy
* @return
*/
public List<E> collect(CollectStrategy<E> strategy) {
List<E> results = new ArrayList<E>();
for (E entry : collection) {
E result = strategy.compare(entry);
if (result != null) results.add(result);
}
return results;
}

/**
* returns the first index of element that strategy returns true, or -1 if none of the element satisfies the strategy
*
* @param strategy
* @return
*/
public int indexOf(SearchStrategy<E> strategy) {
int index = 0;
for (E entry : collection) {
if (strategy.isEqual(entry)) return index;
index++;
}
return -1;
}

/**
* returns true if the collection has an element that satisfies given strategy
* @param strategy
* @return
*/
public boolean contains(SearchStrategy<E> strategy) {
for (E entry : collection) {
if (strategy.isEqual(entry)) return true;
}
return false;
}


public Collection<E> getCollection() {
return collection;
}

public void setCollection(Collection<E> collection) {
this.collection = collection;
}
}


Search Strategies

public interface CollectStrategy<T> {
T compare(T object);
}

public interface SearchStrategy<T> {
/**
* Returns true if the given object satsfies criteria
* @param object
* @return
*/
boolean isEqual(T object);
}

Thursday, March 22, 2007

Performance: clone() vs. new GregorianCalendar()

Performance for getting a new Calendar Object

::Environment::
- JDK "1.6.0" (build 1.6.0-b105)
- Linux (CentOS 4.4)

::Result::
loopby Clone
by MilliSec
by Setter
by Constructor
100
11ms
11ms
5ms
2ms
1000
59ms
61ms
9ms
6ms
10000
85ms
71ms
59ms
19ms
30000
178ms
136ms
102ms
28ms
60000
279ms
249ms
189ms
42ms



::Code::
By Clone

public long byClone(final int loop) {
final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = (Calendar) Calendar.getInstance().clone();
}
final long end = System.currentTimeMillis();
return end - start;
}


By Setting MilliSeconds

public long byMilliSeconds(final int loop) {
final Calendar now = Calendar.getInstance();
final TimeZone tz = now.getTimeZone();
final long mill = now.getTimeInMillis();

final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = new GregorianCalendar(tz);
cal.setTimeInMillis(mill);
}
final long end = System.currentTimeMillis();

return end - start;
}


By Setting Each Field

public long bySetter(final int loop) {
final Calendar now = Calendar.getInstance();
final int currentYear = now.get(Calendar.YEAR);
final int currentMonth = now.get(Calendar.MONTH);
final int currentDate = now.get(Calendar.DATE);
final int currentHour = now.get(Calendar.HOUR);
final int currentMinute = now.get(Calendar.MINUTE);
final int currentSecond = now.get(Calendar.SECOND);
final int currentMilliSecond = now.get(Calendar.MILLISECOND);
final TimeZone tz = now.getTimeZone();

final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = new GregorianCalendar(tz);
cal.set(currentYear, currentMonth, currentDate, currentHour, currentMinute, currentSecond);
cal.set(Calendar.MILLISECOND, currentMilliSecond);
}
final long end = System.currentTimeMillis();

return end - start;
}


By Constructor

public long byConstructor(final int loop) {
final Calendar now = Calendar.getInstance();
final int currentYear = now.get(Calendar.YEAR);
final int currentMonth = now.get(Calendar.MONTH);
final int currentDate = now.get(Calendar.DATE);
final int currentHour = now.get(Calendar.HOUR);
final int currentMinute = now.get(Calendar.MINUTE);
final int currentSecond = now.get(Calendar.SECOND);
final int currentMilliSecond = now.get(Calendar.MILLISECOND);
final TimeZone tz = now.getTimeZone();

final long start = System.currentTimeMillis();
for (int i = 0; i < loop; i++) {
Calendar cal = new GregorianCalendar(currentYear, currentMonth, currentDate, currentHour, currentMinute, currentSecond);
cal.set(Calendar.MILLISECOND, currentMilliSecond);
cal.setTimeZone(tz);
}
final long end = System.currentTimeMillis();

return end - start;
}

Friday, March 9, 2007

New DI Container from Google

Google released their new Dependency Injection container.
- Google Guice

I found it from Joe Walker's Blog and Bob Lee's blog!!

Nice to have java5 annotation for DI info!!
At my first glimpse, I feel google's developers like to write everything in source code as you see GWT write codes in Java, instead of writing configuration files.

Personally, I agree with their approach.
I haven't had experience that a change only requires modifying configuration files and deploy it without compilation.

My current app uses Spring and configuration files are getting big. Unfortunately I can't say it is clean.(well, there are many ways to deal with it. for example changing loader to read files with our own convention rules)
So, I may feel it's rather convenient writing certain kind of information in source code because we can take advantage of our intelligent IDE.

In Japan, there is yet another famous DI container called Seasar. This is also towarding to less configuration files choosing "Convention Over Configuration" rule.

It's fun to have new DI technologies!!

Thursday, March 8, 2007

Hibernate: Annotation one-to-one (primary-key)

Hibernate Doc (Chap 8.2.2)


::Relationship::
person(one) -> address(one)


::DB Schema::
person( personId )
address( personId )
* address's primary key is same as person's primary key


::Java Operation::
person.getAddress();


::Annotation::
@Entity
@Table(name="PERSON")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;

@OneToOne
@PrimaryKeyJoinColumn
private Address address;

public Address getAddress() {
return address;
}
}

@Entity
@Table(name = "ADDRESS")
public class Address {
@Id
@Column(name = "personId")
private int id;
}



::Generated SQL (mysql5.0)::
- person.getAddress();
select person0_.personId as personId2_2_, address1_.personId as personId3_0_, person2_.personId as personId2_1_ from PERSON person0_ left outer join ADDRESS address1_ on person0_.personId=address1_.personId left outer join PERSON person2_ on address1_.personId=person2_.personId where person0_.personId=?

[Association Mapping List]

Hibernate: Annotation one-to-one(foreign-key)

Hibernate Doc (Chap 8.2.2)


::Relationship::
person(one) -> address(one)


::DB Schema::
person( personId, addressId )
address( addressId )


::Java Operation::
person.getAddress();


::Annotation::
@Entity
@Table(name="PERSON")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="personId")
private int id;

@OneToOne
@JoinColumn(name="addressId")
private Address address;
}

@Entity
@Table(name = "ADDRESS")
public class Address {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "addressId")
private int id;
}


[Association Mapping List]

Tuesday, February 27, 2007

Hibernate: Annotation many-to-one(foreign-key)

Hibernate Doc (Chap 8.2.1)


::Relationship::
person(many) -> address(one)


::DB Schema::
person( personId, addressId )
address( addressId )


::Java Operation::
person.getAddress();


::Annotation::
@Entity
@Table(name="PERSON")
public class Person {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@ManyToOne
@JoinColumn(name = "ADDRESS_ID")
private Address address;

public Address getAddress() {
return address;
}
}

@Entity
@Table(name="ADDRESS")
public class Address {   

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
}


[Association Mapping List]

Thursday, February 15, 2007

Hibernate: When is "inverse=true" and when it's not?

When I was a hibernate beginner, I was confused about deciding "inverse=true" or "inverse=false".

Here is an easy way to understanding it:

example scenario:
Person(one) <-> Address(many)
* bi-directional one-to-many relationship. (A person has multiple addresses.)


public class Person {
private Integer id;
private Set<Address> addresses;

// setter, getter
Set<Address> getAddresses() { return addresses; }
....
}


public class Address {
private Integer id;
private Person person;

// setter, getter
Person getPerson() { return person; }
.....
}
  • Person class has "Set<Address> getAddresses()" method
  • Address class has "Person getPerson()" method

If you think about the relation between two classes, it may gives an idea that person has addresses. (Person -> Addresses)
So, it feels like a person is an owner, and an address is a child. Then, you want to think that address is "inverse=true" because address is owned by person.
However, it's not correct.


Here, I'd like to suggest a way to think about it.

Let's look at table structure instead of classes.
  • PERSON[ id, name, ...]
  • ADDRESS[ id, person_id, city, street,...]

The person_id column in Address table is the relational information between thease two tables.
So, Address is an owner of the relationship, and Person is the inverse side.
"inverse=true" means "this side is the inverse side", and "inverse=false" means "this is not the inverse side. this side is the owner of the relationship".


Answer is:

<class name="Person">
<id name="id">...</id>
<set name="addresses" inverse="true">
<key column="person_id"/>
<one-to-many class="Address"/>
</set>
</class>


<class name="Address">
<id name="id">...</id>
<many-to-one name="person" class="Person" />
</class>



In sum, look at the table structure to distinguish "inverse=true" or "inverse=false".
If the table has relational information, then it is the owner side. So, it's not inverse side.(inverse=false)
If the table doesn't have relational information, then it is the inverse side. So, it needs "inverse=true".

Sunday, February 11, 2007

Hibernate: Single Shot Query to get Count and Data by ScrollableResults

Pagination is one issue when you display list-style data.
Usually it requires two kinds of data: "certain range of objects"(offset, limit) and "total number of data"(count).

There is a couple of ways to implement this.

(1) Use two queries. one for total number(count) and one for ranged data(offset,limit).
(2) Load all data into java layer, then check the size of list.

(2) is not practical if you have large number of data and displaying only a few of them, it has huge overhead.
So, I have seen couple of projects using (1) style to display list of data and total number of them.

For retrieving ranged data, hibernate provides "setFetchSize(int)" and "setFirstResult(int)" to specify offset and limit.
This way, application only instantiate ranged return objects.
Also, these two methods separate pagination information from hql and absorbes vendor specific offset and limit SQL.

For example, HQL that retrieves all active users is :
"from User where active = true"
you may easily retrieve ranged users from offset 10 to 30, 30 to 50...

Now you need to retrieve total number of active users in order to display how many pages it has.
For total number of active users, you may use this hql.
"select (*) from User where active = true"

Then, how do you manage those almost similar two queries?

From my experience, some of the applications use string manipulation that dynamically replace or add select clause to HQL statement.
(Adding "select count(*)" is easy to do because hql statement can start with from-clause. )
But replacing may not easy if the hql already has select clause. "select id, name from User where active = true".
You can write some methods to replace it but the source code may not seem neat.

Some applications prepare two hql statements, one for total number and the other for data.


If your JDBC happily supports scrollable result set, here is one way to retrieve count and data by single shot query.


public ResultList scrollCount(final String hql, final int scrollOffset,
final int scrollSize) {
return (ResultList) getHibernateTemplate().execute(
new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {

Query q = session.createQuery(hql);
ScrollableResults rs = q.scroll(ScrollMode.FORWARD_ONLY);

rs.beforeFirst();
rs.scroll(scrollOffset);

ResultList resultList = new ResultList();
for (int i = 0; i < scrollSize && rs.next(); i++) {
Object[] results = rs.get();
// flatten only if one object per row
resultList.add(results.length == 1 ? results[0]
: results);
}

rs.last();
final int total = rs.getRowNumber() + 1; // start with -1
resultList.setTotalSize(total);

return resultList;
}
});
}



public class ResultList extends ArrayList {
private int totalSize;

public int getTotalSize() {
return totalSize;
}

public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
}


You can put it into your dao class, probably in generic dao, base dao, abstract dao...

So, now you don't need to do any string manipulation.
I'm not sure about the cost of moving result set.
But single query can retrieve required two data.
From maintaining source code view, it also reduces duplication.

Friday, February 9, 2007

Hibernate: tune up Named Query initialization time

Our application uses Hibernate with many named queries.
Whenever I start up jboss, parsing the named queries is one of the time consuming initialization process.

I read a post on hibernate JIRA regarding preparing named query.
It says, instead of writing named queries in mapping files, creating named-query-only-files gives faster startup.

So, I tried it.

Hiberante3 & Jboss4
Num of NamedQuery: 405

Named queries exist within entity mapping(our current status):


start up time
1.
2m:33s:301ms
2.
2m:15s:737ms
3.
2m:25s:113ms
4.
2m:15s:753ms
5.
2m: 1s:706ms
6.
2m: 2s:941ms
7.
2m:19s:722ms
8.
2m: 8s:769ms
9.
2m:18s:473ms
10.
2m:14s: 82ms
AVG
2m:15s:633ms


Moved approximately half of named query to a query-only-file.
(moving all queries takes time, so I just slacked…)



start up time
1.
1m:52s:300ms
2.
2m: 7s:191ms
3.
2m:11s:566ms
4.
2m: 7s:581ms
5.
1m:51s:799ms
6.
2m:19s:613ms
7.
1m:49s:612ms
8.
2m: :675ms
9.
1m:58s: 50ms
10.
2m:10s:675ms
AVG
2m: 2s:951ms


::Result::
Average start up time : 2m:15s:633ms => 2m: 2s:951ms


Well, start-up time varies. But surely it makes startup faster.
If I move all named-queries to a couple of query-only-files, we will gain more performance.

Monday, February 5, 2007

Hello World!!

Well, here is my first step for my blog.
Who am I?
I'm a software developer who loves to create application very nice and clean.
What makes application nice and clean? My opinion is make its architecture simple and standard.
I have experienced many applications that some guys created with wrong use of technologies.
Accessing database very aggressively in taglib, putting many objects into HttpSession, giviing HttpRequest object to DAO layers, creating own wicked web action handling framework using opensource web frameworks, and unfortunately more and more...
Well, I'm not always correct but at least let's create maintainable and understandable application, and let's make other developers happy to develop it!!
For this blog, I don't know what kind of topic I'll post yet, but I'm hoping I can share my knowledge to other developers.
Lastly, English is my second language. So, bear with me when my writing sounds awkward!!
Thank you for reading!!