Skip to main content

Posts

Showing posts from 2012

Disable hibernate Optimistic Lock for specific scenario

Optimistic Lock gives more control on concurrent modification on data. That can be achieved very easily in hibernate using @Version column. But in some cases we may have to avoid concurrency check, example background process. To disable Optimistic lock set OptimisticLockMode as NONE for entity class while hibernate is initiating. We can't remove version column that needs Database changes and ORM changes. In this way we can disable Optimistic Lock by code. implement "org.hibernate.event.Initializable" interface override the below method.     @Override     public void initialize(Configuration cfg) {         Iterator<?> persistentClassIterator = cfg.getClassMappings();         while (persistentClassIterator.hasNext()) {             PersistentClass persistentClass = PersistentClass.class.cast(persistentClassIterator.next());                     Iterator propertyIterator = persistentClass.getDeclaredPropertyIterator();                    while (proper

HIbernate @version for existing data, cannot be null

Hibernate have option to check concurrent modification of a same data. Which will be easy when we add a version column in the table and hibernate entity. If existing row has version as NULL then hibernate will throw the NullPointerException while updating the existing data. To avoid this exception   @version column  should be NOT NULL and should have a default value. if it number 1.

remove(unset) property in a property file using ANT PropertyFile task

Using PropertyFile task we can edit the property file during ANT build. Edit property value are very easy and can find here details. http://ant.apache.org/manual/Tasks/propertyfile.html but delete a property or comment a property is the tricky one. If we are using latest ant version (1.8.1 or later), we can delete a property like below. <propertyfile file="my.properties" comment="My properties">   < entry  key="propertykey" operation="del"/> < /propertyfile> but the older version that is before 1.8.1 don't have operation called " del ", if we run the command in lower version ant, we will get a error says undefined operation "del".      there is workaround instead deleting a property we can comment that property using ant's replace command. < replace file="sample.properties">                      < replacefilter token="propertykey" value="#propert

Online HTML & CSS editor, MOZILLA THIMBLE

Very nice HTML & css editor for learners. https://thimble.webmaker.org/en-US/

Hibernate primaryKey (ID) auto generate using Database sequence

    @Id     @GeneratedValue(strategy = GenerationType.AUTO, generator ="CUSTOMER_SEQ_G")     @SequenceGenerator( name = "CUSTOMER_SEQ_G", sequenceName = "CUSTOMER_SEQ")     @Column(name = "CUSTOMER_ID")     private Long customerId; generator in @GeneratedValue should same as name in @SequenceGenerator sequenceName is Database Sequence object.

Deploy ear in exploded in Jboss using maven plugin

There is a option to deploy EAR/WAR as exploded folder in JBoss using jboss-maven-plugin. setting unpack "true" will do everything.... <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jboss-maven-plugin</artifactId> <version>1.5.0</version> <configuration> <jbossHome>C:/jboss-6.1.0.Final</jbossHome> <serverName>all</serverName> <fileName>target/MyEar.ear</fileName> <unpack>true</unpack> </configuration> <executions> <execution> <id>harddelpoy</id> <phase>install</phase> <goals> <goal>harddeploy</goal> </goals> </execution> </executions> </plugin>

WARN [org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker] Unexpected error in pingDatabase: java.lang.IllegalArgumentException: object is not an instance of declaring class {SOLVED}

When we use Oracle driver in application which is deployed JBossAS, We have to place the Oracle driver jar in server/lib directory. If we keep the jar in our Ear or War file also will get this warning message. 15:56:40,098 WARN  [org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker] Unexpected error in pingDatabase: java.lang.IllegalArgumentException: object is not an instance of declaring class To solve this, just remove the Oracle lib from your Ear or War.

start JBoss 6 server in different port

