本文整理汇总了Java中javax.persistence.EntityManager.persist方法的典型用法代码示例。如果您正苦于以下问题:Java EntityManager.persist方法的具体用法?Java EntityManager.persist怎么用?Java EntityManager.persist使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.persistence.EntityManager
的用法示例。
在下文中一共展示了EntityManager.persist方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: changeTaskOwner
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public void changeTaskOwner(long taskId, String newOwner) {
Task task=this.getTask(taskId);
if(task.getStatus().equals(TaskStatus.Created)){
EntityManager em=getEntityManager();
EntityTransaction trans=em.getTransaction();
try{
trans.begin();
task.setOwner(newOwner);
em.persist(task);
trans.commit();
}catch(Exception ex){
trans.rollback();
throw new RuntimeException(ex);
}
}else{
throw new IllegalStateException("Current task status is "+task.getStatus()+",can not change it owner!");
}
}
示例2: persist
import javax.persistence.EntityManager; //导入方法依赖的package包/类
private static void persist(SecurityGroupMember sgm, EntityManager em) {
SecurityGroup sg = sgm.getSecurityGroup();
VirtualizationConnector vc = sg.getVirtualizationConnector();
Set<VirtualSystem> virtualSystems = vc.getVirtualSystems();
// If the SG already has an id it already has been persisted.
if (sg.getId() == null) {
em.persist(vc);
em.persist(sg);
for (VirtualSystem vs : virtualSystems) {
em.persist(vs.getDomain().getApplianceManagerConnector());
em.persist(vs.getApplianceSoftwareVersion().getAppliance());
em.persist(vs.getApplianceSoftwareVersion());
em.persist(vs.getDistributedAppliance());
em.persist(vs.getDomain());
em.persist(vs);
}
}
em.persist(sgm.getLabel());
em.persist(sgm);
}
示例3: persist
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static Companies persist(Companies company) {
if (company != null) {
EntityManager em = EMFUtil.getEMFactory().createEntityManager();
EntityTransaction trans = em.getTransaction();
try {
trans.begin();
em.persist(company);
trans.commit();
return company;
} catch (Exception e) {
e.printStackTrace();
trans.rollback();
return null;
} finally {
em.close();
}
}
return null;
}
示例4: jpa_with_active_span_only
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Test
public void jpa_with_active_span_only() {
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("jpa_active_span_only");
Employee employee = new Employee();
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(employee);
entityManager.getTransaction().commit();
entityManager.close();
entityManagerFactory.close();
assertNotNull(employee.id);
List<MockSpan> finishedSpans = mockTracer.finishedSpans();
assertEquals(0, finishedSpans.size());
assertNull(mockTracer.activeSpan());
}
示例5: additem
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public void additem()
{
EntityManager em = w.getEntityManager();
em.getTransaction().begin();
em.persist(w);
em.getTransaction().commit();
w = new Warehouses();
}
示例6: persist
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static void persist(SecurityGroupMember sgm, EntityManager em) {
em.getTransaction().begin();
SecurityGroup sg = sgm.getSecurityGroup();
VirtualizationConnector vc = sg.getVirtualizationConnector();
Set<VirtualSystem> virtualSystems = vc.getVirtualSystems();
em.persist(vc);
em.persist(sg);
for (VirtualSystem vs : virtualSystems) {
em.persist(vs.getDomain().getApplianceManagerConnector());
em.persist(vs.getApplianceSoftwareVersion().getAppliance());
em.persist(vs.getApplianceSoftwareVersion());
em.persist(vs.getDistributedAppliance());
em.persist(vs.getDomain());
em.persist(vs);
}
for (Pod pod : sgm.getLabel().getPods()) {
em.persist(pod);
}
em.persist(sgm.getLabel());
em.persist(sgm);
em.getTransaction().commit();
}
开发者ID:opensecuritycontroller,项目名称:osc-core,代码行数:28,代码来源:UpdateK8sSecurityGroupMemberLabelMetaTaskTestData.java
示例7: createNewGuest
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public void createNewGuest(Guest guest) {
EntityManager entityManager = HibernateUtil.getEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist(guest);
entityManager.getTransaction().commit();
} catch (PersistenceException e) {
entityManager.getTransaction().rollback();
} finally {
entityManager.close();
}
}
示例8: insert
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Override
public void insert(User po) {
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
entityManager.getTransaction().begin();
entityManager.persist(po);
entityManager.getTransaction().commit();
} catch (RuntimeException e) {
entityManager.getTransaction().rollback();
}
entityManager.close();
}
示例9: addCreateDocument
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public void addCreateDocument(){
d.setDocumentDate(new Date());
for(int i = 0; i < alltd.size(); i++){
if(alltd.get(i).getTypedocId() == temptd){
d.setTypedocId(alltd.get(i));
}
}
EntityManager em = d.getEntityManager();
em.getTransaction().begin();
em.persist(d);
em.getTransaction().commit();
for(int i = 0; i < alldd.size(); i++){
if(alldd.get(i).getDd().getItemId() == null || alldd.get(i).getDd().getWarehousesId() == null || alldd.get(i).getDd().getQuantity() == 0){
alldd.remove(i);
i--;
}
}
for(int i = 0; i < alldd.size(); i++){
alldd.get(i).getDd().setDocumentId(d);
EntityManager em2 = alldd.get(i).getDd().getEntityManager();
em2.getTransaction().begin();
em2.persist(alldd.get(i).getDd());
em2.getTransaction().commit();
}
createD();
}
示例10: persistObjects
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static void persistObjects(EntityManager em, SecurityGroup sg) {
Set<VirtualSystem> virtualSystems = sg.getVirtualizationConnector().getVirtualSystems();
em.getTransaction().begin();
em.persist(sg.getVirtualizationConnector());
for (VirtualSystem vs : virtualSystems) {
em.persist(vs.getDomain().getApplianceManagerConnector());
em.persist(vs.getApplianceSoftwareVersion().getAppliance());
em.persist(vs.getApplianceSoftwareVersion());
em.persist(vs.getDistributedAppliance());
em.persist(vs.getDomain());
em.persist(vs);
}
em.persist(sg);
for (SecurityGroupMember sgm : sg.getSecurityGroupMembers()) {
if (!sgm.getPodPorts().isEmpty()) {
for (Pod pod : sgm.getLabel().getPods()) {
for (PodPort podPort : pod.getPorts()) {
em.persist(podPort);
}
em.persist(pod);
}
}
em.persist(sgm.getLabel());
em.persist(sgm);
}
sg.getSecurityGroupInterfaces().forEach(sgi -> em.persist(sgi));
em.getTransaction().commit();
}
示例11: createEntity
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* Create an entity for this test.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static Tag createEntity(EntityManager em) {
Tag tag = new Tag()
.typeKey(DEFAULT_TYPE_KEY)
.name(DEFAULT_NAME)
.startDate(DEFAULT_START_DATE);
// Add required entity
XmEntity xmEntity = XmEntityResourceIntTest.createEntity(em);
em.persist(xmEntity);
em.flush();
tag.setXmEntity(xmEntity);
return tag;
}
示例12: main
import javax.persistence.EntityManager; //导入方法依赖的package包/类
public static void main(String[] args) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
Role r1 = new Role("ordinary");
Role r2 = new Role("super");
em.persist(r1);
em.persist(r2);
User user = new User("Paweł", "Bogdan", 28, r1);
em.persist(user);
user = new User("Adam", "Mickiewicz", 44, r2);
em.persist(user);
user = new User("Juliusz", "Słowacki", 33, r1);
em.persist(user);
user = new User("Jan", "Kowalski", 55, r2);
em.persist(user);
em.getTransaction().commit();
em.close();
entityManagerFactory.close();
}
示例13: testWriteRead
import javax.persistence.EntityManager; //导入方法依赖的package包/类
@Test
public void testWriteRead() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
JsonProvider jp = JsonProvider.provider();
try {
MyEntity entity = new MyEntity();
entity.id = 1l;
entity.js = jp.createArrayBuilder().add("str").add(false).add(0l).build();
entity.jo = jp.createObjectBuilder().add("thearray", jp.createArrayBuilder().add("str").add(false).add(0l).build()).build();
entity.ja = jp.createArrayBuilder().add(4).add("word").build();
LOGGER.info("Persisting entity: " + entity);
em.persist(entity);
entity = new MyEntity();
entity.id = 2l;
// leave nulls because postgres is being shitty with those
LOGGER.info("Persisting entity: " + entity);
em.persist(entity);
em.getTransaction().commit();
}
finally {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
em = emf.createEntityManager();
em.getTransaction().begin();
try {
MyEntity entity1 = new MyEntity();
entity1.id = 1l;
entity1.js = jp.createArrayBuilder().add("str").add(false).add(0l).build();
entity1.jo = jp.createObjectBuilder().add("thearray", jp.createArrayBuilder().add("str").add(false).add(0l).build()).build();
entity1.ja = jp.createArrayBuilder().add(4).add("word").build();
MyEntity entity2 = new MyEntity();
entity2.id = 2l;
MyEntity me1 = em.find(MyEntity.class, new Long(1));
LOGGER.info("Extracted entity: " + me1);
MyEntity me2 = em.find(MyEntity.class, new Long(2));
LOGGER.info("Extracted entity: " + me2);
assertEquals(entity1, me1);
assertEquals(entity2, me2);
}
finally {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
}
}
示例14: createEntity
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* Create an entity for this test.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static Testeur createEntity(EntityManager em) {
Testeur testeur = new Testeur()
.identifiant(DEFAULT_IDENTIFIANT)
.nom(DEFAULT_NOM)
.prenom(DEFAULT_PRENOM);
// Add required entity
TypeTest typetest = TypeTestResourceIntTest.createEntity(em);
em.persist(typetest);
em.flush();
testeur.setTypetest(typetest);
return testeur;
}
示例15: addBlogUser
import javax.persistence.EntityManager; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void addBlogUser(BlogUser user) throws DAOException {
EntityManager em = JPAEMProvider.getEntityManager();
em.persist(user);
}