Pages

Showing posts with label JAVA. Show all posts
Showing posts with label JAVA. Show all posts

Wednesday, March 09, 2011

"java.lang.IllegalArgumentException: Unknown entity" and "org.hibernate.MappingException: Unknown entity:"

I got this exception during using hibernet with MySQL. This is a very simple error. Just you need to add the entity class name in persistence.xml file as follows (marked red). In fact most of the IDEs will provide you a GUI for doing this.

 

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

  <persistence-unit name="SimpleTestPU" transaction-type="RESOURCE_LOCAL">

    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>yourPackage.yourEntity </class>

    <properties>

      <property name="hibernate.connection.username" value="root"/>

      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>

      <property name="hibernate.connection.password" value="blah blah "/>

      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/da2642e0 "/>

      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>

    </properties>

  </persistence-unit>

</persistence>

 

Wednesday, February 23, 2011

How to update only the time part of a date type column in MySQL

I was reading data from the web and there was only date  in this format (01/01/2011). So it came to my database with the time part all zeros. But I at least required fake times instead of all zeros. To do that the query in MySQL is as follows
   
                   update news set news.post_date = TIMESTAMPADD(HOUR,14,news.post_date) where TIME(news.post_date) = TIME("00:00:00");

by the way one more thing, If you are using Hibernet to access your database (As I was doing) dont forget to change the temporal type to TimeStamp. Sample code is as follows

              @Column(name = "post_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date postDate;

Thats it.

Thursday, October 07, 2010

java.lang.ArithmeticException: Rounding necessary

I was working with JPA, Hibernate and Netbeans. Netbeans automatically generates the entity classes from the database. But the auto generator may confuses with the number type in oracle. Sometimes it generates BigInteger and sometimes BigDecimal. In my case the auto generator made the field BigInteger but actual value in the database was decimal. Thats why  java.lang.ArithmeticException: Rounding necessary. The variable types were changed from BigInteger to BigDecimal and the Exception was no more.

Saturday, June 26, 2010

JavaZone Trailer: Java 4-ever

If you love JAVA you will love this video :D

Saturday, June 12, 2010

java.lang.ClassNotFoundException for servlet in App Engine

Today I just got this exception from my app engine. I switched back to my local set up and run the app. It ran well and fine. I was confused. I just uploaded my App once again in app engine. and the problem is solved.

Friday, June 11, 2010

Entity comparison

Today I was doing comparison between two JPA entities. I just accessed the the values via getter setters and checking the equality of them. On the debugger I am getting the values equal but the my comparison is returning me false. I was stuck a bit. Then the point became clear. It was a really silly mistake. The member variables in the entity are the wrapper class of the primitive types  of JAVA. So direct equality check will compare the whole objects rather than the values. In this case obviously two objects are not same. So to compare first we need to have the primitive values using doubleValue() or intValue() function. Or the wrapper class's compare() function can be used.

Date time Format

In different environment date time format differs a little. But this little difference wastes a lot of time in cases. I am trying to put all together here.

JAVA

Date and Time PatternResult
"yyyy.MM.dd G 'at' HH:mm:ss z"2001.07.04 AD at 12:08:56 PDT
"EEE, MMM d, ''yy"Wed, Jul 4, '01
"h:mm a"12:08 PM
"hh 'o''clock' a, zzzz"12 o'clock PM, Pacific Daylight Time
"K:mm a, z"0:08 PM, PDT
"yyyyy.MMMMM.dd GGG hh:mm aaa"02001.July.04 AD 12:08 PM
"EEE, d MMM yyyy HH:mm:ss Z"Wed, 4 Jul 2001 12:08:56 -0700
"yyMMddHHmmssZ"010704120856-0700
                                                                                      Source

ORACLE

MMNumeric month (e.g.07)
MONAbbreviated month name (e.g.JUL)
MONTHFull month name (e.g.JULY)
DDDay of month (e.g.24)
DYAbbreviated name of day (e.g.FRI)
YYYY4-digit year (e.g.1998)
YYLast 2 digits of the year (e.g.98)
RRLike YY, but the two digits are ``rounded'' to a year in the range 1950 to 2049. Thus, 06 is considered 2006 instead of 1906
AM (or PM) Meridian indicator
HHHour of day (1-12)
HH24Hour of day (0-23)
MIMinute (0-59)
SSSecond (0-59)

                                                                                                                                                                    Source

I am trying to add more. Help me with your comments..

Thursday, October 22, 2009

java.sql.SQLException: ORA-01816: month may only be specified once

I was using "dd-mon-yyyy hh24:mm" as my date format. Here I have specified mm for two times. That’s why exception occurred. I was expecting minute. Instead of mm I need to use mi. So the correct date format string would be

 

            "dd-mon-yyyy hh24:mi"

 

java.sql.SQLException: Fail to convert to internal representation

I was getting this exception in

Resultset.getLong(“column_name”);

 

Reason behind this is was simple. The data in the database was float and I was trying to convert it to long. This made this exception. Just using getFloat(“column_name”) instead of getLong(“column_name”) solved the problem.

 

Friday, July 31, 2009

Sending mail from my desktop using JAVA

For some reason I was trying to send email from my desktop using JAVA and my Gmail account. I found the best code here. There is few bugs here but its fine. I will try to post 100% bug free code in near future. Anyway you need Java Mail API to run this code. Enjoy sending email..

Sunday, July 05, 2009

Regular expression in JAVA

I found pattern matching in JAVA very simple and easy. Till now I just know two class, One is Pattern and the other is Matcher. With this two class I am working fine. and the code is also very simple. One thing to remember none of the two class cant be initiated with the new operator.

Code is something like this.

String regx = "my reg X";
String input = "here put your input string";
Pattern pattern = Pattern.compile(regx); 
Matcher matcher = pattern.matcher(input);
Now you have got your parsing engine ready. you have to just search now.

while (matcher.find()) {                 System.out.println("Match found");
  System.out.println("Start" + matcher.start());
  System.out.println("Start" + matcher.end());
}
Now stop using StringTokenizer and all other complex logics to parse something.

Wednesday, July 01, 2009

Future SIM cards

The list may be like this

  1. GPS module
  2. WI FI module
  3. NFC module
  4. And a JVM

Yah all this thing can be in your SIM card in future. To me the JVM is the most important part which is now being called as JAVA card 3. JAVA card 3 is now far far better than java card 2 which was introduced in 1998. JAVA card 3 has now like CLDC1.1 with the syntax support of JAVA 5. That is cool. Thoik now you can develop application not only for your phone but also for your SIM card.

Still the number of JAVA card 3 supported SIM card is very small now in market. So SUN has developed a box which is named SUN spot. In fact in the Sun SPOT you can insert your current real SIM card and then connect the Sun SPOT to your mobile phone and use your phoen as usual sometimes even better. Interestingly Telenor has a good support on this project. You will get some nice demos on the following video.

Saturday, May 02, 2009

Converting hexadecimal to decimal in JAVA

This is really as simple as it is.

import java.io.*;
import java.lang.*;

public class HexadecimalToDecimal{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the Hexadecimal number:");
String str= bf.readLine();
int i= Integer.parseInt(str,16);
System.out.println("Decimal:="+ i);
}
}

