AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
### 保存 ~~~ public class JpaTest { /** * 1. 加载配置文件创建实体管理器工厂对象 * 2. 通过实体管理器工厂创建实体管理器 * 3. 获取事务,开启事务 * 4. 完成增删改查 * 5. 提交事务 * 6. 释放资源 */ @Test public void testSave(){ EntityManagerFactory factory = Persistence.createEntityManagerFactory("myJpa"); EntityManager em = factory.createEntityManager(); EntityTransaction tx = em.getTransaction(); Customer customer = new Customer(); customer.setName("test"); em.persist(customer); tx.begin(); tx.commit(); em.close(); factory.close(); } } ~~~ ### 查找 ``` 根据id查询数据 Customer customer = entityManager.find(Customer.class, 1); ``` ``` 根据id查询数据, 得到一个动态代理对象,什么时候用什么时候查,懒加载 Customer customer = entityManager.getReference(Customer.class, 1); ```