Saturday, December 31, 2011

One table for 2 or More Classes

In some situations we may need create 1 table out of 2 or more classes. for instance when we map inheritance.

New Annotations :
  • @Embeddable : when a class is going to be embedded inside another persistence class we have to annotate the class as embeddable
  • @Embedded : When some (object) field is going to persist inside a class. The object type must annotated as embeddable

One Class Into 2 Tables

In some cases we have to create 2 tables out of one class. In such cases those tables must must maintain the referential integrity.

New Annotations Used :
  • @Table(name="Customer")  : Main table (1st table)
  • @SecondaryTable(name="CustomerDetail") : Secondary table
  • @Column(table="CustomerDetail") : Mark the fields which are going to maintain in the second table

Genetating Primary Key (HIBERNATE Note 2)

Here I am going to note down how to generate primary keys in hibernate.
Q : How do I drop a schema?

AnnotationConfiguration config=new AnnotationConfiguration();
new SchemaExport(config).drop(true, true);

ok lets see how to annotate a class to generate primary keys.

Note : Useful annotations

  • @Table (name="TableName") : Specify a name for the table
  • @Column(name="ColumnName") : Specify a column name
  • @Column(nullable=false) : the specified column cannot be empty
  • @Transient : The relevant attribute is volatile. not persistent
  • @Basic : default annotation. if we don't give annotation. default one become Basic
  • @Temporal(TemporalType.DATE) :  used to annotate date types
  • @Temporal(TemporalType.TIME) : used to annotate time
  • @Temporal(TemporalType.TIMESTAMP) : used to annotate date and time (time stamp)
  • @GeneratedValue : generate values automatically
  • @GeneratedValue(strategy=GenerationType.TABLE, generator="stdId") most DBs support this type. and generate keys in a sequence. SEQUENCE type is not supported by DBDerby.

Getting started with HIBERNATE note 1

The best Hibernate video tutorial I have ever seen.
I have configured the user library and hibernate.cfg.xml for the MySQL.
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"



        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/hibernatesample</property>
        <property name="connection.username">root</property>
        <property name="connection.password"></property>
        <!-- define default schema -->
        <property name="hibernate.default_schema">hibernatesample</property>

Saturday, December 17, 2011

Download Binary File From The Internet


public class A{
 public static void main(String args[]){
  A a=new A();

Friday, December 9, 2011

Inserting and Retrieving images to sqlite

I tried with BinaryStream and Blob but both don't work properly.
Blob is not implemented with sqlite jdbc driver.

Note : setBytes() , getBytes() works properly with sqlite.

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package sqliteimagetest;

import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

Showing an Image

file path -> FileInputStream -> BufferedImage -> Image -> ImageIcon

   1 File imageFile=new File("C:\\garden.jpg");
   2 try {
   3     FileInputStream fis=new FileInputStream(imageFile);
   5     BufferedImage;
   6     Image img=Toolkit.getDefaultToolkit().createImage(buffImg.getSource());
   7     ImageIcon imgico=new ImageIcon(img);
   8     jLabel1.setIcon(imgico);
  10 } catch (Exception e) {
  11     System.out.println("Error : " + e.getMessage());
  12 }

JLabel only supports ImageIcon type.

JNI | Returning an Array

   1 public class ArrayRet{
   2         public native int[][] rand2DArray(int size);
   4         public static void main(String args[]){
   5                 ArrayRet jniAr=new ArrayRet();
   6                 System.loadLibrary("randomintdll");
   8                 int randArr[][]=jniAr.rand2DArray(3);
   9                 for(int i=0;i<3;i++){
  10                         for(int j=0;j<3;j++){
  11                                 System.out.print(" " + randArr[i][j]);
  12                         }
  13                         System.out.println();
  14                 }
  16         }
  17 }

JNI With Arrays

   1 class ArrayJNI{
   2         public native int getSum(int[] numbers);
   4         public static void main(String args[]){
   5                 System.loadLibrary("arraylib");
   6                 ArrayJNI arrjni=new ArrayJNI();
   7                 int intarr[]={12,32,43,13,54,5,-2};
   8                 int sum=arrjni.getSum(intarr);
   9                 System.out.println(sum);
  10         }
  11 }

Thursday, December 8, 2011

JNI With Strings

   1 class StringJNICaller{
   2         public native String getUppercaseString(String inpStr);
   4         public static void main(String args[]){
   5                 System.loadLibrary("stringlib");
   7                 StringJNICaller sjnic=new StringJNICaller();
   8                 String uprstr=sjnic.getUppercaseString("hello sri lanka");
   9                 System.out.println(uprstr);
  10         }
  11 }

My JNI Experiments Part I | Handling integers

JNI(Java Native Interface) is a very important aspect of Java programming language. JNI provides an interface to the native environment. So we can execute native methods within our Java programs. Lets assume that we  have to do a task that need high performance in our Java program. C/C++ is good for high performance tasks rather than Java. If we can cooperate or embed C/C++ with our Java code, we can achieve our high performance targets. But Java is interpreted(byte code based) and C/C++ is compiled. So JNI is the way to cooperate those native code with our Java code.

Lets assume we have calculate factorial of a given integer value. calculation is done by a code written in C++. We call the C++ native method within our Java program and show the result on the console.

Step 1 : write Java code
This Java program contains the main method
   1 public class OurProgram{
   2         public static void main(String args[]){
   3                 try{
   4                         System.loadLibrary("myfactorialdll");
   5                         JNICallerInt jnic=new JNICallerInt();
   7                         for(int i=1;i<15;i++){
   8                                 int factorial=jnic.factorial(i);
   9                                 System.out.println("Factorial of " + i + " is \t : " + factorial);
  10                         }
  12                 }catch(UnsatisfiedLinkError e){
  13                         System.out.println("Unsatisfied link to the dll");
  14                 }
  15         }
  16 }
create a interface to the C++ method
   1 class JNICallerInt{
   2         public native int factorial(int value);
   3 }

© kani.stack.notez 2012 | Blogger Template by Enny Law - Ngetik Dot Com - Nulis