Thursday, March 19, 2009

Parent Child Relationship - Hibernate

Parent Child Relationship - Hibernate

Inserting values in both parent and child table in Hibernate in a single session.save() command. You need not insert value in the parent table first and then get the ID and set it in the child and then insert it. You can do both at the same time. Below are the sample code snippets of the files used.

hbm.xml file:

<class name="com.sample.pojos.UserInfoBean" table="USER_INFO" lazy="false">

... other properties configured
<property name="firstName" type="java.lang.String" column="FIRST_NAME" />
<property name="lastName" type="java.lang.String" column="LAST_NAME" />
<bag cascade="all-delete-orphan" inverse="true" lazy="true" name="userDetailsBean" table="USER_DETAILS">
<key column="USER_INFO_ID"/>
<one-to-many class="com.sample.pojos.UserDetailsBean"/>
</bag>
</class>

<class name="com.sample.pojos.UserDetailsBean" table="USER_DETAILS" lazy="false">

... other properties configured
<property name="address" type="java.lang.String" column="ADDRESS" />
<property name="city" type="java.lang.String" column="CITY" />
<many-to-one cascade="none" class="com.sample.pojos.UserInfoBean" column="USER_INFO_ID" insert="true" name="userInfoBean" outer-join="false" update="true"/>
</class>

UserInfoBean.java

private List userDetailsBean;

... other properties

/**
* @return Returns the userDetailsBean.
*/
public List getUserDetailsBean() {
return userDetailsBean;
}

/**
* @param userDetailsBean The userDetailsBean to set.
*/
public void setUserDetailsBean(List userDetailsBean) {
this.userDetailsBean = userDetailsBean;
}

... getters and setters for other properties
[/sourcecode]

UserDetailsBean.java

[sourcecode language='java']
private UserInfoBean userInfoBean;

... other properties

/**
* @return Returns the userInfoBean.
*/
public UserInfoBean getUserInfoBean() {
return userInfoBean;
}

/**
* @param userInfoBean The userInfoBean to set.
*/
public void setUserInfoBean(UserInfoBean userInfoBean) {
this.userInfoBean = userInfoBean;
}

... getters and setters for other properties

Business class:

Transaction transaction = null;

UserInfoBean userInfoBean = new UserInfoBean();

... set the required properties

UserDetailsBean userDetailsBean = new UserDetailsBean();

... set the required properties

// Setting the parent class in the child class
userDetailsBean.setUserInfoBean(userInfoBean);

// List for storing the User Details Beans
List userDetailsList = new ArrayList();

// Adding the UserDetailsBean to the list
userDetailsList.add(userDetailsBean);

// Setting the list in the UserInfoBean
userInfoBean.setUserDetailsBean(userDetailsList);

try {
Session session = SessionManager.currentSession();
transaction = session.beginTransaction();
session.save(userInfoBean);
session.flush();
session.clear();
transaction.commit();
createUserStatus = true;
} catch (Exception e) {
transaction.rollback();
} finally {
SessionManager.closeSession();
}

Now in a single session.save() command, values gets inserted in both the parent and child table.

Hope this helps.

Monday, January 26, 2009

Cloud Computing In Plain English

Wednesday, December 31, 2008

Continuous Build Integration

In the Agile Software Development practice we would need to build and test the application frequently. For example we might need to build many times in a day. We could automate this process and have a cron job that builds and generates reports for the development team to consume.
There are tools that help us in this process of building and testing the application at frequent pre-defined intervals, such as Cruisecontrol, Hudson, Bamboo etc., Refer to this link for the feature matrix comparison of these Continuous Integration Tools.
As an example we shall look at Cruisecontrol which is a Java based, open source CI tool - released with BSD style licence.

How CruiseControl works
  • Developer checks work into version control

  • CC polls version control

  • CC triggers a build

  • CC captures logs and build artifacts

    • Examples: jar, war, javadoc, unit test report, code coverage report, code quality metrics

  • CC publishes results

    • Examples: send email, send instant message


We have a config.xml file through which we could configure the tool.
We would specify the version control information(such as CVS host and credentials), the project details, the scheduling information(such as when to build the app), where to log the builds and save the artifacts and whom to inform when the build completes etc., in this config file.
Since plugins are available for cruisecontrol there is a lot of options that we can configure in this config.xml file.

Here's a sample config.xml file:
<cruisecontrol>
  <project name="connectfour">

<listeners>
<currentbuildstatuslistener file="logs/${project.name}/status.txt"/>
</listeners>

