java类javax.persistence.EntityManager的实例源码

TaskServiceImpl.java 文件源码 项目:aries-jpa 阅读 36 收藏 0 点赞 0 评论 0
@Override
public void addTask(final Task task) {
    jpa.tx(new EmConsumer() {
        @Override
        public void accept(EntityManager em) {
                em.persist(task);
                em.flush();
        }
    });
}
UserResourceIntTest.java 文件源码 项目:MTC_Labrat 阅读 35 收藏 0 点赞 0 评论 0
/**
 * Create a User.
 *
 * This is a static method, as tests for other entities might also need it,
 * if they test an entity which has a required relationship to the User entity.
 */
public static User createEntity(EntityManager em) {
    User user = new User();
    user.setLogin(DEFAULT_LOGIN);
    user.setPassword(RandomStringUtils.random(60));
    user.setActivated(true);
    user.setEmail(DEFAULT_EMAIL);
    user.setFirstName(DEFAULT_FIRSTNAME);
    user.setLastName(DEFAULT_LASTNAME);
    user.setImageUrl(DEFAULT_IMAGEURL);
    user.setLangKey(DEFAULT_LANGKEY);
    return user;
}
SetNetworkSettingsService.java 文件源码 项目:osc-core 阅读 26 收藏 0 点赞 0 评论 0
@Override
public SetNetworkSettingsResponse exec(SetNetworkSettingsRequest request, EntityManager em) throws Exception {

    NetworkSettingsApi networkSettingsApi = new NetworkSettingsApi();
    NetworkSettingsDto networkSettingsDto = new NetworkSettingsDto();
    networkSettingsDto.setDhcp(request.isDhcp());
    if (!request.isDhcp()) {
        networkSettingsDto.setHostIpAddress(request.getHostIpAddress());
        networkSettingsDto.setHostSubnetMask(request.getHostSubnetMask());
        networkSettingsDto.setHostDefaultGateway(request.getHostDefaultGateway());
        networkSettingsDto.setHostDnsServer1(request.getHostDnsServer1());
        networkSettingsDto.setHostDnsServer2(request.getHostDnsServer2());
        validate(request);
    }
    boolean isIpChanged = !NetworkUtil.getHostIpAddress().equals(request.getHostIpAddress());

    if(isIpChanged) {
        // If IP is changed, these connections are no longer valid, shutdown so they get restarted again.
        this.server.shutdownRabbitMq();
        this.server.shutdownWebsocket();
    }

    networkSettingsApi.setNetworkSettings(networkSettingsDto);
    SetNetworkSettingsResponse response = new SetNetworkSettingsResponse();

    /*
     * IP address change needs to get propagated to security managers
     */
    if (isIpChanged) {
        response.setJobId(startIpPropagateJob());
        this.server.startRabbitMq();
        this.server.startWebsocket();
    }

    return response;
}
DeleteK8sDAIInspectionPortTask.java 文件源码 项目:osc-core 阅读 21 收藏 0 点赞 0 评论 0
@Override
public void executeTransaction(EntityManager em) throws Exception {
    OSCEntityManager<DistributedApplianceInstance> daiEmgr = new OSCEntityManager<DistributedApplianceInstance>(DistributedApplianceInstance.class, em, this.txBroadcastUtil);
    this.dai = daiEmgr.findByPrimaryKey(this.dai.getId());

    try (SdnRedirectionApi redirection = this.apiFactoryService.createNetworkRedirectionApi(this.dai.getVirtualSystem())) {
        DefaultInspectionPort inspectionPort = new DefaultInspectionPort(null, null, this.dai.getInspectionElementId(), this.dai.getInspectionElementParentId());
        redirection.removeInspectionPort(inspectionPort);
    }

    // After removing the inspection port from the SDN controller we can now delete this orphan DAI
    OSCEntityManager.delete(em, this.dai, this.txBroadcastUtil);
}
TaskTest.java 文件源码 项目:Mod-Tools 阅读 29 收藏 0 点赞 0 评论 0
@Override
public List<ProcessTask> handle(EntityManager manager) {
    ArrayList<ProcessTask> list = new ArrayList<>();
    list.add(new CounterProcessTask(1));
    list.add(new CounterProcessTask(2));
    list.add(new CounterProcessTask(3));
    return list;
}
DefaultEntryDao.java 文件源码 项目:Guestbook9001 阅读 34 收藏 0 点赞 0 评论 0
@Override
public Entry getEntry(long id) {
    EntityManager em = EntityManagerHelper.getEntityManager();
    EntityManagerHelper.beginTransaction();
    Entry entry = null;
    try {
        entry = em.find(Entry.class, id);
    } catch (Exception e) {
        throw new RuntimeException("Error getting entry", e);
    }
    EntityManagerHelper.commitAndCloseTransaction();
    return entry;
}
SyncDistributedApplianceServiceTest.java 文件源码 项目:osc-core 阅读 24 收藏 0 点赞 0 评论 0
@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(this.em.find(eq(DistributedAppliance.class), eq(MARKED_FOR_DELETE_DA_ID)))
           .thenReturn(MARKED_FOR_DELETE_DA);
    when(this.em.find(eq(DistributedAppliance.class), eq(GOOD_DA_ID))).thenReturn(GOOD_DA);
    when(this.jobFactory.startDAConformJob(any(EntityManager.class), any(DistributedAppliance.class)))
           .thenAnswer(new Answer<Long>() {
               @Override
               public Long answer(InvocationOnMock invocation) throws Throwable {
                   DistributedAppliance result = invocation.getArgumentAt(1, DistributedAppliance.class);
                   return result.getId();
               }
           });
}
JpaUtilTests.java 文件源码 项目:linq 阅读 38 收藏 0 点赞 0 评论 0
@Test
@Transactional
public void testExists() {
    Assert.isTrue(!JpaUtil.exists(User.class), "Not Success.");
    User user = new User();
    user.setId(UUID.randomUUID().toString());
    user.setName("tom");
    User user2 = new User();
    user2.setName("kevin");
    user2.setId(UUID.randomUUID().toString());
    User user3 = new User();
    user3.setName("kevin");
    user3.setId(UUID.randomUUID().toString());

    JpaUtil.persist(user);
    JpaUtil.persist(user2);
    JpaUtil.persist(user3);

    Assert.isTrue(JpaUtil.exists(User.class), "Not Success.");

    EntityManager em = JpaUtil.getEntityManager(User.class);
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<User> cq = cb.createQuery(User.class);
    Root<User> root = cq.from(User.class);

    Assert.isTrue(JpaUtil.exists(cq), "Not Success.");

    cq.where(cb.equal(root.get("name"), "tom"));
    Assert.isTrue(JpaUtil.exists(cq), "Not Success.");

    JpaUtil.removeAllInBatch(User.class);
}
DeploymentSpecConformJobFactory.java 文件源码 项目:osc-core 阅读 23 收藏 0 点赞 0 评论 0
private void updateDSJob(EntityManager em, final DeploymentSpec ds, Job job) {

        // TODO: It would be more sensible to make this decision
        // using if(txControl.activeTransaction()) {...}

        if (em != null) {
            ds.setLastJob(em.find(JobRecord.class, job.getId()));
            OSCEntityManager.update(em, ds, this.txBroadcastUtil);

        } else {

            try {
                EntityManager txEm = this.dbConnectionManager.getTransactionalEntityManager();
                TransactionControl txControl = this.dbConnectionManager.getTransactionControl();
                txControl.required(() -> {
                    DeploymentSpec ds1 = DeploymentSpecEntityMgr.findById(txEm, ds.getId());
                    if (ds1 != null) {
                        ds1.setLastJob(txEm.find(JobRecord.class, job.getId()));
                        OSCEntityManager.update(txEm, ds1, this.txBroadcastUtil);
                    }
                    return null;
                });
            } catch (ScopedWorkException e) {
                // Unwrap the ScopedWorkException to get the cause from
                // the scoped work (i.e. the executeTransaction() call.
                log.error("Fail to update DS job status.", e.getCause());
            }
        }
    }


问题


面经


文章

微信
公众号

扫码关注公众号