Posts Tagged ‘ hibernate

Release Notes: com.altuure.yagdao-0.3.jar

Append Method handler

Another dynamic query builder. But this one , use simple StringBuilder instead of trying to parse the argument.  So more fast and adjustable dynamic query builder

For more see test cases and example dao

   @YMethod(type = YMethodType.APPEND,select = "pbyte,count(id)",groupBy = "pbyte")
    List<SimpleBean> append2(@YParameter("pint>=?") int i);

    @YMethod(type = YMethodType.APPEND,select = "pbyte,count(id)",groupBy = "pbyte",having = "count(id)>10")
    List<SimpleBean> append3(@YParameter("pint>=?") int i);

group and having support with aggregation support

Append and JPA criteria methods now support Projections

Execute Method handler

bulk method support

   @YMethod(type = YMethodType.EXECUTE,query = "update SimpleBean set pint=:newInt where id=:id ")
    int execute1(@YParameter(value = "newInt") int newInt,@YParameter(value = "id") long id);

experimental criteria query parser for JPA

Criteria method is now support more sophisticated queries . please see test cases and example dao

    @YMethod(type = YMethodType.CRITERIA)
    SearchResultList<SimpleBean> criteria2(@YParameter("pint >=") Integer arg1,@YParameter("pint <=") Integer arg2);

    @YMethod(type = YMethodType.CRITERIA)
    SearchResultList<SimpleBean> criteria3(@YParameter("pstring is null") boolean apply);

    @YMethod(type = YMethodType.CRITERIA)
    SearchResultList<Object> criteria2CheckApplied(@YParameter("pint>=20000") boolean apply1,@YParameter( "pint<=50000") boolean apply2);

com.altuure.yagdao-0.2.jar

I published for version of the yagdao (Yet Another Generic DAO) finally.

this version is a little bit experimental but stable.

yagdao is an generic dao framework  that would be enabled by annotations.

simple step to go through for setup read more

  • add jar into your classpath
  • extend GenericDAO

moreover you can implement custom method read more

Retrieving Id field from JPA and Hibernate

quick code snippet to get the identifier field name for a given entity class from both hibernate and JPA 2.0 APIs.

for hibernate

public String getIdProperty(Class entityClass) {
String idProperty=sessionAccessor.getSessionFactory()
                                              .getClassMetadata(entityClass)
                                              .getIdentifierPropertyName();
return idProperty;
}

for JPA 2.0 metamodel API this method or to get value of id field see comment below

public String getIdProperty(Class entityClass) {
        String idProperty;
        Metamodel metamodel = getEntityManager().getMetamodel();
        EntityType entity = metamodel.entity(entityClass);
        Set<SingularAttribute> singularAttributes = entity.getSingularAttributes();
        for (SingularAttribute singularAttribute : singularAttributes) {
            if (singularAttribute.isId()){
                idProperty=singularAttribute.getName();
                break;
            }
        }
        if(idProperty==null)
            throw new RuntimeException("id field not found");
        return idProperty;
    }

to

hibernate criteria API bug: unnecessary fetch

I unfortunately came across a bug on hibernate and I think it is pretty match important for anyone who is using hibernate criteria API and caring about performance.

Although this is an important, it is a hidden bug unless you trace you generated SQL for hibernate.

simply the two lines of code below should generate same SQL

        Criteria rootCriteria = session.createCriteria(OrderItem.class);
        Criteria criteria = rootCriteria.createCriteria("order").createCriteria("customer");
        List<OrderItem> list1 = criteria.add(Restrictions.eq("city", "city-1")).list();
        List<OrderItem> list2 = session.createQuery("select s from OrderItem s where s.order.customer.city=?").setString(0,"city-1").list();
        assertEquals(list1, list2);

but when you trace the generated SQL you can see the huge difference. it simply changes fetchmode defaults and ignore any manual fetchmode setting

SQL from criteria API

select this_.id as id1_2_, this_.name as name1_2_, this_.order_id as order5_1_2_, this_.product_id as product6_1_2_, this_.quantity as quantity1_2_, this_.totalPrice as totalPrice1_2_, order1_.id as id0_0_, order1_.customer_id as customer7_0_0_, order1_.deliveryDate as delivery2_0_0_, order1_.name as name0_0_, order1_.orderDate as orderDate0_0_, order1_.priority as priority0_0_, order1_.totalValue as totalValue0_0_, customer2_.id as id3_1_, customer2_.city as city3_1_, customer2_.name as name3_1_ from erp_orderitem this_ inner join erp_order order1_ on this_.order_id=order1_.id inner join erp_customer customer2_ on order1_.customer_id=customer2_.id where customer2_.city=?

SQL generate from hql

select orderitem0_.id as id1_, orderitem0_.name as name1_, orderitem0_.order_id as order5_1_, orderitem0_.product_id as product6_1_, orderitem0_.quantity as quantity1_, orderitem0_.totalPrice as totalPrice1_ from erp_orderitem orderitem0_ cross join erp_order order1_ cross join erp_customer customer2_ where orderitem0_.order_id=order1_.id and order1_.customer_id=customer2_.id and customer2_.city=?

you can find more discussion on

https://forums.hibernate.org/viewtopic.php?f=1&t=962905

http://opensource.atlassian.com/projects/hibernate/browse/HHH-3524

PS:if you use hibernate criteria API , please vote for the bug.

hibernate tip: smart id generator

A small tip to generate more meaningful Id. Instead of 32 for OrderID use O000000032.

Use smart and meaningful ids

Use smart and meaningful ids

Simple implementations:

  • SmartIdTableGenerator
  • SmartIdSequenceGenerator

Read more

Clean database schema with inheritance

ORM Diagram

It is very common way of usage that  when ever you need some new attribute for your model objects/table you add a new column to the related database table.  After several requirements you will notice: Read more

Grails Days 1: GORM-CRUD

A basic calculator
Image via Wikipedia

It has been more than a week since I downloaded Grails, Although my first Grails impressions are not so well, I am playing with Grails with lots of curiosity and joy, Read more