View RSS Feed

All Blog Entries

  1. one-to-many, sql

    by , 06-02-2011 at 07:20 PM (My Java Tips)
    Let me presnet a simple <one-to-many> association from Parent to Child.

    XML Code:
    <set name="children">
    <key column="parent_id"/>
    <one-to-many class="Child"/>
    </set>
    Assume that we wish to execute the following code:

    Java Code:
    Parent p = .....;
    Child c = new Child();
    p.getChildren().add(c);
    session.save(c);
    session.flush();
    ...
    Tags: one-to-many Add / Edit Tags
    Categories
    Hibernate
  2. Svn revert

    by , 06-02-2011 at 07:16 PM (My Java Tips)
    You must have used SVN while working in a team on a Java project. In this post, Ill write about 'revert' command.

    Let me present a scenario. You made some changes in a class and now you realize that the changes are not required/not correct. You wish to discard your changes. You have to use svn revert to discard your changes.

    Java Code:
    $ svn revert foo.java
    Reverted foo.java
    Following switches are available:

    Java Code:
    --targets
    ...
    Tags: revert, svn Add / Edit Tags
    Categories
    Subversion
  3. No Persistence provider for EntityManager

    by , 06-02-2011 at 07:15 PM (My Java Tips)
    You might have encountered the following exception while working with hibernate:

    Java Code:
    [java] Exception in thread "main" javax.persistence.PersistenceException: 
    No Persistence provider for EntityManager named  ****
    Make sure you have all the required jars in the class path.

    Java Code:
          \antlr-2.7.6.jar
          \asm.jar
          \asm-attrs.jar
          \c3p0-0.9.0.jar
          \cglib-2.1.3.jar
    ...
  4. Static/Non static methods - Example

    by , 06-02-2011 at 07:12 PM (My Java Tips)
    I will present an example to show the difference between static and non-static methods.

    Go through the example below:

    Java Code:
    public abstract class AbstractExample {
    
       public static void foo() {
          System.out.println("Static foo called.");
       }
    
       public void bar() {
          System.out.println("Non-static bar called.");
       }
    }
    
    public class AbstractCaller extends AbstractExample {
    ...
    Categories
    Java SE
  5. Factory Methods

    by , 06-02-2011 at 07:11 PM (My Java Tips)
    This post lists some facts about factory methods.

    Factory methods are static methods that return an instance of the native class. Some examples from JDK include :

    • LogManager.getLogManager
    • Pattern.compile
    • Collections.unmodifiableCollection
    • Collections.synchronizeCollection
    • Calendar.getInstance


    Factory methods do not have names like constructors and do not need to create a new object upon each invocation. Objects can be cached and reused, if ...
  6. Memory Leaks

    by , 05-25-2011 at 10:04 AM (My Java Tips)
    You are aware of the fact that garbage collection collects and removes all the unwanted objects from memory without used intervention. But sometimes, you run into memory leaks problems that demands investigation. I will shed some light on this issue in the next few posts.

    The Java GC process is a low-priority thread and it constantly searches memory for unreachable objects that are not reachable by any live thread. Different JVMs use different algorithms to determine how to collect ...
    Categories
    Performance
  7. Subclipse - Checkout

    by , 05-25-2011 at 10:02 AM (My Java Tips)
    Subclipse is a popular SVN plugin for Eclipse. This post is about how to do checkout using Subclipse.

    You will check out code from the trunk or branch of a project. First configure your repositories. Open the SVN Repository Perspective and then open the contents of the repository where your project is located. Locate the trunk/ or branch/ directory in the project and right-click the trunk/ directory or branch/ directory, and choose Checkout from the context-menu. You will be see a ...
    Categories
    Subversion
  8. Committing code in Subversion

    by , 05-25-2011 at 10:00 AM (My Java Tips)
    Committing means persisting your changes. You may commit one file or multiple files at once.

    Subclipse makes committing easy and simple. When a file is modified, Subclipse displays an icon next to the file name (asterisk) which indicates that your local copy is different from what was in the repository
    when the file was checked out. This helps you know what to commit. Remember, if you create a new file, it will have a different icon(a small question-mark). The "changed" ...
    Tags: subclipse Add / Edit Tags
    Categories
    Subversion
  9. Using interfaces in APIs

    by , 05-25-2011 at 09:59 AM (My Java Tips)
    When you are designing your API, you might want to use interface since this seems a common practice. There exist a style in Java programming where everything is expressed in terms of Java interfaces instead of classes.

    Using interfaces provides benefits but its not a good idea for an entire API to be expressed in terms of them.

    Use interface as a type if you have a good reason for it to be. Following are some important facts to be considered:

    Interfaces ...
    Tags: api, interface Add / Edit Tags
    Categories
    Java SE
  10. Writing deployment descriptor

    by , 05-25-2011 at 09:57 AM (My Java Tips)
    If you writing J2EE applications, you need to deploy your application to J2EE container like JBOSS. I will shed some light on how to write simple deployment descriptors for your web applications.

    I am deploying my JSP application on JBOSS. For JBOSS, we need to put our application in WAR format in deploy folder. So deployment descriptor will have following 2 tasks:

    1. Archieve the application into WAR
    2. Copy that WAR to deploy folder of JBOSS


    Assuming ...
    Tags: j2ee, war Add / Edit Tags
    Categories
    JBoss , Java EE
  11. Wrapping JDBC Objects

    by , 05-25-2011 at 09:55 AM (My Java Tips)
    In Glassfish connection pool has a property called "Wrap JDBC Objects" which can have value true or false. You may define it in domain.xml file.

    This property is defined as: "When set to true, application will get wrapped jdbc objects for Statement, PreparedStatement, CallableStatement, ResultSet, DatabaseMetaData".

    If you follow JDBC specifications, you will be see that obtaining connection using DataSource.getConnection() or other objects like ...
  12. hibernate.connection.rele ase_mode

    by , 05-25-2011 at 09:49 AM (My Java Tips)
    Hibernate JDBC/Connection's property hibernate.connection.release_mode is used to specify when Hibernate should release JDBC connections.

    A JDBC connection is held until the session is explicitly closed or disconnected by default. For a JTA datasource, one should use after_statement to aggressively release connections after every JDBC call. And for a non-JTA connection, it often makes sense to release the connection at the end of each transaction. after_transaction.auto will
    ...
    Categories
    Hibernate
  13. Hibernate Dialects

    by , 05-25-2011 at 09:46 AM (My Java Tips)
    To set SQL dialect, one has to set the hibernate.dialect property to correct org.hibernate.dialect.Dialect subclass for the database.

    Specify a dialect and then Hibernate will use defaults for some of the properties and this will save the effort of specifying them manually.

    Following dialects are supported.

    • DB2 org.hibernate.dialect.DB2Dialect
    • DB2 AS/400 org.hibernate.dialect.DB2400Dialect
    • DB2 OS390 org.hibernate.dialect.DB2390Dialect
    • PostgreSQL
    ...
  14. JSP Tags

    by , 05-25-2011 at 09:32 AM (My Java Tips)
    JSP tags are like HTML tags and they do not use <%. Instead they use < character.

    JSP tags can have a "start tag", a "tag body" and an "end tag". As you may have guessed, the start and end tag both use the tag name, enclosed in < and > characters. Just has HTNL tags, the ending JSP tag starts with a / character after the < character. The tag names have an embedded colon character : in them, the part before the colon describes the type ...
    Tags: jsp tags Add / Edit Tags
    Categories
    JSP
  15. XSLT Conditions

    by , 05-25-2011 at 09:30 AM (My Java Tips)
    Consider the scenario: You have xml data and you need to filter some data out. You may do that in Java but its open a good idea to keep the logic separate in XSLT filters.

    if-then-else condition is done the following way in XSLT:

    Java Code:
    <xsl:choose>
      <xsl:when test="...">
        ...
      </xsl:when>
      <xsl:when test="...">
        ...
      </xsl:when>
      <xsl:otherwise>
    ...
    Tags: xslt Add / Edit Tags
    Categories
    XML
  16. Javap

    by , 05-24-2011 at 01:58 PM (My Java Tips)
    Javap is shiped with JDK and it really very useful when you want to see what your compiler is doing to your code. It generates source code from a compiled class file.

    Javap is a utility provided in Java Development Kit (JDK) that can be used to improve source code. this is done by generating source code from compiled code and seeing how our code was treated by the Compiler. Later code improvements can be made.

    A very interesting example can be to see how source code ...
  17. ArrayStoreException

    by , 05-24-2011 at 01:48 PM (My Java Tips)
    java.lang.ArrayStoreException is a runtimeException and is thrown when we try to store wrong type of object into an array of objects.

    Constructors:

    Java Code:
    ArrayStoreException()
    ArrayStoreException(String s)
    For example: Storing an Integer object in a Strign array will cause this exception.

    Java Code:
    Object x[] = new String[3];
    x[0] = new Integer(0);
    Since its a runtime exception, compiler won't ...
    Categories
    Java SE
  18. Finalize

    by , 05-24-2011 at 01:46 PM (My Java Tips)
    All Java classes inherits the finalize() method from java.lang.Object and this method is called by the garbage collector when it determines no more references to the object exist.

    The finalize method performs no actions but it may be overridden by any class. It is overridden to clean-up non-Java resources ie closing a file, closing a connection etc. In overridding finalize() it is a good programming practice to use a try-catch-finally statement and to always call super.finalize(). ...
    Tags: finalize Add / Edit Tags
    Categories
    Java SE
  19. Writing a file from JSP

    by , 05-24-2011 at 01:45 PM (My Java Tips)
    You may want to write a file from JSP page. For example, user fills a form and you want to persist the details on a text file on server. Now you need to know how to write text file from JSP.

    Remember, always give the path from root. In the given snippet, nameOfTextFile contains the file path. We will use FileOutputStream to write to the file.

    Java Code:
    <%@ page import="java.io.*"  %>
    <%
    String str = "form data goes here";
    ...
    Categories
    JSP
  20. Reading files from JSP

    by , 05-24-2011 at 01:44 PM (My Java Tips)
    Working in Servlets/JSP, you might have encountered The javax.servlet.ServletContext API. It provides methods to access resources in general.

    • getResource(String resource): will returns a URL for the resource in the web application.
    • getResourceAsStream(String resource): will returns an InpuStream for the resource in the web application
    • getResourcePaths(String path): will a directory-like listing of all the paths to resources within the web application.


    An example ...
    Categories
    JSP
  21. Using Java Beans in JSP

    by , 05-24-2011 at 01:42 PM (My Java Tips)
    JavaBeans are classes written in the Java programming language conforming to a particular convention.

    Consider the scenario: you are writing a JSP and you wish to write pure Java code in it. You may use scriptlets within JSP.

    Java Code:
    <%
    // Java code goes here
    %>
    Using scriptlets means that your JSP will contains business logic as well which is not a good design approach. Ideally you would like to keep business logic separately ...
    Categories
    JSP
  22. Fetching parameters from web.xml

    by , 05-24-2011 at 01:30 PM (My Java Tips)
    The file named web.xml is part of your web applications. You may use it for different purpose. Sometimes you wish to define some parameters for your web application in it. Obviously, you need to use this in your application. I will briefly tell you how to do this.

    At first, let me give you an example. We have a web application that needs to send its id and license code to some authentication server before doing some business operation. The id and license code are static and you may ...

    Updated 05-24-2011 at 01:34 PM by Java Tip

    Categories
    JSP , Servlet , XML
  23. Stack traces

    by , 05-24-2011 at 01:28 PM (My Java Tips)
    You must have seen stack traces in console window of your Eclipse. Good this is that Java stack traces in the console appear with hyperlinks. This means you can click these to go to the referred place in the code. This helps in problem identification.

    Here is how is works: place the mouse over a line in a stack trace and the pointer changes to the hand. Pres the mouse button and associated Java source file will be opened and the cursor will be placed at the corresponding line. Pressing ...
    Tags: stack Add / Edit Tags
    Categories
    Eclipse
  24. Running code with compile errors

    by , 05-24-2011 at 01:26 PM (My Java Tips)
    While coding in Java using Eclipse, you might have experienced situations where your Java class has some compile errors but stilly you want to run it. The reason might be that the problematic area might be invoked in your execution. What ever is the reason, Ill briefly explain how to execute code with compile errors.

    In Eclipse, you can run and debug code that did not compile cleanly. You cannot do this using command prompt. If line of code with compile error is executed, following ...
    Tags: debug, debugger Add / Edit Tags
    Categories
    Eclipse
  25. Debugger: Code Assist

    by , 05-24-2011 at 01:25 PM (My Java Tips)
    You might have used code assistant while coding in Eclipse. Its really useful and saves time for trivial stuff. Most of us think that code assistant is only available in code writing context. That’s not right.

    Code assistant is available in debug mode at following:

    • When entering a breakpoint condition,
    • In the Details pane of the Variables & Expressions view,
    • When entering a Detail Formatter code snippet,
    • When entering a Logical Structure code snippet,
    • When
    ...
    Categories
    Eclipse
  26. Scrapbook in Eclipse

    by , 05-24-2011 at 01:23 PM (My Java Tips)
    Consider the following scenario: you are working on a project with client specific APIs .You need to test some API methods before you put them in your real code. For that, you might consider a separate test workspace or defining a new test class in your running workspace. What about scrapbook page? Ever heard of it?

    Eclipse provide scrapbook pages to test write test code for experimental purposes. It is easier to use a Java scrapbook page than create a new class. A scrapbook page ...
    Tags: scrapbook Add / Edit Tags
    Categories
    Eclipse
  27. Setting watches in Eclipse

    by , 05-21-2011 at 10:55 PM (My Java Tips)
    Watch point is a special type of break point that suspends execution when specified filed is accessed or modified.

    Setting a watchpoint is simple. First select a field in the Outline view and choose Toggle Watchpoint from its context menu.

    Name:  watches.jpg
Views: 573
Size:  22.9 KB

    Now you have to configure this watchpoint. This is done by selecting the watchpoint in the view and choose Properties from its context menu. Don’t forget to configure the important properties ...
    Categories
    Eclipse
  28. Motivation for unit tests

    by , 05-21-2011 at 10:53 PM (My Java Tips)
    Unit tests are essential part of software development especially. In complex and large softwares, introducing new changes in very tricky as it can break some other part. If you have clearly defined test cases, then you can check the working of newly introduced change and make sure it works as expected.

    Unit tests are small snippets of code that are written to check a particular method or class. With unit tests in place, when a change is made to the code we can simply run all the tests ...
    Tags: unit test Add / Edit Tags
    Categories
    JUnit
  29. Importing junit.jar

    by , 05-21-2011 at 10:52 PM (My Java Tips)
    Before you start writing unit tests, you must import junit.jar, so we have access to the testing framework. Follow these steps to import the required jar:

    • Right-click on the project name, and choose Properties.
    • In the tree on the left, select Java Build Path.
    • Next, choose Add External JARs… and browse to find junit.jar.


    It will be located in "<eclipsedir>\plugins\org.junit_<version number>\junit.jar".

    Once you successfully ...
    Tags: jar Add / Edit Tags
    Categories
    Eclipse , JUnit
  30. Wrapping Strings

    by , 05-21-2011 at 10:50 PM (My Java Tips)
    Its always useful to wrap strings if they are too long. This provides better readability. Eclipse has support for this.

    String literals can wrapped when you edit them. Place your cursor at the point of literal where you want to wrap and press Enter. For example:

    Java Code:
    String message= "Java is fun to learn and it sure is a popular programming language.";
    I kept the cursor at ‘sure’ and pressed enter key. The String was changed to: ...
    Tags: string Add / Edit Tags
    Categories
    Eclipse
Page 47 of 48 FirstFirst ... 3745464748 LastLast