Monday, February 25, 2013

DB2 to SQL Server conversion

I have been working lately on converting native queries in Java project from DB2 to SQL.

Apart from the date conversion one of the more tricky conversion was NEXTVALUE()  which had no equivalent in sql server 2008.


So here is how I did it.

Essentially we will create a table which wil have auto generated Ids that will supply us next value.


 Create table tableName ( [id] [int] IDENTITY(1,1) NOT NULL ON [PRIMARY]); 


In order not to save ids in table created above we need to have stored procedure which will get id and roll back the transaction.


CREATE procedure spName

 as

 begin     

declare @id int     

set NOCOUNT ON     

insert into tableName default values     

set @id = scope_identity()     

delete from tableName WITH (READPAST)



select @id as id



end


Additional links


If you are using an identity column on your SQL Server tables, you can set the next insert value to whatever value you want. An example is if you wanted to start numbering your ID column at 1000 instead of 1.
It would be wise to first check what the current identify value is. We can use this command to do so:


DBCC CHECKIDENT (‘tablename’, NORESEED)
For instance, if I wanted to check the next ID value of my orders table, I could use this command:


DBCC CHECKIDENT (orders, NORESEED)
To set the value of the next ID to be 1000, I can use this command:


DBCC CHECKIDENT (orders, RESEED, 999)
Note that the next value will be whatever you reseed with + 1, so in this case I set it to 999 so that the next value will be 1000.
Another thing to note is that you may need to enclose the table name in single quotes or square brackets if you are referencing by a full path, or if your table name has spaces in it. (which it really shouldn’t)


DBCC CHECKIDENT ( ‘databasename.dbo.orders’,RESEED, 999)

Also calling stored procedure Seam project (Java) was a big task and has been accoplished in Entity class like code below:

Declaration in Model/Entity class:


import javax.persistence.NamedNativeQueries;

import javax.persistence.NamedNativeQuery;

import javax.persistence.SqlResultSetMapping;

import javax.persistence.Table;





@Entity



@SqlResultSetMapping(name="mappingName", columns=@ColumnResult(name="id")) 

@NamedNativeQueries({

          @NamedNativeQuery(name = NativeQueries.spName, 

                          query = NativeQueries.spName, 

                          hints={ @QueryHint(name = "org.hibernate.callable",value = "true"), @QueryHint(name = "org.hibernate.readOnly",value = "true")}, 

                          resultSetMapping = "mappingName")

          })


Calling a Stored Procedure:



  if(entityManager == null) {

            entityManager = (EntityManager)Component.getInstance("entityManager");

            if(entityManager == null) {

                log.fatal("entityManager is not available in seam context!");

            }

        }

        Query query = entityManager.createNamedQuery(spName);

        NEXTNUMBER (Integer) query.getSingleResult();     



                         




Thursday, January 10, 2013

Recovering Windows key

Have you ever wondered how much pain is it when u loose you product key. Windows 8 have 90 days free support where they can remotely access your computer and try to fix it.

Well I was trying to install .net 3.5 for running  downward compatible programs but oh my got it was just in a loop.  I cant get key because I downloaded from my Academic institute and they just wont give me access.

Well not deviating from my topic how to find product key here is how it worked for me

Belarc Advisor: http://www.belarc.com/free_download.html
(It does a good job of providing a wealth of information.)
Also: http://www.magicaljellybean.com/keyfinder.shtml
and: http://www.nirsoft.net/utils/product_cd_key_viewer.html
and RockXP: http://www.majorgeeks.com/download4138.html which has additional features

are the four utilities listed on (source: http://answers.microsoft.com/en-us/windows/forum/windows_8-windows_install/where-do-i-find-the-windows-8-product-key-when-it/d4c5c0c1-825d-47f2-9bed-d9625c7e68ff)

Belarc Advisor didnt worked for me. :(

But  http://www.magicaljellybean.com/keyfinder.shtml actually worked.

Now I am gonna try my OS registry files and see but ya best of luck. :)

Monday, January 7, 2013

Changing WAR or adding time stamp to your war name for maven supported project

Changing names for a maven project can be done in one to the two ways.

Easy fix:

The war name is same as artifact ID

    <groupId>com</groupId>
    <artifactId>ABC</artifactId>
    <version>0.0.1-SNAPSHOT    </version>
    <packaging>war</packaging>


will generate ABC.WAR in target specified folder


Other way:

So in this senario I also want to attach dynamically generated timestamp to my war

We will need to install plugin to generate time stamp as we want
 
           <build>
                <finalName>
                        ${project.artifactId}-${project.version}-${timestamp}
                </finalName>
                <plugins>
                    <plugin>
                        <groupId>
                            com.keyboardsamurais.maven
                        </groupId>
                        <artifactId>
                            maven-timestamp-plugin
                        </artifactId>
                        <version>
                            1.0
                        </version>
                        <configuration>
                            <propertyName>
                                timestamp
                            </propertyName>
                            <timestampPattern>
                                    dd.MM.yyyy HH.mm
                            </timestampPattern>
                        </configuration>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>
                                        create
                                    </goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>

Should output: ABC-0.0.1-SNAPSHOT-07.01.2013 11.54.war