Skip to content

Writing a book about Java EE 5

A couple of days ago a friend sent me an email saying that I haven‘t blogged for few weeks now. The reason is that I‘m writing a book about Java EE 5 and it‘s taking me a lot of time (after work and in the week-ends). The chapters will roughtly be : * Presentation of Java EE 5 * Presentation of the Architecture of the application to develop * Java Persistent API * Stateless EJB 3.0 * JNDI an remote access with swing client * JSF 1.2 and JSP 2.1 (Unified EL) * Stateful EJB * JMS and MDB * Web Services (with Jaxb 2) It‘s a practical book, not a reference one, so there is only 30/40 pages per chapter with external references to go to. It‘s aimed at experienced java developpers who have had web development experience. I am a JBoss/Hibernate user but unfortunately I had to leave these frameworks because not really Java EE 5 compliant (stil JSP 2 and JSF 1.1). I‘m using Glassfish with Toplink and Derby. I lost a bit of time with this switch but it‘s ok. I didn‘t know Glassfish before and I‘m quite impressed with what they‘ve done. Ok, have to go and I will blog more about what I‘m doing… I should publish an article about Java EE 5. PS : JSF is driving me mad

Read More →

How to unit test CRUD operations

I like to unit test CRUD (Create, Read, Update, Delete) operations for my domain objects as well as find all. I use the same template again and again, it’s quick to do and makes sure that the basic operations work. Here is what I do : * A first find all to get all the objects from the database * Create an object with random values and persist it into the database * Find the created object from the database * Make sure it exists * Make sure it has the right random values * Update the created object with other random values into the database * Find the updated object from the database * Make sure it still exists * Make sure it has the new random values * A second find all makes sure that there is one more object in the database * Delete the object from the database * Find the object from the database * Make sure it doesn’t exist * A third find all makes sure that there is the initial number of objects in the database Here is the (simplified) JUnit code that tests the CRUD operations for an Item. @Test public void testCRUD() throws Exception { // Gets two random numbers Long random = getRandom(); Long updateRandom = getRandom(); // Item is the domain object Item item = new Item(); // The method findAll brings back all the objects from the DB […]

Read More →

HelloWorld with JPA, Hibernate and MySql

I love Hello Worlds. So when I read the blog HelloWorld with JPA, TopLink and MySql I’ve quickly copied/pasted the code, downloaded everyhting and made it work. Good. But because I’m more a Hibernate user, I thought, let’s give it a try with the same example but using Hibernate EntityManager instead of Toplink. So here is the same simple example of standalone java application using Java Persistence API (JPA), Hibernate, and MySql 5. Here is what you have to do : * First, download Hibernate Core 3.2.CR2, Hibernate Annotations 3.2.0 CR1 and Hibernate EntityManager 3.2.0 CR1 * Write the META-INF/persistence.xml file <persistence> <persistence-unit name="hello-world" transaction-type="RESOURCE_LOCAL"> <class>com.foo.Greeting</class> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test"/> <property name="hibernate.connection.username" value="root"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> </properties> </persistence-unit> </persistence> * Use the same Entity class and Main class than the one written in the HelloWorld with JPA, TopLink and MySql blog * Start MySQL * Compile and run the project with the folowing classpath java cp ..\classes;%LIB%\ejb3-persistence.jar; %LIB%\hibernate-entitymanager.jar;%LIB%\hibernate3.jar;%LIB%\jboss-common.jar; %LIB%\dom4j-1.6.1.jar;%LIB%\hibernate-annotations.jar;%LIB%\commons-logging-1.0.4.jar; %LIB%\cglib-2.1.3.jar;%LIB%\javassist.jar;%LIB%\commons-collections-2.1.1.jar; %LIB%\mysql-connector-java-5.0.3-bin.jar;%LIB%\ehcache-1.2.jar;%LIB%\asm.jar; %LIB%\jta.jar com.foo.HelloWorld

Read More →

Article on JUnit 4

I’ve published an article about JUnit 4 on DevX : Get Acquainted with the New Advanced Features of JUnit 4. JUnit 4 has changed a lot and it looks like there is much more to come. This article shows you how to migrate from JUnit 3.8 to JUnit 4 and gives you an overview of JUnit 4 new features. An article is never written alone. So first of all I would like to thank my wife Denise who read and corrected a boring technical article that she didn’t understand a word. Second, Lori Piquet from JupiterMedia for his help and expertise in writing articles. And a special thanks to both my friends Alexis Midon and Zouheir Cadi who took a bit of time to review this article. Do not hesitate to post comments.

Read More →

Annotations are great ! Really ?

Do you remember 6/7 years ago. EJBs were the big thing and with them came deployment descriptor written in XML. Then Webapp with their web.xml, and then Enterprise applications with their application.xml. XML was everywhere, XML was cool. Imagine, you can write your Java code, deploy it and after just twist bits and pieces in your XML files. XML, XML… Then we had SAX, DOM, JAXB, Castor, XSL, XSLT, Schemas, DTD… God, life became so flexible. Too flexible ? Of course ! XML has taken over our lives, we all hate XML now. We hate it so much that we started using xDoclet to get rid of it. But now, thanks to Java 5, we are really getting rid of XML and giving up all this XML flexible into more rigid annotations. But it‘s so much better : code and annotations are at the same place, on the same class, on the same method. Much better, easier to read. You see, annotations are so cool that we use them for persistence (Hibernate, JPA…), components (EJB 3), unit testing (JUnit 4), XML (with JAXB 2)… and so on. Yes, I tell you, annotations are great ! And there is no way that in 6/7 years we will end up coding this : @Entity @Name('componentName') @Inheritance @Scope(ScopeType.CONVERSATION) @Logger('loggerName') @TransactionAttribute(NEVER) @Remote({examples.Animal.class}) @ExcludeDefaultInterceptors @NamedQuery(name='findAllAnimals') class Animal extends Creature ( @Id @SequenceGenerator(name='ANIMAL_SEQ', allocationSize=25) @GeneratedValue(strategy=SEQUENCE, generator='ANIM_SEQ') @Column(name='ANIM_ID') private String id @AssociationOverride @Enumerated private Owner owner @Deprecated […]

Read More →

JAXB 2.0 Hello World

For people who have played with JAXB 1.x, JAXB 2.0 has the same beahavior: it can marshall/unmarshall object from/to XML. But the syntax is completly different. It uses all kind of annotations. This blog is just about writing and executing a good old Hello World with JAXB 2.0. First you need to download and install the binary. For the following example you will just need to put jaxb-api.jar and jaxb-impl.jar in your classpath. The following code represents a HelloWorld class with two attributes. The main method creates a HelloWorld object, sets some values, marshalles it to the hello.xml file, displays the xml representation, unmarshalles the xml file into a HelloWord and displays the toString method : import javax.xml.bind.JAXBContext; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlRootElement; import java.io.File; import java.io.FileOutputStream; @XmlRootElement public class HelloWorld { private String hello; private Integer world; public String getHello() { return hello; } public void setHello(String hello) { this.hello = hello; } public Integer getWorld() { return world; } public void setWorld(Integer world) { this.world = world; } public String toString() { return hello "-" world; } public static void main(String[] args) throws Exception { // Set up file and JAXB context final File file = new File("hello.xml"); JAXBContext context = JAXBContext.newInstance(HelloWorld.class); // Creates a HelloWorld object HelloWorld hw = new HelloWorld(); hw.setHello("Hello !!!"); hw.setWorld(1234); // From a HelloWorld object creates a hello.xml file Marshaller m = context.createMarshaller(); m.marshal(hw, new FileOutputStream(file)); m.marshal(hw, System.out); // From the hello.xml […]

Read More →

Java Day 2006 in Paris

Well, in fact Java Day 2006 wasn’t held in Paris itself but in Versailles, just in front of the castle So, yes, things also happen outside the US. I know, it was just a one day java conferences with only 5 sessions, nothing compare to the four days of Java One and its 188 sessions. But still, we had James Goslings on stage. If you look at the agenda, this is what happened. Welcome Eric Mahé ? Technology Advisor ? Sun Microsystems France Eric, who has been working for 17 years at Sun, introduced the java day and the agenda. After few marketing slides (you always have to thank the sponsors) he joked about Extreme Pixel Programming. The idea is that Eric is not a java programmer, the tool he uses the most is Photoshop and he would like to use it to code some java classes. So he opened Photoshop, dragged & dropped some images, saved it as a raw file, opened the raw file, copied the content and pasted it into a java file in Netbeans. He then executed it, and the image was displayed with animation. He said that the one who finds the trick will win one of its own art work… bets opened. The Next Wave of Java Tools James Gosling ? Chief Technology Officer of Sun’s Developer Products group James arrived on stage and threw some left over t-shirt of Java One at the […]

Read More →