Tuesday, December 20, 2011

Auto generated ID's for MySql and Oracle using Hibernate

Here you have auto generated ID's examples using Hibernate with Oracle and MySql databases.

Oracle

...

@Entity
@Table(name = "grupo")
public class Grupo implements GenericModel {


@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "GRUPO_ID_SEQ")
@SequenceGenerator(name = "GRUPO_ID_SEQ", sequenceName = "GRUPO_ID_SEQ", allocationSize = 1)
@Column(nullable=false)
private Long id_grupo;

...

Sequence GRUPO_ID_SEQ must exist in Oracle database.


Considering that MySql does not have sequences, if you try to use the code above with MySql you will get a runtime exception:


Caused by: org.hibernate.MappingException: Dialect does not support sequences
at org.hibernate.dialect.Dialect.getSequenceNextValString(Dialect.java:619)
at org.hibernate.id.SequenceGenerator.configure(SequenceGenerator.java:88)
at org.hibernate.id.SequenceHiLoGenerator.configure(SequenceHiLoGenerator.java:66)
at org.hibernate.id.IdentifierGeneratorFactory.create(IdentifierGeneratorFactory.java:127)

MySQL


...
@Entity
@Table(name = "grupo")
public class Grupo implements GenericModel {


@Id
@GeneratedValue
@Column(nullable=false)
private Long id_grupo;
...

The field id_grupo must be marked as auto increment in MySql.




Monday, December 12, 2011

I PASSED: 1Z0-894 Oracle Certified Expert Java Platform, Enterprise Edition 6 JavaServer Pages and Servlet Developer


I did the test on 09/12 and got through!

I was taking 70% in Mocks and hoped to pass with a score about 75% or 80%, but the test was harder than I expected and I passed with 68%.

As I had posted October, 1st, I based my studies on EPractize Labs Study Guide and Mock Exam, because I had already read Katy Sierra's book Head First J2EE1.4 sometimes on other occasions. Also I read in a Servlet 3.0 spec.

I really do not recommend EPractize Labs. The Study Guide is very superficial and Mock has many errors. In addition, the Mock is not consistent with the real exam. The real exam does not cover almost anything about custom tags and it covers much of the Servlet 3.0 spec, but Mock is upside down.

I believe Piotr's web page (http://piotrnowicki.com/2011/03/jee-6-scwcd-mock-questions/) saved me. It has 67 questions focused on the Servlet 3.0 specification. Enthuware Mock also helped a lot (http://enthuware.com/index.php/mock-exams/oracle-certified-expert/oce-jsp-servlet-mock-questions).

This Mock is cheaper and the quality of it is infinitely greater. The proof is that in addition to being 100% Test Pass Garantee, they return the money if you find three errors. Many exam questions were very similar to those on Mock.

My tip for anyone to prepare for this test is:

* Study the book Head First Servlets & JSP, Second Edition (JEE5): http://shop.oreilly.com/product/9780596516680.do?green=25526262438&cmp=af-mybuy-9780596516680.IP 
* Study the JSR-315 Spec Servlets 3.0: http://jcp.org/aboutJava/communityprocess/mrel/jsr315/index.html 
* Answering the questions of Piotr
* Make Enthuware Mock Exams

Following these steps with time, for sure you will do better than me :-)

Good studies!

Thursday, November 10, 2011

Telnet on Windows Vista


On Start menu, click on "Run..." option, then write "optionalfeatures" (without quotes) and click OK button.


"Windows Features" window will open so you can enable Telnet Client feature. Click OK and wait some minutes (MANY MINUTES in my case) while the features are been enabled.


When it is finished you have to restart the system before having fun with Telnet Client.

Tuesday, November 1, 2011

How to find UtcTimeOffsetCode table in TAP3 files

One of the information in Moc (Mobile Originated Calls) and Mtc (Mobile Terminated Calls) is Call Event Start Time Stamp.

It is formed by Local Time Stamp (yyyyMMddhhmmss) plus Utc Time Offset Code.

Utc Time Offset Code (5F-81-68) can be any integer from 0 to 99 and with this information you will be able to discover the Utc Time Offset (5F-81-67).



The problem is this information is not standardized for all TAP files, it is an information self-contained in TAP3 file, in another section. In other words, each file has its own Utc Time Offset Code list, mapping codes to real offsets.

This table is named Utc Time Offset Info and is located in Network Information section. Utc Time Offset Info can have one or more Utc Time Offset Definition and each Definition has the mapping between Utc Time Offset Code to Utc Time Offset.


In the example shown in pictures above, the offset code 65 (0x41) was linked to Utc Time Offset -05:00.

TAP3 protocol was designed this way to minimize the amount of data transferred and to avoid the repetition of
frequently identical information at the call/event level.