Wednesday, March 11, 2009

How to write new line to a file in JAVA

Printing a new line is …

       System.out.print(“\n”); You may feel a joke here.

 But when you are trying to do the same thing with a file it becomes a little tricky.

 Print(“\n”) will not print a new line to a file..

    The trick is also simple. You just have to add a carriage return before your new line. Remember before the new line..

 That means ::: Print(“\r\n”) ;

 So very simple here is a sample code with BufferedWriter.

 BufferedWriter bw = null;

        try {

            bw = new BufferedWriter(new FileWriter(new File("hello.txt")));

            bw.write("helo world" + "\r\n");

            bw.write("hello world 2");

            bw.flush();

            bw.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

 

 

 

Thursday, February 12, 2009

Reading all the contents of a web page in JAVA

You need to use the URL class. The sample code is stated below.

public static void main(String[] args) {
        // TODO code application logic here
        URL url;
        InputStream is = null;
        
        BufferedReader br = null;
        try {
        
        
            url = new URL("http://dsebd.org/latest_share_price_all.php");
            is = url.openStream();
            br= new BufferedReader(new InputStreamReader(is));
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
        
        String tempLine;
        try {
            while((tempLine = br.readLine()) != null)
                System.out.println(tempLine);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        
        
        
        
        
    }

--
http://ifteebuet.blogspot.com/

Saturday, January 31, 2009

Today for the first time I am writing some PHP code on netbeans 6.5. This seems amazing. I am mainly used to JAVA. But I think NB 6.5 will be really helpful in learning PHP in JAVA style
Have a look at this screen shot.  I will try to continue with more NB PHP feature in near future..

Wednesday, January 28, 2009

Now I am inserting huge amount of DATA in ORACLE. Its about 6 million rows. I am using JAVA for chunked insert. So that I can commit after a while. Using JAVA threads improved the performance a lot. Using one where clause you can make chunks of the whole data for insertion then just use threads to inserts few chunks of data at a time. It will decrease the load on the DB also. Because it will not need to increase the memory size for cached data for the insertion data. By the way I will go for a procedure very soon...

Saturday, September 27, 2008

At the very first time I was thinking of doing some JAVA ode to do this...

But it is really simple...


you can just use PL/SQL developer to do this..

If your table contains any CLOB/BLOB type field then just open your table in data editable mode in PL/SQL. and select the Image/ File tab...


and you will get all you need...