Archive for the ‘Uncategorized’ Category.

Increment with hibernate generator

This is continuation of my previous post.

We discussed with different type of generators for primary keys and we have seen an example with ‘assigned’ previously. We can see ‘generator‘ in this post.

RoseIndia says we shouldnt use this in clustered invironment.

Create a table using the following structure.

BOOK-table structure

BOOK-table structure

Let your DTO be like this

package hib;

public class Book {
 private long bookID;
 private String bookName;
 public long getBookID() {
 return bookID;
 }
 public void setBookID(long bookID) {
 this.bookID = bookID;
 }
 public String getBookName() {
 return bookName;
 }
 public void setBookName(String bookName) {
 this.bookName = bookName;
 }
}

Add the following entries to contact.hbm.xml inside the root element

<class name="hib.Book" table="BOOK">
 <id name="bookID" type="long" column="ID">
 <generator></generator>
 </id>
 <property name="bookName">
 <column name="BOOKNAME"></column>
 </property>
 </class>

Let your client program be as follows

package hib;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class IncrementTest {

 /**
 * @param args
 */
 public static void main(String[] args) {
 Session session=null;
 try {
 SessionFactory sessionFactory=new Configuration().configure().buildSessionFactory();
 session = sessionFactory.openSession();
 Transaction tx = session.beginTransaction();
 System.out.println("Transaction beginning");
 Book book = new Book();
 book.setBookName("LIVE LA FRANCE");
 session.save(book);
 System.out.println("book stored");
 tx.commit();
 } catch (HibernateException e) {
 e.printStackTrace();
 }finally
 {
 if (session!=null){
 session.flush();
 session.close();
 }
 }
 }
}

And be happy, your inserted the records with auto incrementing ID :)

book console

book console

book result

book result

Show light on Hibernate mapping file

This is in continuation of my previous post.

Lets take the hibernate mapping file and get to the tags used in that file.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="hib.Contact" table="CONTACT">
 <id name="id" type="long" column="ID" >
 <generator/>
 </id>

 <property name="firstName">
 <column name="FIRSTNAME" />
 </property>
 <property name="lastName">
 <column name="LASTNAME"/>
 </property>
 <property name="email">
 <column name="EMAIL"/>
 </property>
 </class>
</hibernate-mapping>

We could see some XML tags tries forms this mapping. Their explanations are given below.

hibernate-mapping

Root element of this mapping

class

maps the java class and SQL table

id

This will handle the primary key constraint. In addition this is the unique representation of an object.
Attributes of ID elements are given below.

name:

The property name used by the persistent class.

column:

The column used to store the primary key value.

type:

The Java data type used.

unsaved-value:

This is the value used to determine if a class has been made persistent. If the value of the id attribute is null, then it means that this object has not been persisted.

generator

This is the notification to generate the primary key. The following values to this attribute decides the type of generator

Increment:

generate primary keys of type long, short or int that are unique only. It should not be used in the clustered deployment environment.

Sequence:

Hibernate can also use the sequences to generate the primary key. It can be used with DB2, PostgreSQL, Oracle, SAP DB databases.

Assigned

Assigned method is used when application code generates the primary key.

property

The property elements define standard Java attributes and their mapping into database schema. The property element supports the column child element to specify additional properties, such as the index name on a column or a specific column type

First Post for WordPress 2.7

hai, friends it’s test posting for wordpress 2.1 to 2.7 upgrading…. :)

Connecting Sony Ericsson W810i to Mandriva and executing AT commands with Java

projects

Hi,

This post is to document the steps I overcome to connect my sony ericsson W810 as a serial modem with windows and linux and passing AT commands to it. My ultimate aim is to operate the phone in command line.

My Windows experience is sour, since sun doesnt offer its javax.comm version of jar for windows. From the googling I found sun issued it before and stopped as of now. I found the old version of comm.jar from the net from an unauthenticated website which resolved all issues. My phone is found to be a serial modem connected to COM7 port. Then passing AT commands reached the phone and I got the reply.

My Linux experience is time consuming, may be because of my ignorance. I am not at all able to mount the phone to any ports such as /dev/tty0, 1, 2. Usually USB mobile phones to be mounted in /dev/ttyACM0 or /dev/ttyACM1 in linux. From the googling I found I need to install the PCMCIA package, http://kernel.org/pub/linux/utils/kernel/pcmcia/pcmcia.html , luckily my Mandriva 2009 (again a FREEdom OS) bundle it by default. Then I found my phone got detected in /dev/ttyACM0.

When I connected the phone I found the following in the kernel logs. Use dmesg | tail -5 to do this.

Feb 22 11:58:32 pallava kernel: usb 3-1: new full speed USB device using ohci_hcd and address 5
Feb 22 11:58:32 pallava kernel: usb 3-1: configuration #1 chosen from 1 choice
Feb 22 11:58:32 pallava kernel: cdc_acm 3-1:1.1: ttyACM0: USB ACM device
Feb 22 11:58:32 pallava kernel: cdc_acm 3-1:1.3: ttyACM1: USB ACM device
Feb 22 11:58:32 pallava kernel: usb 3-1: New USB device found, idVendor=0fce, idProduct=d042
Feb 22 11:58:32 pallava kernel: usb 3-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
Feb 22 11:58:32 pallava kernel: usb 3-1: Product: Sony Ericsson W810
Feb 22 11:58:32 pallava kernel: usb 3-1: Manufacturer: Sony Ericsson
Feb 22 11:58:32 pallava kernel: usb 3-1: SerialNumber: 352337019712611_0

But my java is not able to understand the port it was listing only the default ports as

SessionIndependent_/dev/ttyS0 1 Port currently unowned
SessionIndependent_/dev/ttyS1 1 Port currently unowned

Later I found the issue is with the java io config file javax.comm. It doesnt contain the entries for ACM ports. I am not sure they will be detected automatically, that doesnt happen to me.

# Paths to server-side serial port devices
serpath0 = /dev/ttyS0
serpath1 = /dev/ttyS1

I need to add ACM ports manually, which resolved all issues.

serpath2 = /dev/ttyACM0
serpath3 = /dev/ttyACM1

As of now its working fine, but now and then I am getting the following exception when I try to open the com port.

java.io.IOException: Not all params are supported by kernel
at com.sun.comm.LinuxSerial.nativeSetSerialPortParams(Native Method)
at com.sun.comm.LinuxSerial.setSerialPortParams(LinuxSerial.java:346)
at gf.sms.pack.Port.open(Port.java:46)
at gf.sms.pack.SendSMS.run(SendSMS.java:21)
at java.lang.Thread.run(Thread.java:619)

Will find why I am getting the error and document it as well.

I got immediate response from ILUGC Chennai Linux User Group, http://chennailug.org/ ,  regarding this. My sincere thanks to them.

java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence

Hi,

I have taken a short break! There are many more error messages, which are keep on accumulating behind the screens. But I am so lazy to put them in grassfield.

Anyway, today I got interrupted with an interesting exception

java.io.UTFDataFormatException: Invalid byte 1 of 1-byte UTF-8 sequence

The scenario was, I am trying to parse an xml string. I am taking the byte array from the xml string, and give that array as input to xml reader stream. I have used java.lang.String.getBytes() for this.

Unfortunately, I got a chinese (or any other funny) characters as a value of one node in the xml. Ooof. I got up with the above error. Later, I found that getBytes() method supports only the western encoding, not UTF-8. So by using java.lang.String.getBytes("UTF-8") method, we solved the issue! nice na!