Parallel between nurturing mind and garden

I love flowers (who doesn’t 🙂 ), specially rose. I am attracted to their beauty, fragrance and color. I was born and brought up in the Mumbai, its very congested city. I always wanted to have garden of my own, but real estate in Mumbai  is very expensive. Last year I bought house in US, the previous owner had nice maintained garden with 4 different color\kind of roses. I was happy to get chance to learn and practice gardening.

As I was acquiring gardening skills, i found there is parallel between nurturing mind and garden.

Parallel 1

In gardening its difficult to manage  nice lawn. There are all kinds of weeds that start growing. I tried different weedicides, but they were not very effective. The best way was –  To monitor the lawn very 4 to 5 days and if there are any weeds, remove them immediately from roots. My front and back yard are small so  it does not take much time to inspect the lawn.

If you consider mind as garden. The negative thoughts are weeds in this case. When negative thoughts come to mind, the best thing is to remove them from the root. We should monitor our thought process and remove the negative thought as soon as they appear. If this is not done, they will become rampant and out of control like weeds.

Parallel2

There was 2 x 2 feet patch on the lawn which did not have any grass on it. I observed that weeds in this area grew more faster then any other place in my lawn. I was able to find the reason for this on the internet. The soil has nutrients in it, these nutrient can be lost because of sun rays. So to protect its surface from sun, soil with no lawn on it, prompts the growth of weeds. This is symbiotic behavior.

The mind is similar to soil. It wants to keep itself occupied with some thought or work. If there is not positive thoughts (lawn) it fill it up with useless or negative thoughts (weeds). There is saying “An idle mind is the devil’s workshop”. So make sure we keep your self occupied with positive thoughts\ideas. I have some suggestions

  1. Maintain todo list. Whenever you find yourself free do the next item on do to list.
  2. Whenever you done with your work, see if your spouse needs help. Help him or her out.
  3. If done with 1 and 2, see if you can volunteer in your community.

This item will not only keep your mind occupied but also increase your self esteem.

Parallel3

As mentioned early the weeds is big problem. We do not plant weeds, they just grow without invitation. If we like to  have something beautiful and productive like vegetable or flower bed, we need work hard for it. We need to prepare the soil first, plant the seeds. Once seed matures to plant, we need to protect it from pests, rodents etc. We need to fertilize the soil on regular basis.

Same is case with mind. We do not need to plant negative thought they appear from nowhere like weeds. But we need to have positive thought, we need to plant it. We need to protect it from negativity of surrounding. Nurture it so that positive thought can grow into productive output. In short it takes lot of work. The result will be some fragrant rose or tasty fruits.

Advertisements

Jibx + xsi:nil=”true”

Jibx is nice framework for marshaling (java object to xml) and un-marshaling (xml to java obj).

In case of marshaling, if an element is null, Jibx does not populate that element in generated xml.  If it required element as per XSD it throws error indicating that require field is absent in java object.

So if you want to  populated xsi:nil=”true”  in case an element is null. There is not easy work around.

There is crude hack, where you define in the binding that said element is required (by not specifying usage=”optional”).  It may not be required element in XSD but that’s OK. This just to force Jibx not to ignore the element in response. And also mark element as nillable=”true”. 

<binding name=”sample_with_long” package=”com.maventechnologies” trim-whitespace=”true”>
<namespace uri=”http://www.maventechnologies.com&#8221; default=”elements” prefix=”tns”/>
<mapping class=”com.maventechnologies.Account” name=”Account”>
<value style=”element” name=”AccountID” get-method=”getAccountID” set-method=”setAccountID” nillable=”true”/> <!– usage=”optional” is not present so jibx think it required element –>
</mapping>
</binding>

So this causes Jibx to think this element is require and nillable so if value is null in java object it will populate the element as below. Sample request generated is 

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<tns:Account xmlns:tns=”http://www.maventechnologies.com&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<tns:AccountID xsi:nil=”true” />
</tns:Account>

Lazy loading not happening in case of One to One relationship sharing Primary Key

There was issue developer was facing, where the relationship between 2 entity was defined to be lazy loaded. Base on the hibernate show sql logs, it was observed that the entity was being eager loaded.

