March 24, 2014

Create join table with JPA annotations

Problem:

Need to create a join table in database using JPA annotations so the DATABASE design as follows:
 and it should be like this in JPA level:

Solution:

Default entities implementation:
@Entity
@Table(name="USER")
public class User implements Serializable {
//...

@ManyToOne
@JoinTable(name="USER_GROUP")
Group group;

@Entity
@Table(name="GROUP")
public class Group implements Serializable {
//...

@OneToMany(mappedBy="group")
Set<User> users;

Changes on User entity as follows:

@Entity
@Table(name="USER")
public class User implements Serializable {
//...
@ManyToOne @JoinTable(name="USER_GROUP",     joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"),     inverseJoinColumns = @JoinColumn(name = "group_id", referencedColumnName = "group_id")) Group group;

Primefaces + EJB + JPA Template Project

The best project template for Primefaces + EJB + JPA and Maven I worked with.



March 23, 2014

Add Microsoft SQL JDBC driver to Maven


1. Download the JDBC driver for Microsoft SQL Server
Visit the MSDN site for SQL Server and download the latest version of the JDBC driver for your operating system.
2. Unzip the package
3. Open a command prompt and switch into the expanded directory where the jar file is located.
4. Execute the following command. Be sure to modify the jar file name and version as necessary:

mvn install:install-file -Dfile=sqljdbc4.jar -Dpackaging=jar -DgroupId=com.microsoft.sqlserver -DartifactId=sqljdbc4 -Dversion=4.0

5. Modify your POM
Include the new dependency by modifying your project’s pom.xml. Add the following dependency:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>sqljdbc4</artifactId>
    <version>4.0</version>
</dependency>

March 2, 2014

replace special characters with HTML entities ONLINE

Great website for replace special characters with HTML entities ONLINE

http://www.htmlescape.net/htmlescape_tool.html

Arabic (UTF-8) Characters encoding Issue in JSF applications

Problem:

Some times when you develop a web application using JSF you face issue posting the Arabic characters to application server with different encoding.

Solution:
Create a filter for POST requests that add character encoding to the post request as follow:

@WebFilter("/*")
public class CharacterEncodingFilter implements Filter {

  @Override
  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException   {
     request.setCharacterEncoding("UTF-8");
     chain.doFilter(request, response);
  }

  @override
  public void destroy() {
  }

  @Override
  public void init(FilterConfig arg0) throws ServletException {
  }
}

And for the GET requests you need to update the application server's deployment descriptor.
In glass fish as example, create glassfish-web.xml file under WEB-INF folder and add the following line:


<parameter-encoding default-charset="UTF-8">