Total members 11889 |It is currently Fri Mar 29, 2024 4:56 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





@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;
    }
    

}
 




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : @EntityListeners in your entity class
 JPA entity class example     -  
 create a named query within entity class     -  
 cmp entity beans     -  
 Get all objects for an entity     -  
 Find entity by id     -  
 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
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com