<<Jboss-HOME>>/bin run.bat -c <<servername> -Djboss.service.binding.set=ports-01   <<servername>>  - could be server directory name (all or minimal). default value is "default"  -Djboss.service.binding.set=ports-01 - this option will increase all port numbers by 100. Example: HA-JNDI port default is 1099 and after using option 1199 There are 4 options available in Jboss6, to view those login into AdminConsole ( http://localhost:8080/admin-console/ ) Service Binding Manager Service Binding Sets ports-01 ports-02 ports-03 ports-default ports-default - 8080 and others ports-01 - +100 ports-02 -  +200 ports-03 -  +300

contains() and indexOf() in Java will return true for Empty String("")

In Java we will use String.contains(String2) to check a String contains another String. This Contains() method will return true if we String2 is empty that means "". Also String.indexOf(String2) and String2 is empty "", this also will return 0. If we check return index is greater than -1 or not equal to -1, this will return true. So to avoid this, we have to check the given string is not empty.                 String s = ""; String b = "test"; System.out.println("b contains s?:"+b.contains(s)); //true System.out.println("b indexOf s?:"+b.indexOf(s)); //0 System.out.println("b indexof 't'?:"+b.indexOf("t")); //0 System.out.println("s contains b?:"+s.contains(b)); //false Do not check index or contains for empty String at any time. 

Skip build while launching remote debug in eclipse

While debugging remote server like jboss, tomcat etc.. eclipse will try to build whole workspace which already build, that will take time finish of. To skip build while launching remote debug, There is option eclipse  * goto preferences --> Run/Debug --> Launching * uncheck Build (If required ) before launching

Maven skip test and skip test compile

In maven there are two ways to skip test. mvn install - DskipTests this will skip the tests but classes will compile. mvn install - Dmaven . test . skip = true this will skip the test and also will skip compilation of test classes Whenever we need to skip compilation of test classes we can use second one.

debug maven test in eclipse using surefire plugin

Some time debugging test cases in eclipse will give "206" error, that classpath string length is exceeding the character limit, due to more libraries in classapth. In this case we can't debug our test cases in eclipse. To solve this problem, maven-surefire-plugin has already given below solution: http://maven.apache.org/plugins/maven-surefire-plugin/examples/debugging.html Debugging Tests Sometimes you need to debug the tests exactly as Maven ran them. Here's how! Forked Tests By default, Maven runs your tests in a separate ("forked") process. You can use the   maven.surefire.debug   property to debug your forked tests remotely, like this: mvn - Dmaven . surefire . debug test The tests will automatically pause and await a remote debugger on port 5005. You can then attach to the running tests using Eclipse. You can setup a "Remote Java Application" launch configuration via the menu command "Run" > "Open Debug Dialog..

org.apache.maven.reactor.MavenExecutionException: Cannot find parent:

This exception will occur maven project has like below structure and parent POM is not installed in repository that means first time building. |-- my-module | `-- pom.xml `-- parent      `-- pom.xml   To solve this error need add relative path in other POMs in parent section.         <parent> <groupId>parent.group</groupId> <artifactId>parent.artifact</artifactId> <version>1.0-SNAPSHOT</version> <relativePath>../parent/pom.xml</relativePath> </parent>

Caused by: java.lang.VerifyError:

java.lang.VerifyError occurs when overriding a final method from the parent class. In my situation implementation class is overriding a method from another library, now that library has changed and they made that method as final.  When  VerifyError comes, check the version of library or class has changed or not?

javac: invalid target release: 1.6

While running Maven build the below error occur, when there are more than one JDK installed.  Failure executing javac, but could not parse the error: javac: invalid target release: 1.6 Usage: javac <options> <source files> Example JDK 5 and JDK 6 has installed JDK 5 is on that JAVA_HOME. this error will occur. To resolve this set JAVA_HOME as JDK 6 in enviroment variables setting dialog or set JAVA_HOME in command prompt before running maven build. Second one temporary solution once the command window closed, JAVA_HOME reset back to default in system settings.

XML validation using XMLbeans

XML file validation is somewhat painful job. If we have XML Schema(XSD) that can be done very easily using Apache XmlBean. Also traversing or parsing XML will be joyful with XmlBean. First we have to generate Java class for XSD using XmlBean's scomp command, this can be done in command line. there are some options to generate source only or only Jar file. After having Xmlbean objects for XSD, parsing and validating can be done like below: void validate(String fileName) {          XmlDocument xmlObject = XmlDocument.Factory.parse(new File(fileName)); // parse the XML file         ArrayList validationErrors = new ArrayList ();           XmlOptions validationOptions = new XmlOptions();          validationOptions.setErrorListener(validationErrors);          boolean isValid = xmlObject.validate(validationOptions);  // to display error we should pass options.         if (!isValid) {             for (XmlValidationError es : validationErrors) {    

Blob type in Hibernate

In some scenarios we would have store large data into database like image or file. For large date Blob type is best one. If you use Hibernate as your ORM framework, there is a small trick to store Blob object into Database. @Column(name="ImageData") @Lob() byte[] imageData; Just add @Lob() annotation for variable or table column in Entity class, rest handling the Blob type will be taken care by Hibernate.