Friday, January 22, 2010

Horrors of the past (IE)

Someone I trust recently told me that the only reason she's not switching to ubuntu linux is that it doesn't have internet explorer on it on which she can test her web applications - with IE being (surprisingly) the most widely-used internet browser in the world, all web apps have as starting requirement "MUST work on internet explorer and other browsers", so if it works on Mozilla and Opera and not on IE, your web application is close to worthless. So here's a little solution to have both the liberty of Ubuntu and the dying shade of IE integrated with it - kind of like a benign form of cancer if you will.

1. Go to Administration/synaptic package manager and install the playonlinux package (which will also install wine as a prerequisite)
2. go to Games/Playonlinux
3. click on the install button in the upper menu
4. search for "Internet explorer" in the search bar
5. install it and despair

NOTE: for the sake of easy accessing, don't forget to tick the option at the end of the installation which will add an icon of IE to your main menu

P.S. When searching for Internet explorer you will find that Playonlinux supports a huge variety of windows software from development tools all the way to the pc games. Windows has had great longevity but its days are numbered to say the least


Monday, January 18, 2010

Polymorphic queries in JPQL ?!

You may be surprised to find out that JPA supports polymorphism, and that JPQL
queries are polymorphic. This means a JPQL query to retrieve a parent entity in an
entity hierarchy is not just limited to the entity, but retrieves all subclasses as well.
For example, in ActionBazaar any query to retrieve User entities will retrieve its
subclasses, such as Seller, Bidder, and Admin.
Suppose we have a query like this:
   SELECT u
   FROM User u
   WHERE u.firstName LIKE :firstName
The query will retrieve all instances of Seller, Bidder, Admin, and so forth that
match this query condition. How do you handle a polymorphic query in your client
code? Consider the following:
       query = em.createNamedQuery("findUserByName");
       query.setParameter("firstName", firstName);
       List<User> users = query.getResultList();
       Iterator i = users.iterator();
       while (i.hasNext()) {
             User user = (User) i.next();
             System.out.print("User:"+emp.getUserId());
             if (user instanceof Seller) {
                     Seller seller = (Seller) user;
                     System.out.println("Seller:" +
                            seller.getCommissionRate());
             }
             else if (user instanceof Bidder) {
                     Bidder bidder = (Bidder) bidder;
                     System.out.println("Bidder:" +
                            bidder.getDiscountRate());
             }
       }
This code snippet uses the instanceof keyword to test user. Some Java gurus
recommend you avoid using instanceof, but we use it here as a last resort.
You have to ensure that your operations are just as polymorphic as your que-
ries! In our example, you can easily convert the operations to be polymorphic by
adding a getRate method in all entities. The getRate method will return the
commissionRate for the Seller entity, whereas it will return the discount-
Rate for the Bidder entity. The resulting code should look like this:
Iterator i = users.iterator();
while (i.hasNext()) {
  User user = (User)i.next();
  System.out.print("User:" + emp.getUserId());
  System.out.println(user.getRate());

Source: Enterprise Java Beans 3 in Action,chapter 10, Manning publications, 2007
}


Sunday, January 17, 2010

Ubuntu wrong resolution fixer

great fix:

http://ubuntuforums.org/showthread.php?p=8302145

Friday, January 15, 2010

J2EE EAP reminder

After properly configuring jboss with respect to the last 2 posts, don't forget to add jbossall-client.jar to the EJB module's classpath

configure mysql data source in jboss [part 2]

This and previous post work hand in hand to make jpa work in mysql

http://community.jboss.org/thread/130668

how to setup a mysql data source in Jboss

Download the driver

 

  • First, http://www.mysql.com/products/connector/j/ appropriate for your edition of mySQL. 

  • Next, untar/unzip it and extract the jar file.

  • Copy the jar file into $JBOSS_HOME/server/xxx/lib, where xxx is your config name (such as "default") NOTE: For JBoss 4.0.2, use the jar file mysql-connector-java-3.1.8-bin.jar, not mysql-connector-java-3.1.8-bin-g.jar.

  • Copy the $JBOSS_HOME/docs/examples/jca/mysql-ds.xml file to $JBOSS_HOME/server/xxx/deploy

 

