Nowbody calls me Ignacio. I’m plunchete, man!

Masterbranch is hiring! Software Developer (Java preferred)

Posted in Java, Software Companies by plunchete on January 4, 2011

You can see the original reference here: Software Developer (Java preferred)

US
We’ve been around for around a year and a half and we’re pretty fun to work with (we’ve got the Jack Rackham and other useless but funny things in the office, come to see them http://www.flickr.com/photos/51659816@N06/4753862381/).
We value good people who love to build things the right way.
We like to be technically challenged, and we want this for the rest of our team too.
We like talking to customers about how to make the product better.
We are located in Barcelona, Spain.

YOU
You’re a curious person.
You’re a generalist who handle all sorts of technical issues with efficiency.
You like to build things.
You love to do things the right way.

PLUSES
Contributions to the open source community
Excellent communication skills

If you are interested you can go to Masterbranch an apply or send me and email.

Why people don’t know FindBugs?

Posted in Java by plunchete on August 1, 2008

During last weeks I talked with some people who has been developing in Java for a long time, I’m really surprised because they didn’t know FindBugs or other static analyzer, of course all of them think that is really useful and they have started or have plans to use it.

If you don’t know what is FindBugs, FindBugs uses static analisis to look for bugs in Java code. FindBugs is licensed under the Lesser General Public License.

Other Java analyzers:

PMD: Similar to FindBugs.

Checkstyle: Help programmers write Java code that adheres to a coding standard.

Do you use any static analyzer when you program?

Which one?

What is your opion about these tools?

The Java EE 5 Song

Posted in funny, Java by plunchete on July 9, 2008

It’s not new but I discovered this song yesterday, it’s really funny and a little bit geek 😛

The Java EE 5 song and the original post

Lyrics:

Ladies and gentlemen, this is Java EE 5!

One, two, three, four, five
There’s a technology I use day and night
For my application with a web frontend
They told me to use .Net
But I really don´t wanna

So many bugs I fixed last week.
My code is neat and talk is a cheap
I like Glassfish, JSF, persistence API
And as I continue you know they´re gettin´ sweeter

So what can I do I really beg you my Lord
To me codin’ it´s just like a sport
All the bad code from the past, let me dump it
Please set in the trumpet

A little bit of injection in my life
A little bit of persistence by my side
A little bit of NetBeans is all I need
A little bit of EJB’s what I see
A little bit of standards in the sun
A little bit of XML all night long
A little bit web services here I am
A little bit of code makes me real man

This is Java EE 5!

Jump up and down and move your code around
Shake your head to the sound bury bad code under ground
Move one step left and one step right
One to the front and one to the side
Refactor it once and refactor it twice
If it looks like this you’re doin´ it right

A little bit of injection in my life
A little bit of persistence by my side
A little bit of NetBeans is all I need
A little bit of EJB’s is what I see
A little bit of standards in the sun
A little bit of XML all night long
A little bit web services here I am
A little bit of code makes me real man

This is Java EE 5!

Apache OpenJPA 1.1.0 Now Available

Posted in Java, OpenJPA, OpenSource by plunchete on June 26, 2008

Apache OpenJPA 1.1.0 is now available at http://openjpa.apache.org/downloads.html and via Maven. Version 1.1.0 includes a number of new features and bugfixes, including:

  • Improvements in SQL generation for DB2, Oracle and MySQL
  • Support for memory-friendly streaming of BLOB and CLOB data
  • Support for interface-only domain models
  • Collections and Maps can now contain primitives, enums and @Embeddables, as well as @Entity and @MappedSuperclass types
  • Assorted performance and scalability improvements

For more details, see the release notes.

OpenJPA 1.1.0 was released some weeks ago but until yesterday we didn’t have a oficial announce, they has some problems with the artifacts signatures in maven.

The support for memory-friendly streaming of BLOB and CLOB data was my work in the summer of code 🙂

BEAWorld 2007

Posted in About Me, Java by plunchete on October 22, 2007

Last weeks I can’t update the blog, I was very busy, sorry.

 Three weeks ago I went to Barcelona to attend to the BEAWorld, this event had a special section for developers (Develop@BEAWorld). I attended several conferences includign the Rod Johnson’s keynote and the Patrick Linskey’s talk about OpenJPA and Spring.

One of the most interested think, for me, in Rod Johnson’s keynote was OSGi, was very funny when Rod asked who had been problems with classloaders, the 90% of the people rise our hands. Definitely OSGi is part of my TO-DO list.

Patrick Linskey started talking about JPA and a how to use JPA in an application, and he talked about the integration OpenJPA + Spring, Spring has a prety integration with this framework. I had the pleasure to meet Patrick and Eddie O’Neil in person, and we had a great informal talk, thanks a lot Patrick for all 🙂

A lot of people attended to this event, but the thing that caught my attention was that only a few people was from Spain, at least I only saw 4 o 5 people from Spain. Okay this event was for all europe, but I don’t understand why Spanish people don’t value this type of events.

 

 

JPQL and how to delete objects in JPA

Posted in Google Summer of Code, Java, JPA, OpenJPA by plunchete on May 30, 2007

Continuing with the example Relations with JPA now I’ve written another example about querys and deleting objects.

JPA includes a mechanism to write querys called JPQL (Java Persistence Query Language) in this example we’re going to execute some querys and deleting some objects and their relations.

At the end of the last article we had this values in database:

[java] Organization: The Apache Software Foundation
[java] -Streaming LOB support (for OpenJPA) asigned to Ignacio Andreu
[java] -Maven Dependency Visualization asigned to Peter Kolbus
[java] Organization: Mono Project
[java] -Gendarme Tasks asigned to Néstor Salceda

And the next relations between entities:

  • A organization has many projects
  • Each projects is asigned to one organization
  • A project has one student
  • Each student is asigned to one project

Ok, if we remember the last example we used a Query to list all the organizations

Query q = em.createQuery("select o from Organization o");

This is a simple Query, now we’re going to write a Query with a WHERE clause.

JPQL provides us two methods for parameterized queries. First is positional parameter, using an integer prefixed by a question mark and second is named parameter, using an string prefixed by a colon. We can populate the Query calling the setParameter method.

If we are writing a Query with a positional parameter:

Query query = em.createQuery(select o from Organization o " 
                  + "where o.name like ?1);
query.setParameter(1, "The Apache Software Foundation");

And if we are writing a Query with a named parameter:

Query query = em.createQuery(select o from Organization o " 
                  + "where o.name like :name);
query.setParameter("name", "The Apache Software Foundation");

EntityManager povides other method to search an object by his primary key, the method find. In our class Organization the primary key is an int value, auto-increment. In out table the value of the Organization calls “The Apache Software Foundation” is 1. If whe want to load our object by the primary key:

Organization organization = em.find(Organization.class, 1);

Now we are going to delete our organization called “Mono Project” using JPQL. The process is the same that a select.

Query query = em.createQuery(delete from Organization o " 
                  + "where o.name like :name);
query.setParameter("name", "Mono Project");
int deleted = query.executeUpdate();

The Query provides methods to get the result like getResultList() when we want a List or getSingleResult() when we’re sure that we only have one match. Also The Query provides a method to set the maximun number of results setMaxResults(int number)

And now we going to list all the projects

Query q = em.createQuery("select p from Project p");
for (Project project : (List)q.getResultList()) {
	System.out.println("-"
                 + project.getOrganization().getName() 
                 + " - " + project.getName()
                 + " asigned to "
                 + project.getStudent().getName());
}

The output is

[java] -Streaming LOB support (for OpenJPA) asigned to Ignacio Andreu
[java] -Maven Dependency Visualization asigned to Peter Kolbus

Wow, all the projects asigned to “Mono Project” have been deleted too (and all the student asigned to this projects). If we remember we have these relations

@Entity
public class Organization {	

	...

	@OneToMany(cascade = CascadeType.ALL,
			mappedBy = "organization")
	@InverseLogical("organization")
	private ArrayList<Project> projects;

	...
@Entity
public class Project {	

	...

	@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name="student_id")
	@InverseLogical("project")
	private Student student;

	...

It’s seems that our relations works 😀

Another way is using the method remove of the EntityManager, but in this method we must indicate all the Object.

Query q = em.createQuery("select o from Organization o " 
		+ "where o.name like :name");
q.setParameter("name", "Mono Project");

// This is unsecure beacause name not is unique
// we can use the method setMaxResults(1)

Organization o = (Organization)q.getSingleResult();
em.remove(o);

The result is the same.

That is all, folks 😉

Relations with JPA

Posted in Google Summer of Code, Java, JPA, OpenJPA by plunchete on May 15, 2007

Continuing with the JPA Basic Example I’ve written another example with relations between Entities.

In the example we have three entities: Organization, Project, Student with this relations:

  • A organization has many projects
  • Each projects is asigned to one organization
  • A project has one student
  • Each student is asigned to one project

For this relations we need three anotations: @OneToOne, @OneToMany and @ManyToOne.

Now we are going to write our Organization entity:

@Entity
public class Organization {	

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "organization_id")
	private int idOrganization;

	@Basic
	@Column(nullable = false, length = 100)
	private String name;

	@OneToMany(cascade = CascadeType.ALL,
			mappedBy = "organization")
	@InverseLogical("organization")
	private ArrayList<Project> projects;

	public Organization(String name) {
		this.name = name;
	}
        // Getters and Setters
}

We used the anotation @OneToMany because we’re going to persist and Organization and we want to add projects to the organization and when we persist the organization the projects will be persist too.
The property cascade = CascadeType.ALL indicates that when we persist, remove, refresh or merge this entity all the entities held in this field would be persist, remove, delete or update. This property can have other values as:

  • CascadeType.PERSIST – When we persist and entity all the entities held in this field persist too. If you want to persist an entity and the fields dont use it fails.
  • CascadeType.REMOVE – When we delete an entity all the entities held in this field delete too.
  • CascadeType.REFRESH – When we refresh an entity all the entities held in this field refresh too.
  • CascadeType.MERGE – When we merde an entity all the entities held in this flied merged too

The following anotations are equivalent:

@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.REMOVE,
             CascadeType.REFRESH, CascadeType.MERGE})
private ArrayList<Project> projects;

And

@OneToMany(cascade = CascadeType.ALL)
private ArrayList<Project> projects;

The propery mappedBy = “organization” indicates who’s the name of the organization entity in the project object.

The annotation @InverseLogical(“organization”) indicates that a field should be maintained bidirectionally. When we use this annotation OpenJPA managed the relation and updates the field oganization in the project entity.

Now we are going to write our Project entity:

@Entity
public class Project {	

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "project_id")
	private int idProject;

	@Basic
	@Column(nullable = false)
	private String name;

	@ManyToOne
	@JoinColumn(name="organization_id", nullable = false)
	private Organization organization;

	@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinColumn(name="student_id")
	@InverseLogical("project")
	private Student student;

	public Project(String name) {
		this.name = name;
	}
        // Getters and Setters
}

Now we use the annotation @ManyToOne to indicate the relation.
The annotation @JoinColumn(name=”organization_id”, nullable = false) indicates that the field can’t be null and the name on the table.
And finally we use the annotation @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) to manage our one-to-one relation between the project entity and the student.