<bootstrappers>
<svnbootstrapper localWorkingCopy="projects/${project.name}" />
</bootstrappers>

<modificationset quietperiod="30">
<svn localWorkingCopy="projects/${project.name}"/>
</modificationset>

<schedule interval="300">
<ant anthome="apache-ant-1.7.0" buildfile="projects/${project.name}/build.xml"/>
</schedule>

<log>
<merge dir="projects/${project.name}/target/test-results"/>
</log>

<publishers>
<onsuccess>
<artifactspublisher dest="artifacts/${project.name}" file="projects/${project.name}/target/${project.name}.jar"/>
</onsuccess>
</publishers>

</project>
</cruisecontrol>
There is a cruisecontrol dashboard through which we can access the history of artifacts and build results and manage those.
Here is a sample Dashboard of cruisecontrol.




Monday, November 10, 2008

CSS and JS compression

A web project has lots CSS and JS files associated with it and lots of these files does affect the page load time based on the internet speed, its size etc.

It would be a wise option to compress the CSS and JS files to make download of these on the fly much faster.
1) GZIP compression
2) Remove unwanted space, line feed characters in JS.
3) Use smaller variable names
4) Remove comments

For (1) - Most of the application servers come with option of enabling compression for HTTP and HTTPS. For example in Tomcat you can do it by setting in the server.xml
...
<connector acceptcount="100" connectiontimeout="60000" disableuploadtimeout="true"
port="9014" redirectport="8944" scheme="https" proxyport="443" compression="on"
compressablemimetype="text/html,text/xml,text/plain,text/css,text/javascript">
...
Having 2, 3 and 4 done on the code base will make it ugly and not readable. There are lots of open source tool which can help as they does all these during build time when creating the achieves.

For example one such tool would be Yahoo UI compressor - it has a easy maven plugin as well - click here for more details on usage.

Thursday, May 10, 2007

Including HTML Tags/codes in Blog Post

This post is the outcome of my previous post. In my previous post,I had to include some HTML codes as an example, it contained Scripts, CSS and some Body code. I did that in a normal way, but Blogger Engine took that as HTML codes and dsiplayed the corresponding result as a browser does. I was thinking what to do for this, I tried commenting that part of the code, but it didnt work well.Finally we got a solution, instead of "<" open tag we shoudl use "amplt;" and instead of close tag ">" we should use "ampgt;". This works out well.
NOTE: Instead of "amp" use the "&" symbol, i have given " amp "here for reperesentation.

Applying Styles for File Button

Everyone who works in the front end will be aware of the input type=”file” , the function of this button is that, it will fetch you the file browser, so that you can chose the file for your Upload. Unfortunately, you can have images for these buttons, unlike we have for other Submit kind of buttons. So what can be done for this? There are two solutions,

1.You can find out the code behind of this File button and can implement that procedure in Onclick of this image button.
2.You can have an image Button over a File Button and you can invoke the Click event of the File button from the onclick event of this image buttton. For this you have to play with the z-index property.

we will have a look at the second method, as the first one is very simple.

How to do this?
Change the z-index of the File Button, make it as negative(or a lesser value than image button). I have included the code here, check it out here

<script type=”text/javascript”>
function clickBrowse()
{
document.getElementById(’openssme’).click()
}
</script<
<style type=”text/css”<
input.hide
{

position:relative;
left:-225px;
z-index: -1;
width:0px;
border-width:0px;
}

input.red
{
position:relative;
background-color:#cc0000;
font-weight:bold;
color:#ffffff;
z-index:100;
width:75px;
height:20px;
font-size:10px;
}
</style>

<!–[if IE]>
<style type=”text/css”>
input.hide
{
position:relative;
visibility:hidden;
left:-83px;
z-index: -1;
width:0px;
border-width:0px;
}
</style>
<![endif]–>

<img src=”btn_browse.gif” class=”red” id=”pseudobutton” onClick=”clickBrowse();” />
<input type=”file” class=”hide” id=”openssme”>

We have to handle IE and FF seperately, so a code for IE is placed seperately in this.
I thank Nirmal, for helping me out in this.

Thursday, April 12, 2007

Internet Explorer Hacks and Tips

Odd Toolbar Behavior

