Skip to main content

Posts

Showing posts from March, 2011

JDBC - Getting tables from database

You can retrieve all tables from database using JDBC. DatabaseMetaData object contains various method to get the meta information from any database. This object can be accessed by invoking getMetaData() on the Connection object. DatabaseMetaData dbmd=con.getMetaData();   After getting the DatabaseMetaData, you can use it to retrieve the tables from any database like following statement, ResultSet rs=dbmd.getTables(null,null,null,new String[]{"table"}); Here, you will get the ResultSet Object containing various other information like Schema, catalog, table name, etc. Table name available in third column of this ResultSet so we can get it as rs.getString(3); To get all tables from a database using Connection, we need to traverse entire ResultSet object. We have to invoke rs.next() till it returns false and on each invocation of rs.next(), get the data from third column to get the name of the table. Here is a method that returns the array of table names from the datab

Hibernate- Defining table and columns for mapping

If you want to define customized name for the table and column, you have to use @Table and @Column annotations. @Table allows you to define the table, catalog, and schema names for your entity mapping. If no @Table is defined the default values are used: the unqualified class name of the entity. You can also define unique constraints to the table using the @UniqueConstraint annotation in conjunction with @Table @Table(name="profile", uniqueConstraints = {@UniqueConstraint(columnNames={"country", "state"})} ) @Column annotation allows you to specify the column name to be pointed out in table. Following are attribute of the @Column annotation name="columnName"; boolean unique() default false; boolean nullable() default true; boolean insertable() default true; boolean updatable() default true; String columnDefinition() default ""; String table() default ""; int length() default 255; int precision() default

SVN problem while committing or updating the directory

You may see following error when you are committing or updating directory, Update/Commit Working copy 'E:\work\myproject locked Please execute the 'Cleanup' command. According to the above error message, you should execute Cleanup command from svn-client like TortoiseSVN. But when you are performing cleanup, you may get another problem as shown below. Cleanup failed to process the following paths: -'E:\ work \myproject ''E:\ work \myproject\build' is not a working copy directory According to above error message, you should follow the following steps : delete the build folder update the folder myproject by executing the svn-command update then commit the changes by executing the commit svn-command If this problem is due to conflicting changes between local and server contents and you have resolve the problem manually, use resolved svn-command for confirmation. There may be other issues and solution for svn, please let me know if you hav

ERROR org.hibernate.LazyInitializationException - could not initialize proxy - no Session

Following code was creating problem while I trying to use the hibernate to fetch data from database. After some debugging, I found the solution for it. Let see it. Code fragment creating problem System .out. println ( "read action" ) ; SessionFactory sessionFactory  =   new   Configuration () . configure () . buildSessionFactory () ; Session session  =  sessionFactory. getCurrentSession () ; session. beginTransaction () ; user = ( User )  session. load ( pack.User. class , user. getId ()) ; session. getTransaction () . commit () ; System .out. println ( user ) ; Exception 1038 [http-8080-2] ERROR org.hibernate.LazyInitializationException - could not initialize proxy - no Session org.hibernate.LazyInitializationException: could not initialize proxy - no Session at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:132) at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:174) at org.hiberna

Hibernate - Using annotations based configuration

Hibrnate provides support for the annotations based mapping for the ORM(object relational mapping). You can use JPA annotations in hibernate to map the object to table. So , you don't require the *.hbm.xml file for the each entity to mapping in database. You can mix annotated persistent classes and classic hbm.cfg.xml declarations with the same SessionFactory. You can however not declare a class several times (whether annotated or through hbm.xml). You cannot mix configuration strategies (hbm vs annotations) in an entity hierarchy either. Following annotaions are being used in the example; @Entity declares the class as an entity (i.e. a persistent POJO class). @Id declares the identifier property of this entity. Other properties will be mapped by hibernate using convensions like column name, column type, table name etc. Entity class package  domain; import  java.util. Date ; import  javax.persistence. Entity ; import  javax.persistence.Id; @ Entity public