RSS

EJB3 Entities Life Cycle Events

14 Mar

2 approaches to implement Entities Life Cycle Events:

A. Write Life Cycle events in external file.

EntityListenerLogger.java

package com.xavier.ejb3.entitylistener;

import javax.persistence.PostLoad;

import javax.persistence.PostPersist;

import javax.persistence.PrePersist;

import javax.persistence.PreUpdate;

import javax.persistence.PostUpdate;

import javax.persistence.PreRemove;

import javax.persistence.PostRemove;

 

public class EntityListenerLogger {

@PostLoad

public void postLoad(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( ) + “} @PostLoad event occurs”);

}

@PrePersist

public void PreInsert(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( )+ “} @PrePersist event occurs”);

}

@PostPersist

public void postInsert(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( )+ “} @PostPersist event occurs”);

}

@PreUpdate

public void PreUpdate(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( )+ “} @PreUpdate event occurs”);

}

@PostUpdate

public void PostUpdate(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( )+ “} @PostUpdate event occurs”);

}

@PreRemove

public void PreRemove(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( )+ “} @PreRemove event occurs”);

}

@PostRemove

public void PostRemove(Object entity) {

System.out.println(“Entity{“ + entity.getClass().getName( )+ “} @PostRemove event occurs”);

}

}

 

User.java

package com.xavier.ejb3.entitybean;

import java.io.Serializable;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.EntityListeners;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;

import com.xavier.ejb3.entitylistener.EntityListenerLogger;

 

@SuppressWarnings(“serial”)

@Entity

@EntityListeners(EntityListenerLogger.class)

@Table(name=“user”)

public class User implements Serializable{

private int id;

private String name;

private int age;

private String phone;

private String address;

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Column(nullable=false)

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Column(nullable=true)

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Column(nullable=true)

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

@Column(nullable=true)

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

}

 

B. Write Life Cycle events in the Entities files.

User.java

package com.xavier.ejb3.entitybean;

import java.io.Serializable;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.EntityListeners;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.PostLoad;

import javax.persistence.PostPersist;

import javax.persistence.PostRemove;

import javax.persistence.PostUpdate;

import javax.persistence.PrePersist;

import javax.persistence.PreRemove;

import javax.persistence.PreUpdate;

import javax.persistence.Table;

import com.xavier.ejb3.entitylistener.EntityListenerLogger;

 

@SuppressWarnings(“serial”)

@Entity

@EntityListeners(EntityListenerLogger.class)

@Table(name=“user”)

public class User implements Serializable{

private int id;

private String name;

private int age;

private String phone;

private String address;

@Id

@GeneratedValue(strategy=GenerationType.IDENTITY)

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Column(nullable=false)

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Column(nullable=true)

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@Column(nullable=true)

public String getPhone() {

return phone;

}

public void setPhone(String phone) {

this.phone = phone;

}

@Column(nullable=true)

public String getAddress() {

return address;

}

public void setAddress(String address) {

this.address = address;

}

@PostLoad

public void postLoad( ) {

System.out.println(“Entity{“ + this.getClass().getName( ) + “} @PostLoad event occurs”);

}

@PrePersist

public void PreInsert( ) {

System.out.println(“Entity{“ + this.getClass().getName( )+ “} @PrePersist event occurs”);

}

@PostPersist

public void postInsert( ) {

System.out.println(“Entity{“ + this.getClass().getName( )+ “} @PostPersist event occurs”);

}

@PreUpdate

public void PreUpdate( ) {

System.out.println(“Entity{“ + this.getClass().getName( )+ “} @PreUpdate event occurs”);

}

@PostUpdate

public void PostUpdate( ) {

System.out.println(“Entity{“ + this.getClass().getName( )+ “} @PostUpdate event occurs”);

}

@PreRemove

public void PreRemove( ) {

System.out.println(“Entity{“ + this.getClass().getName( )+ “} @PreRemove event occurs”);

}

@PostRemove

public void PostRemove( ) {

System.out.println(“Entity{“ + this.getClass().getName( )+ “} @PostRemove event occurs”);

}   

}

Advertisements
 
Leave a comment

Posted by on 03/14/2012 in EJB

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: