?? entitylifecycledaobean.java
字號:
package com.foshanshop.ejb3.impl;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.foshanshop.ejb3.EntityLifecycleDAO;
import com.foshanshop.ejb3.bean.EntityLifecycle;
@Stateless
@Remote ({EntityLifecycleDAO.class})
public class EntityLifecycleDAOBean implements EntityLifecycleDAO {
@PersistenceContext
protected EntityManager em;
public EntityLifecycle Load() {
return em.find(EntityLifecycle.class, 1);//此處將會觸發@PostLoad事件
}
public void Persist() {
EntityLifecycle entitylifecycle = new EntityLifecycle("孫麗");
em.persist(entitylifecycle);//此處將會觸發@PrePersist事件
//下面讓線程等待5秒,在五秒時間內你可以查看數據是否已經插入進數據庫
try {
System.out.println("***當前線程睡眠5秒,在五秒時間內你可以查看數據是否已經插入進數據庫***");
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void Remove() {
Query query = em.createQuery("from EntityLifecycle e");
List result = query.getResultList();
if (result!=null && result.size()>1){
EntityLifecycle entitylifecycle = (EntityLifecycle)result.get(result.size()-1);
em.remove(entitylifecycle);//此處將會觸發@PreRemove事件
//下面讓線程等待5秒,在五秒時間內你可以查看數據是否已經刪除
try {
System.out.println("***當前線程睡眠5秒,在五秒時間內你可以查看數據是否已經刪除***");
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void Update() {
Query query = em.createQuery("from EntityLifecycle e");
List result = query.getResultList();
if (result!=null && result.size()>0){
EntityLifecycle entitylifecycle = (EntityLifecycle)result.get(result.size()-1);
entitylifecycle.setName("張權");
em.merge(entitylifecycle); //此處將會觸發@PreUpdate事件
//下面讓線程等待5秒,在五秒時間內你可以查看數據是否已經更新
try {
System.out.println("***當前線程睡眠5秒,在五秒時間內你可以查看數據是否已經更新***");
Thread.sleep(5*1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -