30 May 2014

MappedSuperclass annotation in JPA. Auditing Solution

  • Advance
IIf you have to map JPA entities with auditory fields, maybe use the @MappedSuperclass annotation, would be a good solution to implement less code and organize it better.

@MappedSuperclass is a annotation of java persistance. You can use to define mappings for an abstract or non-persistent superclass. Mapped superclasses cannot define a table, but can define mapping for its attributes and other common persistence behavior.

  • Explanation
If many of our entities have audit fields we define a class with these fields:

@MappedSuperclass
public abstract class AuditDomain implements Serializable {

 /**
  * serialVersionUID.
  */
 private static final long serialVersionUID = 6570818726931423004L;

 /**
  * Logical Deletion.
  */
 @Column(name = "LOGICAL_DEL")
 private Boolean logicalDeletion;

 /**
  * Date insert.
  */
 @Column(name = "DATE_INSERT")
 @Temporal(TemporalType.TIMESTAMP)
 private Date dateInsert;
 
 /**
  * Date update.
  */
 @Column(name = "DATE_UPDATE)
 @Temporal(TemporalType.TIMESTAMP)
 private Date dateUpdate;
 
 /**
  * Role insert.
  */
 @Column(name = "ROLE_INSERT")
 private String roleInsert;
 
 /**
  * Role update.
  */
 @Column(name = "ROLE_UPDATE")
 private String roleUpdate;
 
        /**
  * User insert.
  */
 @Column(name = "USER_INSERT")
 private String userInsert;
 
 /**
  * User update.
  */
 @Column(name = "USER_UPDATE")
 private String userUpdate;
 

 //getters and setters

}

To every entity with audit fields, we only have to make them to extend from our class AuditDomain, and all these fields will be added by inheritance. We saved encrypt all audit fields in all institutions. Now our code will be more readable and robust.

public class UserOnLine extends AuditDomain {
 
 private static final long serialVersionUID = 4892071387935963546L;
 
 @Id
 @Column(name = "USER_ON_LINE_ID")
 private Long id;
 
 @Column(name = "NAME")
 private String name;

        @Column(name = "EMAIL")
 private String email;

        @Column(name = "TELEPHONE")
 private String telephone;

        //getters and setters

8 May 2014

Websockets: How to use it in order to get real time data in a web browser


  • Advance
The main idea with this project is just learning how to achieve the effect of real time data appearing in the web browser, without need of refreshing the web browser.

How does it achieved? Using websockets technology, spring framework and rabbitmq broker message with stomp support and with a bit of html5 and Jquery.

  • Explanation


The example project is based in knowing where the buses of Dublin city are in every moment. Luckily Dublin city has every bus connected to internet, so every customer can connect to internet during the trip, and also every bus is connected to a system to send their position to the system.

That system is available using a REST api provided by Dublin city, so, where is the trick? The trick is you need to ask periodically to the system about new data, enqueue the data to the rabbitmq-stomp broker message and with a bit of jquery code the effect is done.

  • Conclusions
I think the code is self-explainable, so you can download it and get the conclusions for your own. Enjoy!

  • About the author:
This post is a contribution made by Alonso Isidoro Román, a Software Engineer from Badajoz (Spain). You can follow or contact with Alonso Isidoro Román through these social networks: