Sunday, August 14, 2011

MYSQL: logging with out userid and pwd

at times when u import the db it may erase ur userid and pwd. When the java tries to connect to the db it throws exception that this ip is not allowed to connect to the db; in such a case use the below fix. it removes the necessity to have userid and pwd to loginto the mysql

  1. skip-grant-tables under the section [mysqld]
  2. restart the mysql: /etc/init.d/mysqld restart

be careful that u dont do this on production dbs

MySQL:Got a packet bigger than 'max_allowed_packet' bytes

This is generally the case when u try to import a database from a dump. The fix is to increase the max_allowed_packet size follow the below steps

  1. vi /etc/my.cnf
  2. add: max_allowed_packet=1G under the section [mysqld] and save the file
  3. restart the mysql: /etc/init.d/mysqld restart;
  4. it should be smooth, if at all u get a warning telling it will ignore the cnf file, then try
    1. chmod 655 /etc/my.cnf and again try to restart.

This should do.

Friday, August 12, 2011

egrep–useful linux command

 

suppose we want to search for a string “abc” in a file then we use

grep “abc” /path/to/file/file.txt

but what if u want to search for either “abc” or “xyz”? u need to grep twice. instead use egrep

egrep “abc|xyz” /path/to/file/file.txt

the best use of egrep comes in when we tail a file and want to search for a set of strings in the tail eg:

tail –f /path/to/file/file.txt | egrep “abc|xyz”

Monday, August 8, 2011

Transaction issue :: hibernate

getSession() –> the java doc says it will create a new session/ continue with the existing one based on getHibernateTemplate().setAllowCreate(…); set to true/ false.

How ever, i found the things to be diffarent. I had a 2 DAOs one executing: getHibernateTemplate().find(..); the other using getSession().createCriteria(..); They are wrapped by the same transaction interceptor. What i found is when the 2nd DAO runs the hibernate throws illegal state exception.

Why?? The getSession() always tries to crate a new session, if the setAllowCreate is set to false, then it throws illegal argument exception. Why this devaiation??? as always only god knows it.

To get the current session in 2nd DAO, i found the fix: getHibernateTemplate().getSessionFactory().getCurrentSession(); and things went off very fine.

Friday, August 5, 2011

Adding complex and & Ors in hibernate

Recently i had to do a query as:

select * from t_table where (t.a=’a’ and t.b=’b’) or (t.a=’aa’ and t.b=’bb’). One way to get over this is via HQL query… which i hate.. i wanted to do it using criterias… finally Conjunction and disjunctions came for resque. Below is the code.

Session session = getSession();
try {
Criteria crit = session.createCriteria(Table.class);
Disjunction conMerOrs = Restrictions.disjunction();
for (AB ab: ABList) {
Conjunction ands = new Conjunction();
ands.add(Restrictions.eq("a", ab.a()));
ands.add(Restrictions.eq("b", ab.b));
ors.add(ands);
}
crit = crit.add(ors);
return crit.list();
} finally {
session.close();
}



let me know if it helped

Sunday, July 24, 2011

Q

May you always have
Love to share,
Health to spare,
And friends that care.
got from a JIRA post 

Friday, July 22, 2011

Auto generating hibernate code using Eclipse (not my eclipse)

I hate using non-licensed tools. generally when it comes to code generation for Hibernate, pple tell of myeclipse; and heres the way to do it. Myeclipse usage requires licenses; so this is how pple get around it –uninstall it just before the trial expires and reinstall it again.

I hate all this crappy stuff and wanted a clean way to do it. I have used eclipse helios for this and guess it would work for other eclipse versions as well

  1. in eclipse help > install new software > give the url: http://download.jboss.org/jbosstools/updates/stable/helios/ and hibernate - http://download.jboss.org/jbosstools/updates/development
  2. say you added link 1, click on next and select all options then click on finish
  3. then redo the above for the link 2 aswell.