Tip:
Nice programs to work with TAP3 files:
TAP3 Editor (http://www.combil.com/tap3ed2.htm): Used to see structured information
ASN1VE (http://www.obj-sys.com/asn1-viewer.php): Used to see binary content of ASN.1 data

Thursday, October 20, 2011

Log4j mixing log messages between applications into Weblogic


I have an Web Service JWS application running in Weblogic 10.2 and its logs were being mixing with another web application running in the same server.

I thought that applications were totaly independent in the server but they are not. They share some classloaders. See picture bellow:



This problem occurs because jog4j.jar is shared between applications and it is retrieved from the same classloader.

In order to solve this issue, it is necessary that each application has its own log4j.jar packed in its .war (or .ear when applicable). It is important that all applications have its own log4j packed. If one of them does not, the problem will still persist.

Besides having log4j.jar packed, its also necessary to edit weblogic-application.xml to warn Weblogic to use log4j classes from application classloader.

Here you have an example of weblogic-application.xml:


<?xml version='1.0' encoding='UTF-8'?>
<weblogic-application xmlns="http://www.bea.com/ns/weblogic/90" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<prefer-application-packages>
   <package-name>org.apache.log4j.*</package-name>
</prefer-application-packages>
</weblogic-application>



If you are using ant to build your Weblogic JWS webservice with task jwsc, here you have a snippet of build.xml that you can use to pack log4j into .war and use a custom weblogic-application.xml.

log4j.jar must be placed into .war in WEB-INF/lib and weblogic-application.xml into .ear in META-INF


<target name="jwsc" description="build web service">
  
    <delete dir="${build.dir}"/>
    <mkdir dir="${build.dir}"/>  
  
    <jwsc
        srcdir="${webapp.dir}"
        destdir="${build.dir}" 
    verbose="on" debug="on"
    keepGenerated="no" >


     <jws file="${jwsc.file}"
           compiledWsdl="${jwsc.jws.compiledWsdl}"
           contextPath="${jwsc.jws.contextPath}" 
           type="JAXWS" explode="false" >
</jws>
    </jwsc>
  
    <war destfile="${war.built}" update="true" >
        <fileset dir="${webapp.dir}">
          <include name="**/*.jar" />
  </fileset>
    </war>


    <copy file="${service.src}/resource/weblogic-application.xml" todir="${build.dir}/META-INF/" overwrite="true" />
 
 </target>


It is important to know that ${war.built} must point to .war file and ${webapp.dir} must point to the web service source that owns WEB-INF folder, that owns lib folder, that owns log4j.jar.

Links:
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/programming/classloading.html
https://forums.oracle.com/forums/thread.jspa?threadID=689458
http://www.coderanch.com/t/554020/Web-Services/java/adding-jar-files-building-war
http://ant.apache.org/manual/Tasks/war.html

Wednesday, October 19, 2011

Why Log4j is not logging?


Recently I had a problem with the log mechanism in a small system I had to maintain.

It simply was ignoring log4j.properties configuration and was logging messages with INFO level.

After some time I discovered that it was using common-logging instead pure log4j.

There is no problem using commons-logging, it does work fine. The real problem was that only commons-logging-api.jar and log4j.jar were included in classpath. It was missing commons-logging.jar.

To support log4j, commos-logging needs both jars in classpath: commons-logging and commons-logging-api.

After adding commons-logging.jar in the classpath, log4j.properties started being read and respected.

Read more about commons-logging here: http://commons.apache.org/logging/guide.html#Jars Included in the Standard Distribution

Saturday, October 1, 2011

1Z0-894 Oracle Certified Expert Java Platform, Enterprise Edition 6 JavaServer Pages and Servlet Developer



I've just started my preparation to Java for Web development certification, former SCWCD - Sun Certified Web Component Developer. 


In the newest version of this certification, and already in the Oracle world, its certification
code is 1Z0-894, and the name is Oracle Certified Expert, Java Platform, Enterprise Edition 6 Servlet and JavaServer Pages Developer.


You'll find more details about this test on the Oracle site: http://education.oracle.com/pls/web_prod-plq-dad/db_pages.getpage?page_id=41&p_exam_id=1Z0_894


EPractize Labs's website also has interesting information, including a simulation of how long you will need to prepare for the test: http://www.epractizelabs.com/blog2/?p=106


Because this test is relatively new, and I had already read the book Head First Servlets & JSP 1.4 JEE twice, I decided to prepare only with the Study Guide and Mock Exam EPractize Labs:
http://www.epractizelabs.com/certification/sun/oce-jsp-servlet-exam-6.html


This material is $ 40.5 dollars, but the EPractize Labs guarantees money back if you get the test failed.


This Study Guide has some Ctlr-C/Ctrl-V errors, several typos and content is very superficial, but I intend to fill the gap in Internet surveys.


I have also created a project in Eclipse to get going in practice what I learn in the Study Guide.


My goal is to make the test in the first week of December/2011, I hope it is enough time!


If you have tips or questions about this certification, share commenting on this post.


--
PS: The result: http://dotdebug.blogspot.com/2011/12/pass-1z0-894-oracle-certified-expert.html

Wednesday, March 9, 2011

java.sql.SQLException: ORA-00911: invalid character

Tags: Java, JDBC, Oracle, ORA-00911, invalid character, semicolon

Here you have the formal description of this fault:


ORA-00911:

invalid character
Cause:identifiers may not start with any ASCII character other than letters and numbers. $#_ are also allowed after the first character. Identifiers enclosed by doublequotes may contain any character other than a doublequote. Alternative quotes (q'#...#') cannot use spaces, tabs, or carriage returns as delimiters. For all other contexts, consult the SQL Language Reference Manual.
Action:none


... but it doesn't help much if your SQL query looks fine and runs well in Toad or PL SQL Developer.

When I got this exception I thought I could have add some strange non-printable character in my StringBuilder object, but it was easier:

JDBC queries are not allowed to have semicolons.

If you have this issue, check if you have a semicolon ";" at the end of the query.

// this will work fine in Toad or PL SQL Developer but will raise ORA-00911
ResultSet result = st.executeQuery("select * from dual ; ");


// this will work fine in Java, JDBC
ResultSet result = st.executeQuery("select * from dual");



Reference:
http://ora-00911.ora-code.com/
http://www.fromdev.com/2008/09/javasqlsqlexception-ora-00911-invalid.html