Did you ever get your Toolbars in IE set just right only to reboot and you lose your settings? Many times when Toolbars are added or removed the Registry entry that controls these settings does not get updated or has become corrupt. You can download a small reg file to reset (to default) and correct this behavior.

  • Download: ResetBrowserToolbar .reg [right-click and select: "Save Target As"
    To use: right-click and select: Merge - Ok the prompt and reboot.

Missing Status Bar

The above reg file will correct this behavior, if your status bar disappears when opening a new IE browser window. However make sure that option is selected - right-click the top Toolbar and select: Status Bar. If you are still having problems verify the below Registry entries exist and are correct:

[HKEY_CURRENT_ USER\Software\ Microsoft\ Internet Explorer\Main]
"Show_StatusBar"="yes"
"Show_URLinStatusBar"="yes"

Opening Internet Explorer Full Size

Ever notice sometimes you right-click on a link and open a new window and it's not full screen? This usually occurs because IE remembers the last window size when closed. Many times a background pop-up will do this too, if it is the last window closed. To resize the window and regain control:

  • Close all instances of Internet Explorer except for one.
  • Right-click on a link in the page and select: "Open in New Window"
  • Close the first browser window using the [ X ] (upper right corner)
  • Resize the window manually by dragging the sides to the desired size.
    Note: Do NOT click the Maximize button, you must do it manually.
  • Hold down the Ctrl key and click the Close button (upper right)

[Exception]
In some cases the "Remember last window size" info becomes corrupted in the Registry.

[HKEY_CURRENT_ USER\Software\ Microsoft\ Internet Explorer\Main]
"Window_Placement"=

To reset the "Window_Placement" entry to default. You can download a small reg file to reset (to default) and correct this behavior.

Download: ResetWindowPlacemen t.reg [right-click and select: "Save Target As"]
To use: right-click and select: Merge - Ok the prompt and reboot.

Gain more viewing space

  • Right-click the Toolbar and select: "Unlock the Toolbars"
  • Left-click the Address Bar button, it changes to a four-way arrow
  • Drag the Address Bar into the empty space next to Help in the Toolbar
  • Next, right-click on any of the Icon Buttons, and uncheck Text Labels
  • Also you can select: Small Icons from the Customize menu.
  • Then right-click the Toolbar and select: "Lock the Toolbars"

Resize your IE Cache size

By default IE reserves 10% of your drive space for the Temporary Internet Files. This was fine several years ago but since then the hard drive capacities have increased dramatically. If you happen to have one of these large disks the 10% is truly unnecessary and wasteful.

  • Close all instances of Internet Explorer and Outlook Express
  • Control Panel | Internet Options | General tab
  • Click Settings and move the slider to 50 mb, click Ok
  • Click Delete Files and also select: "Delete all offline files", click Ok
  • Click Clear History, click Ok, Apply\Ok

Note: While viewing Newsgroup messages Outlook Express dumps a series of zero-byte files into the TIF. When Outlook Express is closed it fails to cleanup these files. To delete these zero-byte files you must select the above option "Delete all offline files".

Spell Checker for Internet Explorer

ieSpell is a free Internet Explorer browser extension that spell checks text input boxes.
Editors Note: has an option to integrate the database from other spell checkers.

Agent Ransack (freeware) File searching software
This is not a replacement to Microsoft's built-in Search Utility, it's just much faster!

Removing Unwanted IE Menu Items

  • Scan your system with Ad-Aware or SpyBot
  • Run HijackThis! and select the "08/09" items you want removed.

To manually remove from the Registry [Experienced Users]

[HKEY_CURRENT_ USER\Software\ Microsoft\ Internet Explorer\MenuExt]

  • Click open "+MenuExt" (left pane) Locate the desired Menu Extension (highlight)
    Make a note of the corresponding (htm) file (right pane)
    Right-click the desired key (Menu Extension) select: Delete
    Note: always Export before editing the Registry.
  • Close Regedit, Open Windows Explorer
    Locate and delete the "corresponding (htm) file" (if exists)

Repairing your Winsock Connection

If you have suddenly lost your Internet connection after removing a unwanted program (such as NewDotNet, and Commonname) the following steps will help restore your connection.

Fast Printing web pages without images

  • Create a one line .css file [example] "NoImages.css", copy the below line to Notepad and Save As: NoImages.css. Open Internet Options | General tab | Accessibility button, place a check in "Format documents using my style sheet", click OK.

    IMG { display: none !important;}
    Download
    : NoImages.css [right-click and select: Save Target As]

The next time you want to print the contents of a web page but don't want to waste the time and ink printing all the ads, banners, etc. just Toggle the above option on\off.


Note: there is no need to restart IE as this trick works on the fly. Also be aware that CWShredder detects any/all .css files as suspect and deletes them. CWShredder is no longer recommended ... avoid using this outdated program.