Friday, January 18, 2008

XML<->Java<->DB

JPA and JAXB both provide a way to annotate your java classes to tell how they should be serialized. JPA annotations defines how your class is mapped to the database, and JAXB annotations define how your class is serialized to XML. But is it possible to have both annotations on the same classe, to be able to convert your data from the database, to java object, then to XML or any combination of the 3?

Well yes it is, and it works pretty well :-)
Here is what your code will look like:

@XmlRootElement
@XmlType(propOrder={

"brand",
"personId"
})
@Entity
@Table(name = "car")

@NamedQueries({@NamedQuery(name = "Car.findByCarId", query = "SELECT c FROM Car c WHERE c.carId = :carId")})

public class Car implements Serializable {
private static final long serialVersionUID = 1L;

@XmlTransient
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)

@Column(name = "car_id", nullable = false)

private Integer carId;
@Column(name = "brand")

private String brand;
@JoinColumn(name = "person_id", referencedColumnName = "person_id")

@ManyToOne
private Person personId;

public Car() {

}

public Car(Integer carId) {
this.carId = carId;

}

public Integer getCarId() {
return carId;

}

public void setCarId(Integer carId) {

this.carId = carId;
}

public String getBrand() {

return brand;
}

public void setBrand(String brand) {

this.brand = brand;
}

@XmlElement(name = "owner")

public Person getPersonId() {
return personId;

}

public void setPersonId(Person personId) {

this.personId = personId;
}
}

There are several things to notice about that:

1)The JPA annotations decorate the attributes where the JAXB annotations decorate the bean properties (...the getters if you want...)

2)There is a catch when you have bidirectional relationships. XML doesn't allow bidirectional relationships (You cannot have element A in element B and element B in element A :-)). So if you convert from XML to Java, then try to persist the object in your DB, you will see that something goes wrong. So just make sure that you restore those missing relationships in your java object (eg, if person.car!=null, then car.owner shouldn't be null) and everything will work fine.

2 things to remember when using JPA

Java persistence API offers a great Object Relational Mapping framework to develop java application. In combination with a wizzard that auto-generates the entity classes from your databse, it makes an easy, clean, and quick way to store your data in a database. Unfortunately, some or the errors you can get with JPA are not always easy to debug. So here are the two lessons I learned while spending hours trying to debug my code:

1)If you have a bidirectionnal relationthip (annotated with a @mapped_by), it is your responsability to make sure that the two fields that reference each-other contain what they should. I say it again, it is YOUR responsability. And if it doesn't, and you try to persist your object, you will probably get a violation constraint in you database, without JPA telling you that you messed up.

2)If your table has a field table_id, which value is auto-generated by your database, make sure to annotate the mapping java field with @Id and @GeneratedValue(strategy="IDENTITY"). If you don't, everything will work fine when you just persist your table, but things won't work when persisting the table in cascade.

Wednesday, January 9, 2008

The FSF is looking for new members

The free software foundation is running a campaign to recruite new associate members.
The free software foundation is dedicated to promote user's right in the IT world. Some of the their most famous activities include the GPL license, the GNU project, freebios or gnash. They also have several campaign to eliminate DRMs, promote ODF or ogg and in general, point out freedom issues as they appear.

So if you want to participate or help encouraging all those good stuffs, consider joining in :-)


Thursday, December 20, 2007

monodevelop 1.0 beta 3 on mac

The team at Novell just released monodevelop 1.0 beta 3 . Beside the usual bug fixes, it seems that some effort is being put to improve the support on mac. It can now run on a mac without X11 using gtk-quartz. I hope that the monodevelop team will put as much effort at stabilizing the project as they do at adding new features. Having a lot of features advertises well, but stability and reliability of an IDE are more than mandatory for it to be used in a professional environment.

Monday, December 17, 2007

Dell shipping new ubuntu

It seems that dell updated its product line, and now ships ubuntu 7.10 (gutsy gibbon) with its inspiron 1420N. As the proud and happy owner of one of those laptops, I would only encourage the linux enthusiasts to vote with their wallet and purchase one of dell ubuntu computers. It's cheaper than with windows, has supported hardware and comes with ubuntu preinstalled :-). Needless to say that it's the best way to show support for your favorite distribution.

Wednesday, December 5, 2007

Experimenting virtual box

So I decided to give a try to Virtual Box, and install ubuntu hardy-heron in it to see how it looks. The experience is really positive so far. Virtual box makes it just so easy to experiment new operating systems. The interface is verry intuitive , so you just have to follow the instruction on screen and it does its job.

Saturday, December 1, 2007

Build monodevelop within monodevelop


There is a monodevelop solution in the monodevelop svn tree. It allows you to build monodevelop from within monodevelop, pretty cool. Look at the screenshot, it shows opening the replace dialog from the monodevelop GUI editor called stetic.
Monodevelop seems to be shaping up verry nicely. It will be cool to finally have an IDE that allows you to create gnome apps without having to read tons of documentation.