Sat Mar 20, 2010 11:02 am
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 ");
}
}
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;
}
}
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
Powered by phpBB © phpBB Group.