Now the hibernate plugin is installed. Below i tell the steps to auto genrate the code

  1. File > new > others > hibernate > new Hibgernate console configuration file
    1. select the project name, configurae a new db connection, property file, configuration files (all will take abt 1 min to complete)
  2. similarly do  File > new > others > hibernnate > new hingernate cfg.xml
  3. similarly do FIle > new others > hibernate > new reverse engineering file
    1. fill the step 1
    2. the step2 click on refresh after having selected the console configuration file in the dropdown
    3. select the table u r looking for
    4. incase ur table has foreign keys select the paret table also
    5. click on finish
  4. Inthe top bar (where u see save, print icos) u should see a new RunAs icon ( in black suffix)
    1. click on it you wil see a drop down
    2. select hibernate code generation configurations
    3. now double click on hiberante code generation (in the left tree)
    4. in the right side select consolef configuration
    5. select a temp directory, reverse engineering file we already created
    6. and say run; Bingo.. the code generation is done.

Now the qn: why did they build the plugin so complicated? why not have it a siimple one? all of it done in a wizard??? i donk know…

let me know if it helped you.

MySQL ERROR 1005 (HY000): Can't create table

Below is the table def that caused the problem:

CREATE  TABLE IF NOT EXISTS `T_ABC` (
  `id` INT NOT NULL AUTO_INCREMENT ,
  `source_col` int NOT NULL ,
  PRIMARY KEY (`id`) ,
  INDEX `fk_src_col` (`source_col` ASC) ,
  CONSTRAINT `fk_src_col`
    FOREIGN KEY (`source_col` )
    REFERENCES `parent_table` (`id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

and on execution it was giving the error ERROR 1005 (HY000): Can't create table schema.T_ABC.frm

The fix to issue is: that parent_table.id is of bigint type, while this table foreign key is only int; when i changed the source_col from int to big_int things went well.

hope this helps somebody.

Sunday, July 10, 2011

getHibernateTemplate().find() / query.list() returns list with null values

So the problem stmt is as follows

  1. The Entity primary key is mapped as composite-id in hbm files. i.e.. all columns/ multiple columns of the entity is taken as composite key. This gnerally happens when u use code generatoros. 
  2. now you say the below in criteria query
    Criteria criteria = getSession().createCriteria(CompositeKeyEntity.class);
    criteria = criteria.add(Restrictions.in("id.subKey", list));
    List<CompositeKey> list = criteria.list();



  3. The above returns a list with entities are null.

    1. why? Tracing the src code figured out that hibernate checks each of the values of primary keys. and when ever he finds one of the value as null doesn’t return the object. (org.hibernate.type.ComponentType.hydrate())
    2. This is logical as we shouldn’t have nulls in primary keys. But the problem is its not documented anywhere!

  4. so the fix: make all composite key columns as not-null; and give a default to each of the column.

 


Let me know if it helps.

Saturday, July 2, 2011

an email...

 
1) Heavy rains remind us of challenges in life.
Never ask for a lighter rain.
Just pray for a better umbrella.
 
2) When flood comes, fish eat ants & when flood recedes,
ants eat fish.Only time matters. Just hold on,
God gives opportunity to everyone!
 
3) Life is not about finding the right person,
but creating the right relationship, it is not how we care in the beginning, but how much we care till the ending.
 
4) Some people always throw stones in your path.
It depends on you what you make with them, Wall or Bridge? Remember you are the architect of your life.
 
5) Every problem has (n+1) solutions, where n is the number of solutions that you have tried and
1 is that you have not tried. That's life.
 
6) It is not important to hold all the good cards in life.
But it is important how well you play with the cards
which you hold.
 
7) Often when we lose all hope & think this is the end,
God smiles from above and says,'Relax dear, it is just a bend. Not the end. Have Faith and
have a successful life.
 
8) When you feel sad, to cheer up, just go to the mirror and say, 'Damn I am really so cute' and you will overcome your sadness.
But don't make this a habit because liars go to hell.
 
9) One of the basic differences between God and human is, God gives, gives and forgives. But human gets,
gets, gets and forgets. Be thankful in life!
 
10) Only two types of persons are happy in this world.
1st is Mad and 2nd is Child. 
Be Mad to achieve what you desire 
and