Switch to full style
Java persistent API
Post a reply

@EntityListeners in your entity class

Sat Mar 20, 2010 11:02 am

@EntityListeners is used for to do action for specific actions on entity .


Listener class :
Code:

import javax
.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;

import codemiles.test.jpa.Comment;

public class ListenerExample {
    
     
@PostLoad
     
@PostPersist
     
@PostUpdate
      public void actionFunction
(Comment comment) { 
         
         
//  When loading the object . 
         System.out.println("Post load action ");
          
         
// When persisting the object .
         System.out.println("Post persist action ");
         
         
//When  updating the values of the object 
         System.out.println("Post update action ");
         
         
      
}

}
 



Entity class :
Code:

import javax
.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import codemiles.test.jpa.listener.ListenerExample;

@
EntityListeners({ListenerExample.class})
@
Entity
@Table(name="COMMENT")
@
SequenceGenerator(sequenceName="COMMENT_SEQ",name="COMMENT_SEQ_GEN")
public class Comment {
    private long id;
    private String content;
    private Topic topic;
    
    public void setId
(long id) {
        this.id = id;
    }
    @Id
    
@GeneratedValue(generator="COMMENT_SEQ_GEN",strategy=GenerationType.SEQUENCE)
    public long getId() {
        return id;
    }
    
    
@Column(name="CONTENT")
    public void setContent(String content) {
        this.content = content;
    }
    public String getContent() {
        return content;
    }
    
    
@ManyToOne(fetch =  FetchType.LAZY)
    @JoinColumn(name = "TOPIC_ID", nullable = false)
    public void setTopic(Topic topic) {
        this.topic = topic;
    }
    public Topic getTopic() {
        return topic;
    }
    

}
 




Post a reply
  Related Posts  to : @EntityListeners in your entity class
 JPA entity class example     -  
 create a named query within entity class     -  
 Get all objects for an entity     -  
 Find entity by id     -  
 cmp entity beans     -  
 JPA entity with table generator     -  
 secondary table per entity     -  
 @Transient annotation in JPA entity     -  
 @Embeddable entity and @AttributeOverrides     -  
 Define class helper class to check the method existance     -  

Topic Tags

Java JPA