Now we are going to write our Student entity:

@Entity
public class Student {

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int studentId;

	@Basic
	@Column(nullable = false)
	private String name;

	@OneToOne
	@JoinColumn(name="project_id")
	private Project project;

	public Student(String name) {
		this.name = name;
	}
	public String getName() {
		return name;
	}
        // Getters and Setters
}

And our persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    version="1.0">
    <persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
        <class>example.Organization</class>
		<class>example.Project</class>
		<class>example.Student</class>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings"
                value="buildSchema"/>
            <property name="openjpa.ConnectionURL"
                value="jdbc:mysql://localhost/OpenJPARelations"/>
            <property name="openjpa.ConnectionDriverName"
                value="com.mysql.jdbc.Driver"/>
            <property name="openjpa.ConnectionUserName"
                value="user"/>
            <property name="openjpa.ConnectionPassword"
                value="password"/>
	    <property name="openjpa.InverseManager"
		value="true"/>
	    <property name="openjpa.Log"
		value="File=../RelationsExample.log,
                DefaultLevel=WARN,SQL=TRACE"/>
        </properties>
    </persistence-unit>
</persistence>

The property openjpa.InverseManager is necesary to manage inverse relations.
Finally the writer and the reader:

public void insertValues() {
	EntityManagerFactory factory = Persistence.
		createEntityManagerFactory("example", System.getProperties());

	EntityManager em = factory.createEntityManager();
	em.getTransaction().begin();
	Organization organization =
		new Organization(" The Apache Software Foundation");

	Project project =
		new Project("Streaming LOB support (for OpenJPA)");
	Student student = new Student("Ignacio Andreu");
	project.setStudent(student);
	organization.addProject(project);

	project = new Project("Maven Dependency Visualization");
	student = new Student("Peter Kolbus");
	project.setStudent(student);
	organization.addProject(project);

	em.persist(organization);

	organization = new Organization(" Mono Project");
	project = new Project("Gendarme Tasks");
	student = new Student("Néstor Salceda");
	project.setStudent(student);
	organization.addProject(project);

	em.persist(organization);

	em.getTransaction().commit();
	em.close();
	factory.close();
}

public void readValues() {
	EntityManagerFactory factory = Persistence.
	createEntityManagerFactory("example", System.getProperties());

	EntityManager em = factory.createEntityManager();

	Query q = em.createQuery("select o from Organization o");

	for (Organization organization : (List)q.getResultList()) {
		System.out.println("Organization: "
			+ organization.getName());
		if (organization.getProjects() != null &&
			organization.getProjects().size() > 0) {
			for (Project p : organization.getProjects()) {
				System.out.println("-"
					+ p.getName()
                                        + " asigned to "
					+ p.getStudent().getName());
			}
		} else {
			System.out.println("No proyects yet");
		}
	}
	em.close();
	factory.close();
}

The output is:

[java] Organization: The Apache Software Foundation
[java] -Streaming LOB support (for OpenJPA) asigned to Ignacio Andreu
[java] -Maven Dependency Visualization asigned to Peter Kolbus
[java] Organization: Mono Project
[java] -Gendarme Tasks asigned to Néstor Salceda

And this is all for the moment!