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

New google maps service: Street View

Posted in General by plunchete on May 30, 2007

Now in OJObuscador, a Spanish web I read that Google launchs a new service called “Google Maps Street View”.

This services offer a street-view, feeling that you’re walking down the street. This service is avaliable in San Francisco, New York, Las Vegas, Miami, and Denver.

You can see the announcement and a video here: http://google-latlong.blogspot.com/2007/05/introducing-street-view.html

And you can view this service in http://maps.google.com/ and clicking on the button “Street View”.

If you’re browser language isn’t english you, probably need to specific english language in the URL, something like this: http://maps.google.com/?hl=en at least Spanish need it.

You on the Golden Gate

Is amazing!!

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 :D

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 ;)

My last day at work

Posted in About Me by plunchete on May 27, 2007

Friday was the last day in my company (TB-Solutions) I have been in Tb-Solutions for eight months, TB-Solutions is a great company from Zaragoza (my city).

Eight months ago when I decided to work in some place while I finalized my University studies, I have been for two weeks looking for a company that was really interesting. I was not searching about jobs offers, I was searching about projects and tecnologies. Finally I found TB-Solutions and I decided to send my CV to the department of practices and they telephoned to me to offer a work as developer.

In TB-Solutions I’ve learned about real projects, real timelines and work in group. Also I’ve known fantastic people. But now is the time to finish my studies.

Thanks to TB-Solutions that it has given to me the oportunity to work with them.

P.D. If you’re a worker of TB-Solutions please post a coment if you aren’t post a coment too :)

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!

Taking important decisions

Posted in About Me by plunchete on May 10, 2007

This week I have taked one of the most important decisions in my life, I decided to leave my job.

The reason is that I want to finish my university studies this year, for me this is the most important.

I’m very happy with my work in TB-Solutions and with my workmates, the proyects and the tecnologies are very interesting (Digital certificates, encryption, XML Encryption …)

On the other hand I have been studying my career for 5 year and if I delay my studies could be that i never finish, I prefer to finish my carrer as soon as posible. In addition I would like go out when I finish, I think that the experience to live in other countries and have time to learn about other tecnologies ( I’ve a big TO-DO list).

Definitely my priority is my studies, my career. Perhaps I’m committing an error … but It will be my error, I need to do the things that I feeld that are correct!

Using OpenJPA

Posted in OpenJPA by plunchete on May 6, 2007

I’ve implemented a simple example using OpenJPA as persistence system. This is my first experience using OpenJPA and I like it, I think that OpenJPA is really a great API. I hope that this post serves as aid to other people who start with OpenJPA.

I started using the “hellojpa” example and the OpenJPA User’s Guide

Writting a simple persistence class:

@Entity
public class Person {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_person")
	private long idPerson;

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

	@Basic
	@Column(name = "e_mail")
	private String mail;

	public Person() {}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getIdPerson() {
		return idPerson;
	}

	public void setIdPerson(long idPerson) {
		this.idPerson = idPerson;
	}
}

Annotations are very intuitive and powerful.

Writting the persistence.xml I used MySQL as database.

<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.Person</class>
        <properties>
            <property name="openjpa.jdbc.SynchronizeMappings"
                value="buildSchema"/>
            <property name="openjpa.ConnectionURL"
                value="jdbc:mysql://localhost/openjpa"/>
            <property name="openjpa.ConnectionDriverName"
                value="com.mysql.jdbc.Driver"/>
            <property name="openjpa.ConnectionUserName"
                value="user"/>
            <property name="openjpa.ConnectionPassword"
                value="password"/>
        </properties>
    </persistence-unit>
</persistence>

The property “openjpa.jdbc.SynchronizeMappings” is amazing, you don’t need any sql definitions of the tables or the rows, the OpenJPA creates the tables with your object information and if you add a variable it makes an alter automatically.

And finally contructing the EntityManage and saving the object:

        EntityManagerFactory factory = Persistence.
                createEntityManagerFactory("example", System.getProperties());

        EntityManager em = factory.createEntityManager();

        em.getTransaction().begin();

        Person person = new Person();
        person.setName("Ignacio Andreu");
        person.setMail("plunchete AT gmail DOT com");
        em.persist(person);

        em.getTransaction().commit();

        em.close();

And this is all :)

Follow

Get every new post delivered to your Inbox.