File: Message.java
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Message {
@Id
private int id;
private String message;
@Temporal(TemporalType.DATE)
private Date postingDate;
@ManyToOne Conversation conversation;
public int getId() {
return id;
}
public void setId(int empNo) {
this.id = empNo;
}
public String getMessage() {
return message;
}
public void setMessage(String name) {
this.message = name;
}
public Date getPostingDate() {
return postingDate;
}
public void setPostingDate(Date postingDate) {
this.postingDate = postingDate;
}
public Conversation getConversation() {
return conversation;
}
public void setConversation(Conversation conversation) {
this.conversation = conversation;
}
public String toString() {
return "Message " + getId() + ": " + getMessage() +
" posted: " + getPostingDate();
}
}
File: Conversation.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
@Entity
@NamedQueries({
@NamedQuery(name="findActiveConversations",
query="SELECT c " +
"FROM Conversation c " +
"WHERE c.status = 'ACTIVE'"),
@NamedQuery(name="findLastMessageDate",
query="SELECT MAX(m.postingDate) " +
"FROM Conversation c JOIN c.messages m " +
"WHERE c = :conversation")
})
public class Conversation {
public final static String ACTIVE = "ACTIVE";
public final static String INACTIVE = "INACTIVE";
@Id
private int id;
private String status;
@OneToMany(mappedBy="conversation")
private Collection messages = new ArrayList();
public int getId() {
return id;
}
public void setId(int empNo) {
this.id = empNo;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public Collection getMessages() {
return messages;
}
public void addMessage(Message m) {
if (!getMessages().contains(m)) {
getMessages().add(m);
if (m.getConversation() != null) {
m.getConversation().getMessages().remove(m);
}
m.setConversation(this);
}
}
public String toString() {
return "Conversation " + getId() + ": status: " + getStatus();
}
}
File: Main.java
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.FlushModeType;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
List active = (List) em.createNamedQuery("findActiveConversations")
.getResultList();
Query maxAge = em.createNamedQuery("findLastMessageDate");
maxAge.setFlushMode(FlushModeType.COMMIT);
for (Conversation c : active) {
maxAge.setParameter("conversation", c);
Date lastMessageDate = (Date) maxAge.getSingleResult();
if (lastMessageDate.before(new Date())) {
c.setStatus("INACTIVE");
}
}
em.getTransaction().commit();
em.close();
emf.close();
Helper.checkData();
}
}
File: Helper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Helper {
public static void checkData() throws Exception {
Class.forName("org.hsqldb.jdbcDriver");
Connection conn = DriverManager.getConnection("jdbc:hsqldb:data/tutorial", "sa", "");
Statement st = conn.createStatement();
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (mrs.next()) {
String tableName = mrs.getString(3);
System.out.println("\n\n\n\nTable Name: "+ tableName);
ResultSet rs = st.executeQuery("select * from " + tableName);
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) {
System.out.println(" Row:");
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.println(" Column Name: "+ metadata.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeName(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"\n");
}
}
}
}
}
File: persistence.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0">