本文整理匯總了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);
}