Configure the datasource

 

  • Edit the mysql-ds.xml file.

  • Replace <jndi-name>MySqlDS</jndi-name> with your datasource name.  If you choose to make mySQL your default database (DefaultDS), then call this DefaultDS and be sure to delete the example $JBOSS_HOME/server/all/deploy/hsqldb-ds.xml which is also configured to be DefaultDS.

  • Replace <connection-url>jdbc:mysql://mysql-hostname:3306/jbossdb</connection-url> with your connection string.  Generally you just need to replace mysql-hostname with your host.  Be sure that your user has permission to connect to that hostname.

  • Set the user-name and hostname elements to your database username and hostname

 

Advanced options for the MySQL Driver can be set with <connection-property name="property">value</connection-property>.

Refer to MySQL Connector/J Manual Chapter 2 for more Information.



Source: http://community.jboss.org/wiki/SetUpAMysqlDatasource

simple EJB3 JPA mappings+code example

Consider the following DB schema:


Now let's create the following entities: Movie, User and Screening. Figure out the mappings from the upper diagram


package com.cinema.entities;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.*;

@Entity
@Table(name="Movie")
public class Movie implements Serializable{

@Id
@Column(name="ID")
private int id;

@Column(name="title")
private String title;

@Column(name="genre")
private String genre;

@Column(name="duration")
private int duration;

@Column(name="director")
private String director;

@OneToMany(mappedBy="movie")
private Set screenings;

public Movie() {}



public Movie(String title, String genre, int duration, String director) {
super();
this.title = title;
this.genre = genre;
this.duration = duration;
this.director = director;
}



public Set getScreenings() {
return screenings;
}



public void setScreenings(Set screenings) {
this.screenings = screenings;
}



public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getGenre() {
return genre;
}

public void setGenre(String genre) {
this.genre = genre;
}

public int getDuration() {
return duration;
}

public void setDuration(int duration) {
this.duration = duration;
}

public String getDirector() {
return director;
}

public void setDirector(String director) {
this.director = director;
}



}




package com.cinema.entities;

import javax.persistence.*;

import java.io.Serializable;
import java.util.*;

@Entity
@Table(name="Screening")
public class Screening implements Serializable{

@Id
@Column(name="ID")
private int id;

@ManyToOne
@JoinColumn(name="Movie_ID", referencedColumnName="ID")
private Movie movie;

@Temporal(TemporalType.TIMESTAMP)
private Date time;

@ManyToMany(mappedBy="screenings")
private Set users;

public Screening(){}

public Screening(Movie movie, Date time) {
super();
this.movie = movie;
this.time = time;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public Movie getMovie() {
return movie;
}

public void setMovie(Movie movie) {
this.movie = movie;
}

public Date getTime() {
return time;
}

public void setTime(Date time) {
this.time = time;
}

public Set getUsers() {
return users;
}

public void setUsers(Set users) {
this.users = users;
}



}



package com.cinema.entities;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.*;



@Entity
@Table(name="User")
public class User implements Serializable{


@Id
@Column(name="username")
private String username;

@Column(name="password")
private String password;

@Column(name="role")
private String role;


@ManyToMany
@JoinTable(name="Bookings",
joinColumns=
@JoinColumn(name="username", referencedColumnName="username"),
inverseJoinColumns=
@JoinColumn(name="Screening_ID", referencedColumnName="ID"))
private Set screenings;


public String getUsername() {
return username;
}

public User() {}



public User(String username, String password, String role) {
super();
this.username = username;
this.password = password;
this.role = role;
}

public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}



public Set getScreenings() {
return screenings;
}

public void setScreenings(Set screenings) {
this.screenings = screenings;
}




}


There you go.... now edit persistence.xml and you're pretty much done:



xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">


org.hibernate.ejb.HibernatePersistence












Peace, out