Close

JPA - @EntityListeners Examples

Code Snippets JPA JAVA EE 

import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@EntityListeners(MyEntityListener.class)
public class MyEntity {
@Id
@GeneratedValue
private int id;
private String msg;

public MyEntity() { }

public MyEntity(String msg) {
this.msg = msg;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

@Override
public String toString() {
return "MyEntity{" +
"id=" + id +
", msg='" + msg + '\'' +
'}';
}
}
Original Post




import javax.persistence.*;
import java.sql.Timestamp;

@Entity
@EntityListeners(AuditableListener.class)
public class Article implements Auditable {
@Id
@GeneratedValue
private int id;
private String content;
@ManyToOne(cascade = CascadeType.ALL)
private Publisher publisher;
private Timestamp dateCreated;
private Timestamp lastUpdated;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Publisher getPublisher() {
return publisher;
}

public void setPublisher(Publisher publisher) {
this.publisher = publisher;
}

@Override
public Timestamp getDateCreated() {
return dateCreated;
}

@Override
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}

@Override
public Timestamp getLastUpdated() {
return lastUpdated;
}

@Override
public void setLastUpdated(Timestamp lastUpdated) {
this.lastUpdated = lastUpdated;
}


@Override
public String toString() {
return "Article{" +
"id=" + id +
", content='" + content + '\'' +
", dateCreated=" + dateCreated +
", lastUpdated=" + lastUpdated +
'}';
}
}
Original Post




import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import java.sql.Timestamp;

@Entity
@EntityListeners(AuditableListener.class)
public class Publisher implements Auditable {
@Id
@GeneratedValue
private int id;
private String name;
private Timestamp dateCreated;
private Timestamp lastUpdated;

public Publisher() {
}

public Publisher(String name) {
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public Timestamp getDateCreated() {
return dateCreated;
}

@Override
public void setDateCreated(Timestamp dateCreated) {
this.dateCreated = dateCreated;
}

@Override
public Timestamp getLastUpdated() {
return lastUpdated;
}

@Override
public void setLastUpdated(Timestamp lastUpdated) {
this.lastUpdated = lastUpdated;
}

@Override
public String toString() {
return "Publisher{" +
"id=" + id +
", name='" + name + '\'' +
", dateCreated=" + dateCreated +
", lastUpdated=" + lastUpdated +
'}';
}
}
Original Post




See Also