22. 25. 8. 查询更新模式 |
|
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<Message> messages = new ArrayList<Message>();
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<Message> 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: 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: 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: 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<Conversation> active = (List<Conversation>) 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: persistence.xml |
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
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">
<persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:data/tutorial"/>
</properties>
</persistence-unit>
</persistence>
|
|
Download: JPA-QueryFlushMode.zip( 5,289 k) |