Total Pageviews

Friday, January 20, 2012

Iterating over result list from a JPA query

Sometimes when using EJB 3.x, it is necessary to use JPA QL or SQL native queries to fetch objects from the database. It might be used to achieve better performance or to just fetch a particular set of attributes of the object but not the complete object with all its dependencies.

There's one thing you have to keep in mind when doing this: The result list containing the object will be an array of objects!

"The SELECT clause queries more than one column or entity, the results are aggregated in an object array (Object[]) in the java.util.List returned by getResultList( )"

Working example:
 Query query = manager.createQuery("SELECT v1.bitbit, v1.numnum, v1.someTime, t1.username, t1.anotherNum FROM MasatosanTest t1 JOIN MasatoView v1 ON v1.username = t1.username;");  
   List results = query.getResultList( ); // Fetches list containing arrays of object  
   Iterator it = results.iterator( );  
   while (it.hasNext( )) {  
     Object[] result = (Object[]); // Iterating through array object   
     Boolean first = (Boolean) result[0]; // Fetching the field from array  
     /* Likewise for all the fields, casting accordingly to the sequence in SELECT query*/  

There's is even the possibility to avoid casting completely by using a constructor expression with the appropriate arguments in the SELECT section:
 SELECT new org.somepackage.XEntity(x.a, x.b) FROM XEntity x  

Remember to declare the appropriate constructor.
The code fragments and the solution in this blog post was taken from this question on stackoverflow.

Monday, January 16, 2012

How to change the DB schema to dbo on SQLServer 2008

Every now and then you are in need to backup and restore databases from one SQL Server to another. Furthermore, sometimes the version of the source SQL Server differs from the version of the target SQL Server.
With SQL Server 2008 Microsoft separated users from schemas.
Imported tables of an imported database may only become accessible with a prefix like "jonathan.MY_TABLE_NAME".
You can change the schema name of those tables using the ALTER SCHEMA command.
It is described in more detail here.
In my case it worked.

Friday, January 06, 2012

How to solve " Target Filesystem doesn't have /sbin/init" on Ubuntu 10.04

I restarted my Ubuntu 10.04 box today and it wouldn't start. It gave me this error:

 mount: mounting /dev/disk/by-uuid/***************************** on /root  
 failed: Invalid argument  
 mount: mounting /sys on /root/sys failed: No such file or directory  
 mount: mounting /dev on /root/dev failed: No such file or directory  
 mount: mounting /sys on /root/sys failed: No such file or directory  
 mount: mounting /proc on /root/proc failed: No such file or directory  
 Target file system doesn't have /sbin/init  
 No init found. Try passing init= bootarg  

So after googling around I found out that this seems to be a common problem ;-)
I have a JMicron RAID controller in my box using a RAID mirror but that doesn't help very much :-( It's a logical error and therefore replicated on the other disk as well.

I found this forum entry which helped me a lot and applied the fix using the following steps:
  1. Install GParted on an USB Stick using Tuxboot (my Linux box does not have a DVD drive)
  2. Boot GParted and wait until the graphical partition editor comes up
  3. Identify the boot partition (mounted on /). My boot partition is named "/dev/mapper/jmicron_GRAID1"
  4. Open a terminal and type in the following command
    sudo e2fsck -f -y -v /dev/mapper/jmicron_GRAID1
  5. When e2fsck finishes it usually must have been corrected some errors. So it's best to take a look into the summary.
  6. Reboot your system
After I applied these steps my system booted up without errors again.
Now I need to identify the source of the problem ...