本文整理汇总了Java中javax.persistence.EntityManager.find方法的典型用法代码示例。如果您正苦于以下问题:Java EntityManager.find方法的具体用法?Java EntityManager.find怎么用?Java EntityManager.find使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.EntityManager
的用法示例。
在下文中一共展示了EntityManager.find方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testInsert
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Test
public void testInsert() {
EntityManager em = emf.createEntityManager();
Utente u = new Utente();
u.setUsername(TEST_USER);
em.getTransaction().begin();
em.persist(u);
em.getTransaction().commit();
Utente expected = em.find(Utente.class, TEST_USER);
if (expected == null) {
fail("Utente non inserito");
} else {
assertEquals(expected, u);
}
}
示例2: handle
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public List<ProcessTask> handle(EntityManager manager) {
if (!manager.getTransaction().isActive()) {
manager.getTransaction().begin();
}
ArrayList<ProcessTask> list = new ArrayList<>();
Original file = (Original) manager.find(Original.class, aid);
if(null == file) {
System.out.println("Original with aid "+aid+" wasn't found");
} else if (null == file.getContent() || "".equals(file.getContent())) {
file.setContent(getContent(file.getRelativePath()));
list.add(new GenerateFilePatch(aid));
}
manager.getTransaction().commit();
return list;
}
示例3: addUserEntity
import javax.persistence.EntityManager; //导入方法依赖的package包/类
private void addUserEntity(EntityManager em) {
User user = new User();
user.setFirstName("Trinh");
user.setLastName("Lee");
user.setLoginName("tlee");
user.setRole(RoleType.ADMIN);
user.setEmail("[email protected]");
OSCEntityManager.create(em, user, this.txBroadcastUtil);
// retrieve back and validate
user = em.find(User.class, user.getId());
assertNotNull(user);
}
示例4: executeTransaction
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public void executeTransaction(EntityManager em) throws Exception {
this.sg = em.find(SecurityGroup.class, this.sg.getId());
VirtualizationType virtualizationType = this.sg.getVirtualizationConnector().getVirtualizationType();
this.tg = new TaskGraph();
if (virtualizationType == VirtualizationType.OPENSTACK) {
this.tg.addTask(this.validateSecurityGroupProjectTask.create(this.sg));
this.tg.appendTask(this.securityGroupUpdateOrDeleteMetaTask.create(this.sg));
} else if (virtualizationType == VirtualizationType.KUBERNETES) {
this.tg.addTask(this.updateOrDeleteK8sSecurityGroupMetaTask.create(this.sg));
}
if (!ValidateUtil.isEmpty(this.sg.getSecurityGroupInterfaces())) {
List<VirtualSystem> referencedVs = VirtualSystemEntityMgr.listReferencedVSBySecurityGroup(em,
this.sg.getId());
for (VirtualSystem vs : referencedVs) {
if (vs.getMgrId() != null && this.apiFactoryService.syncsPolicyMapping(vs)) {
this.tg.appendTask(this.mgrSecurityGroupInterfacesCheckMetaTask.create(vs),
TaskGuard.ALL_PREDECESSORS_COMPLETED);
}
}
}
}
示例5: validate
import javax.persistence.EntityManager; //导入方法依赖的package包/类
private SecurityGroup validate(EntityManager em, BaseDeleteRequest request) throws Exception {
BaseIdRequestValidator.checkForNullIdAndParentNullId(request);
VirtualizationConnector vc = VirtualizationConnectorEntityMgr.findById(em, request.getParentId());
if (vc == null) {
throw new VmidcBrokerValidationException("Virtualization Connector with Id: " + request.getParentId()
+ " is not found.");
}
SecurityGroup securityGroup = em.find(SecurityGroup.class, request.getId());
if (securityGroup == null) {
throw new VmidcBrokerValidationException("Security Group with Id: " + request.getId() + " is not found.");
}
if (!securityGroup.getMarkedForDeletion() && request.isForceDelete()) {
throw new VmidcBrokerValidationException(
"Security Group with ID "
+ request.getId()
+ " is not marked for deletion and force delete operation is applicable only for entries marked for deletion.");
}
return securityGroup;
}
示例6: updateEntity
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public boolean updateEntity(long id, Category entity) {
boolean found = false;
EntityManager em = getEM();
try {
em.getTransaction().begin();
PersistenceCategory category = em.find(getEntityClass(), id);
if (category != null) {
category.setName(entity.getName());
category.setDescription(entity.getDescription());
found = true;
}
em.getTransaction().commit();
} finally {
em.close();
}
return found;
}
示例7: getSchoolByID
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Schools getSchoolByID (int schoolID) {
if (schoolID > 0) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
Schools school = em.find(Schools.class, schoolID);
em.close();
return school;
}
return null;
}
示例8: executeTransaction
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public void executeTransaction(EntityManager em) throws Exception {
delayedInit();
this.vs = em.find(VirtualSystem.class, this.vs.getId());
this.tg = new TaskGraph();
if (this.vs.getMarkedForDeletion() || this.vs.getDistributedAppliance().getMarkedForDeletion()) {
this.tg = buildVirtualSystemDeleteTaskGraph();
} else {
this.tg = buildVirtualSystemUpdateTaskGraph();
}
}
示例9: executeTransaction
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public void executeTransaction(EntityManager em) throws Exception {
this.tg = new TaskGraph();
Label label = this.sgm.getLabel();
label = em.find(Label.class, label.getId());
List<KubernetesPod> k8sPods = Collections.emptyList();
try (KubernetesClient client = new KubernetesClient(this.sgm.getSecurityGroup().getVirtualizationConnector())) {
if (this.k8sPodApi == null) {
this.k8sPodApi = new KubernetesPodApi(client);
} else {
this.k8sPodApi.setKubernetesClient(client);
}
k8sPods = this.k8sPodApi.getPodsByLabel(this.sgm.getLabel().getValue());
}
Set<String> existingPodIdsInOSC = emptyIfNull(label.getPods()).stream().map(Pod::getExternalId)
.collect(Collectors.toSet());
Set<String> existingPodIdsInK8s = emptyIfNull(k8sPods).stream().map(KubernetesPod::getUid)
.collect(Collectors.toSet());
Set<KubernetesPod> k8sPodsToCreate = emptyIfNull(k8sPods).stream()
.filter(p -> !existingPodIdsInOSC.contains(p.getUid())).collect(Collectors.toCollection(LinkedHashSet::new));
Set<Pod> dbPodsToDelete = emptyIfNull(label.getPods()).stream()
.filter(p -> !existingPodIdsInK8s.contains(p.getExternalId())).collect(Collectors.toCollection(LinkedHashSet::new));
// TODO emanoel: Using append instead of adding here because of a transactional issue updating multiple
// pods in the same label concurrently. An alternative to this might be locking the label member entity.
k8sPodsToCreate.forEach(p -> this.tg.appendTask(this.labelPodCreateTask.create(p, this.sgm.getLabel())));
dbPodsToDelete.forEach(p -> this.tg.addTask(this.labelPodDeleteTask.create(p, this.sgm.getLabel())));
}
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:34,代码来源:UpdateK8sSecurityGroupMemberLabelMetaTask.java
示例10: carLifecycleRL
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* Create, find and delete car using resource local transactions
* @param emf
* @throws BundleException
*/
protected void carLifecycleRL(EntityManager em) throws BundleException {
em.getTransaction().begin();
Car car = createBlueCar();
em.persist(car);
em.getTransaction().commit();
Car car2 = em.find(Car.class, BLUE_PLATE);
assertBlueCar(car2);
em.getTransaction().begin();
em.remove(car2);
em.getTransaction().commit();
em.close();
}
示例11: findInternal
import javax.persistence.EntityManager; //导入方法依赖的package包/类
protected Dto findInternal(EntityManager manager, long id) throws NotFoundException {
Dto entity = manager.find(classVersion, id);
if (entity == null) {
throw new NotFoundException(Messages.DEPLOY_TARGET_NOT_FOUND, id);
}
return entity;
}
示例12: addApplianceEntity
import javax.persistence.EntityManager; //导入方法依赖的package包/类
private Appliance addApplianceEntity(EntityManager em) {
Appliance appliance = new Appliance();
appliance.setManagerType("NSM");
appliance.setModel("model-1");
appliance.setManagerSoftwareVersion("1.2");
OSCEntityManager.create(em, appliance, this.txBroadcastUtil);
// retrieve back and validate
appliance = em.find(Appliance.class, appliance.getId());
assertNotNull(appliance);
return appliance;
}
示例13: findById
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Appliance findById(EntityManager em, Long id) {
return em.find(Appliance.class, id);
}
示例14: edit
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public void edit(Propietario propietario) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
Propietario persistentPropietario = em.find(Propietario.class, propietario.getCedula());
Collection<Telefono> telefonoCollectionOld = persistentPropietario.getTelefonoCollection();
Collection<Telefono> telefonoCollectionNew = propietario.getTelefonoCollection();
List<String> illegalOrphanMessages = null;
for (Telefono telefonoCollectionOldTelefono : telefonoCollectionOld) {
if (!telefonoCollectionNew.contains(telefonoCollectionOldTelefono)) {
if (illegalOrphanMessages == null) {
illegalOrphanMessages = new ArrayList<String>();
}
illegalOrphanMessages.add("You must retain Telefono " + telefonoCollectionOldTelefono + " since its propietarioId field is not nullable.");
}
}
if (illegalOrphanMessages != null) {
throw new IllegalOrphanException(illegalOrphanMessages);
}
Collection<Telefono> attachedTelefonoCollectionNew = new ArrayList<Telefono>();
for (Telefono telefonoCollectionNewTelefonoToAttach : telefonoCollectionNew) {
telefonoCollectionNewTelefonoToAttach = em.getReference(telefonoCollectionNewTelefonoToAttach.getClass(), telefonoCollectionNewTelefonoToAttach.getId());
attachedTelefonoCollectionNew.add(telefonoCollectionNewTelefonoToAttach);
}
telefonoCollectionNew = attachedTelefonoCollectionNew;
propietario.setTelefonoCollection(telefonoCollectionNew);
propietario = em.merge(propietario);
for (Telefono telefonoCollectionNewTelefono : telefonoCollectionNew) {
if (!telefonoCollectionOld.contains(telefonoCollectionNewTelefono)) {
Propietario oldPropietarioIdOfTelefonoCollectionNewTelefono = telefonoCollectionNewTelefono.getPropietarioId();
telefonoCollectionNewTelefono.setPropietarioId(propietario);
telefonoCollectionNewTelefono = em.merge(telefonoCollectionNewTelefono);
if (oldPropietarioIdOfTelefonoCollectionNewTelefono != null && !oldPropietarioIdOfTelefonoCollectionNewTelefono.equals(propietario)) {
oldPropietarioIdOfTelefonoCollectionNewTelefono.getTelefonoCollection().remove(telefonoCollectionNewTelefono);
oldPropietarioIdOfTelefonoCollectionNewTelefono = em.merge(oldPropietarioIdOfTelefonoCollectionNewTelefono);
}
}
}
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
String id = propietario.getCedula();
if (findPropietario(id) == null) {
throw new NonexistentEntityException("The propietario with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
示例15: edit
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public void edit(Telefono telefono) throws NonexistentEntityException, RollbackFailureException, Exception {
EntityManager em = null;
try {
utx.begin();
em = getEntityManager();
Telefono persistentTelefono = em.find(Telefono.class, telefono.getId());
Propietario propietarioIdOld = persistentTelefono.getPropietarioId();
Propietario propietarioIdNew = telefono.getPropietarioId();
if (propietarioIdNew != null) {
propietarioIdNew = em.getReference(propietarioIdNew.getClass(), propietarioIdNew.getCedula());
telefono.setPropietarioId(propietarioIdNew);
}
telefono = em.merge(telefono);
if (propietarioIdOld != null && !propietarioIdOld.equals(propietarioIdNew)) {
propietarioIdOld.getTelefonoCollection().remove(telefono);
propietarioIdOld = em.merge(propietarioIdOld);
}
if (propietarioIdNew != null && !propietarioIdNew.equals(propietarioIdOld)) {
propietarioIdNew.getTelefonoCollection().add(telefono);
propietarioIdNew = em.merge(propietarioIdNew);
}
utx.commit();
} catch (Exception ex) {
try {
utx.rollback();
} catch (Exception re) {
throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
}
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = telefono.getId();
if (findTelefono(id) == null) {
throw new NonexistentEntityException("The telefono with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}