Thursday, September 23, 2010

hibernate criteria API exists subquery

SELECT *
FROM service_type
WHERE EXISTS (SELECT 1
FROM service
WHERE service.active = 1
AND service.is_57 = service_type.id)


Criteria criteria = session.createCriteria(ServiceType.class, "servicetype");
DetachedCriteria serviceCriteria = DetachedCriteria.forClass(Services.class, "service");
serviceCriteria.add(Restrictions.eq("ACTIVE", new Integer(1)));
serviceCriteria.add(Property.forName("servicetype.ID").eqProperty("service.IS_57"));
criteria.add(Subqueries.exists(serviceCriteria.setProjection(Projections.property("service.ID"))));
return criteria.list();

Tuesday, September 21, 2010

I hate hibernate

today's problem
I see it reported by others as well:
http://www.theserverside.com/discussions/thread.tss?thread_id=37010
http://www.coderanch.com/t/422591/ORM/java/Hibernate-one-many-association-foreign


foreign keys when the column name on child side differs from the primary key

in JOB_HRD mapping file, key is JOB_DI, foreign column is SERVICE_ID
<set name="SERVICES" cascade="all" inverse="true" lazy="true" outer-join="auto">
<key column="ID"/>
<one-to-many class="at.oeamtc.oop.hybernate.Services"/>
</set>
the generated join query is JOB_HDR.JOB_ID = SERVICE.ID and not the wanted one : JOB_HDR.SERVICE_ID = SERVICE.ID

second attempt to use many-to-one
<many-to-one name="services" class="at.oeamtc.oop.hybernate.Services" insert="false" update="false">
<column name="SERVICE_ID"></column>
</many-to-one>
when there is property SERVICE_ID in the mapping file

first complained that there is such a property SERVICE_ID and that update and insert into many-to-one should be set to false; after set to false, it complains that it cannot set the corresponding property (in this case services in JobHeader class)...

the only solution found, to remove the other mapping of SERVICE_ID

and all these to be able to write a join subquery with Criteria API...

-------------------
finally solved:
dual mapping on SERVICE_ID is possible but beside update='false' and insert='false' it is needed also cascade='none':
<many-to-one name="service" class="at.oeamtc.oop.hybernate.Services" cascade="none" insert="false" update="false" >
<column name="SERVICE_ID"></column>
</many-to-one>

getter and setter methods to be defined in java bean class, this time no setter is called in the constructor (as I did the previous time)

and the query is:
Criteria criteria = session.createCriteria(Jobs.class);
//add restrictions / expressions

//join with job_header
criteria = criteria .createCriteria("JOB_HDR");
//add further restrictions / expressions

//join with service:
//note that service is the name of many-to-one relationship in the mapping file
//as above JOB_HDR is many-to-one in the mapping file for jobs
crit = crit.createCriteria("service").add(Expression.eq("IS_57", serviceTypeId));

Tuesday, September 14, 2010

metro (JAX-WS 2.2.) web service - down up approach

starting from java code when generating wsdl by using wsgen, I do not see collection elements in XSD (when only getters are used), i.e.

for
public class Cheque implements java.io.Serializable {

Integer id;
Customer customer;
List chequeRows = new ArrayList();

public get List() {
return chequeRows;
}

//no setter for chequeRows is used
//instead getChequeRows().add() is used to add cheque rows to the list
//...
}

and the result xsd does not have chequeRow elements

to force it - annotation @XMLElement is used:

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlElement;


@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)

public class Cheque implements java.io.Serializable {

@XmlElement
Integer id;

@XmlElement
Customer customer;

@XmlElement
List chequeRows = new ArrayList();

public get List() {
return chequeRows;
}

//no setter for chequeRows is used
//instead getChequeRows().add() is used to add cheque rows to the list
//...
}


also, it is necessary to use @WebParam and @WebResult annotations with name="" otherwise parameters are named 'paramx' and the method result is named 'return';
targetNamespace has to be specified though

Thursday, September 2, 2010

impressed by Sprarx Systems Enterprise Architect v.8

among other nice new features, EA can be used as a project management tool as well
Sparx Systems

Wednesday, September 1, 2010

metro (JAX-WS 2.2.) web service on java5

ok, a web service implemented with JAX-WS 2.2. is deployed and running under
tomcat 7.0.0 and JDK 6.0_21
I am very happy with the performance
but I do need it running on tomcat6 with jre1.5, so...

according to the documentation JAX-WS 2.2. should work on java 5 update 2

0) precondition
metro 2.0.1 is installed

1) compilation

. javax.annotation.PostConstruct and javax.annotation.PreDestroy for java5 may be found in jsr250-api.jar from Sun JWSDP 2.0 (--> \jaxws\lib );
. @Override for interfaces implementation is not supported? -> annotations are commented
-> compilation of the web service with JDK 1.5.0_07 is successful

2) deployment
under tomcat 6.0.18, JRE 1.5.0_07
as written in readme of metro2.0.1 use metro-on-tomcat.xml
just run
ant -Dtomcat.home= -f /metro-on-tomcat.xml uninstall
(which just copy:
. webservices-api.jar and jsr173_api.jar to \endorsed
. webservices-extra.jar, webservices-extra-api.jar, webservices-rt.jar and webservices-tools.jar to \shared\lib
. )

it is working, I am impressed

TODOs

cloud computing: salesforce