Wednesday, September 21, 2011

Calling procedure using SQL

In my case procedure does not need to return anything.
Calling it from DAO layer:

String queryString = "call myProcedure()";
Query query = getSession().createSQLQuery(queryString);
query.executeUpdate();

Thursday, September 15, 2011

Associations via criteria

Fetch data from one table by criteria on linked table

Person has Address. (1:1)
Address has field street.
I want to fetch all Persons that has Address with with specific street.
DetachedCriteria criteria= DetachedCriteria.forClass(Person.class);
criteria.createCriteria("address").add(Restrictions.eq("street", "WANTED"));

SELECT *
    FROM
        PERSON p
    inner join
        ADDRESS a
            on p.addressID=a.ID 
  WHERE
a.street=?

Fetch data from one table by having foreign key of linked table

Object A has 1:1 relation to object B.
Table A has foreign key to table B.
Class A has B.
I want to fetch class A and I only have ID of class B.
How to construct criteria?
SELECT * FROM a WHERE b_ID=?
//createCriteria on class A
criteria.add(Restrictions.eq("b.id", b.getId()));
criteria.list();


Fetch data from 3 linked tables

DPT has 1:n relation to OFFCOURTPAYMENT,
OFFCOURTPAYMENT has 1:n relation to SUBJECTASSEMBLY
How to fetch DPTs that belong to specific SUBJECTASSEMBLY?
select d.* from DPT d
INNER JOIN OFFCOURTPAYMENT o ON D.ACTIVEOCPOID = O.OID
INNER JOIN SUBJECTASSEMBLY sa ON SA.OID = O.SUBJECTASSEMBLYOID
where d.COMPANYOID= coid (4)
and D.CURRENTSTATE = 'MIROVANJE'
and SA.RECORDNUMBER = '1'
and SA.SUBJECTABLETYPE = 'OCS_DPT'


SubjectAssembly sa is passed in method
DetachedCriteria crit = DetachedCriteria.forClass(DailyParkingTicket.class);
crit.add(Restrictions.eq("currentDPTState", DPTState.StateName.MIROVANJE));
DetachedCriteria saCrit = crit.createCriteria("activeOcp").createCriteria("subjectAssembly"); // 2 inner joins will be generated
saCrit.add(Restrictions.eq("recordNumber", sa.getRecordNumber()));
saCrit.add(Restrictions.eq("subjectableType", sa.getSubjectableType()));
criteria.getExecutableCriteria(getSession()).list();


Notes

I want to fetch (via join) objects associated with my source object A:
A has list of Bs and B has C.
DetachedCriteria criteria= DetachedCriteria.forClass(A.class);
criteria.setFetchMode("B", FetchMode.JOIN); //must be present!
criteria.setFetchMode("B.C", FetchMode.JOIN);



// without DISTINCT_ROOT_ENTITY @ManyToMany returns full cartesian join!
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

Wednesday, September 14, 2011

@Column annotation and constraints in db


@Column(unique = true, nullable = false)
private Date startDate;

This annotation is description on startDate column when reading java file.
It is not used to stop inserting or updating database with false values, if you do not have constraints on table!
It can be used for generating table from entity class.

1 exception to the above rule:
If you try insert null into not null field declared in annotation it will throw Exception!