It fired my curiosity and i got involved. To explain the exact scenario i use example from “Hibernate make easy by Cameron Wallace McKenzie”.

The relationship between the entity was one to one mapping. It was parent child relationship. So the parent entity was Exam, defined something like this –>

@Entity
@Table(name = “exam”)
public class Exam {

……….

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@PrimaryKeyJoinColumn
public ExamDetail getDetail() {
return detail;

……….

}

There was exam details child entity defined as –>

@Entity
@Table(name = “exam_detail”)
public class ExamDetail {

……….

@OneToOne(mappedBy = “detail”, cascade = CascadeType.ALL)
public Exam getExam() {
return exam;
}

……….

}

Now in such case when the exam object is loaded by the hibernate, along with it exam detail object also get loaded.

Session session = HibernateUtil.beginTransaction();
Exam exam = (Exam) session.get(Exam.class, 1);

For the above following is output in the log.

Hibernate: select exam0_.id as id0_0_, exam0_.shortName as shortName0_0_ from exam exam0_ where exam0_.id=?
Hibernate: select examdetail0_.id as id1_1_, examdetail0_.fullName as fullName1_1_, examdetail0_.numberOfQuestions as numberOf3_1_1_, examdetail0_.passingPercentage as passingP4_1_1_, exam1_.id as id0_0_, exam1_.shortName as shortName0_0_ from exam_detail examdetail0_ left outer join exam exam1_ on examdetail0_.id=exam1_.id where examdetail0_.id=?

This may sound wrong since we have define exam detail to be lazy loaded. But once we understand how hibernate operates, this become obvious and clear. Hibernate typically does lazy loading using proxy objects. So it creates proxy object and loads the proxy with actual object when we first reference the attribute or variable or property of that object.  Thats lazy loading, loading when we actual use the object.

In above parent children relationship, the parent entity can exist without child entity which means, the exam can exist without exam details records. So when hibernate loads exam entity it does not know whether to populate the exam detail entity within exam, with proxy or null. Hibernate can only make that decision once it loads exam details.

Solutions 1

One work around would be to define the relationship as mandatory. Which means that each exam records should have exam details record. This makes things straight forward for hibernate, it always loads the exam details entity within exam as proxy. because exam details can never be null.

We can change the relationship between exam and exam details to be mandatory by changing the notation as follows

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY, optional=false)
@PrimaryKeyJoinColumn
public ExamDetail getDetail() {

The Code –>

Session session = HibernateUtil.beginTransaction();
Exam exam = (Exam) session.get(Exam.class, 1);
System.out.println(exam.getShortName());
exam.getDetail().getFullName();
session.getTransaction().commit();

Gives following out –>

Hibernate: select exam0_.id as id0_0_, exam0_.shortName as shortName0_0_ from exam exam0_ where exam0_.id=?
SCJA
Hibernate: select examdetail0_.id as id1_1_, examdetail0_.fullName as fullName1_1_, examdetail0_.numberOfQuestions as numberOf3_1_1_, examdetail0_.passingPercentage as passingP4_1_1_, exam1_.id as id0_0_, exam1_.shortName as shortName0_0_ from exam_detail examdetail0_ left outer join exam exam1_ on examdetail0_.id=exam1_.id where examdetail0_.id=?

Solution 2

Other solution is not sharing the Primary between the parent and child entities and define a separate column in the exam table to link the parent and child table. In this case the exam table looks something like this.

id int(11)
shortName varchar(255)
detail_id int(11)

In this case hibernate can look at detail_id column to figure out, if the exam detail entity within exam entity, should be populated with null or proxy. 

If detail_id is null then exam detail entity within exam object will be null. Conversely if detail_id is not null then exam detail entity within exam object will be proxy.

The annotation on the java object would look like

@OneToOne(cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@JoinColumn(name = “detail_id”)
public ExamDetail getDetail() {

The code –>

Session session = HibernateUtil.beginTransaction();
Exam exam = (Exam) session.get(Exam.class, 1);
System.out.println(exam.getShortName());
exam.getDetail().getFullName();
session.getTransaction().commit();

Give following output –>

Hibernate: select exam0_.id as id0_0_, exam0_.detail_id as detail3_0_0_, exam0_.shortName as shortName0_0_ from exam exam0_ where exam0_.id=?
SCJA
Hibernate: select examdetail0_.id as id1_1_, examdetail0_.fullName as fullName1_1_, examdetail0_.numberOfQuestions as numberOf3_1_1_, examdetail0_.passingPercentage as passingP4_1_1_, exam1_.id as id0_0_, exam1_.detail_id as detail3_0_0_, exam1_.shortName as shortName0_0_ from exam_detail examdetail0_ left outer join exam exam1_ on examdetail0_.id=exam1_.detail_id where examdetail0_.id=?
Hibernate: select exam0_.id as id0_0_, exam0_.detail_id as detail3_0_0_, exam0_.shortName as shortName0_0_ from exam exam0_ where exam0_.detail_id=?

Based on the context solution 1 or 2 may work for you. In some case neither solutions will work, and you will have live with eager loading, but aleast you know why it is eager loading 🙂

Soap UI Mocking not working when using default namespace.

The Soap UI mocking does not work in case of default namespace.

The below is the mock response. Its using implicit soap ui response object – ${#MockResponse#Request#//v1:testRequest/v1:Header/v1:ChainID/text()}

So its echoing back whatever it received in request. 

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:v1=”http://www.mycompany.com/testService/v1&#8243; >
<soapenv:Header/>
<soapenv:Body>
<v1:testResponse>
<v1:Header>
<v1:ChainID>${#MockResponse#Request#//v1:testRequest/v1:Header/v1:ChainID/text()}</v1:ChainID>
<v1:StoreID>${storeId}</v1:StoreID>
<v1:TxType>?</v1:TxType>
</v1:Header>
</v1:testResponse>
</soapenv:Body>
</soapenv:Envelope>

This work (i see the chain id being echoed back)  if my request looks like this. This request is using namespace prefix.

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:v1=”http://www.mycompany.com/testService/v1&#8243;>
<soapenv:Header/>
<soapenv:Body>
<v1:testRequest>
<v1:Header>
<v1:ChainID>123</v1:ChainID>
<v1:StoreID>123</v1:StoreID>
<v1:TxType>TWO</v1:TxType>
</v1:Header>
</v1:testRequest>
</soapenv:Body>
</soapenv:Envelope>

It does not work if request (i do not see the chain id being echoed back)  looks something like this. This is using default namespace instead of prefix. This is valid request as well.

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/”&gt;
<soapenv:Header/>
<soapenv:Body>
<testRequest xmlns=”http://www.mycompany.com/testService/v1&#8243;>
<Header>
<ChainID>123</ChainID>
<StoreID>123</StoreID>
<TxType>TWO</TxType>
</Header>
</testRequest>
</soapenv:Body>
</soapenv:Envelope>

In my case this request is generate by third party product, there is not way to force it to use the namespace prefix.

So i had to find work around in soapui itself.

I am using groovy script in mock response. After this change it works for both request.

<soapenv:Envelope xmlns:soapenv=”http://schemas.xmlsoap.org/soap/envelope/&#8221; xmlns:v1=”http://www.mycompany.com/testService/v1″&gt;
<soapenv:Header/>
<soapenv:Body>
<v1:testResponse>
<v1:Header>
<!–
<v1:ChainID>${#MockResponse#Request#//v1:testRequest/v1:Header/v1:ChainID/text()}</v1:ChainID>
–>
<v1:ChainID>${chainId}</v1:ChainID>
<v1:StoreID>${storeId}</v1:StoreID>
<v1:TxType>?</v1:TxType>
</v1:Header>
</v1:testResponse>
</soapenv:Body>
</soapenv:Envelope>

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def holder = groovyUtils.getXmlHolder( mockRequest.getRequestContent() )
holder.declareNamespace( ‘v1’, ‘http://www.mycompany.com/testService/v1&#8217;)
context.setProperty(“chainId”, holder.getNodeValue( “//v1:testRequest/v1:Header/v1:ChainID/text()”));
context.setProperty(“storeId”, holder.getNodeValue( “//v1:testRequest/v1:Header/v1:StoreID/text()”));

untitled