Tag: jpa

将Hibernate 5.2与Spring框架4.x集成

我正在使用最新稳定的Spring版本( 4.2.6.RELEASE )。 将Hibernate从5.1升级到5.2并将hibernate-entitymanager依赖更改为hibernate-core hibernate文档后: https : //github.com/hibernate/hibernate-orm/wiki/Migration-Guide—5.2 我没有收到任何编译错误,但我的所有测试都失败了这个堆栈跟踪: Jun 06, 2016 12:00:38 PM org.springframework.test.context.TestContextManager beforeTestMethod WARNING: Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@548b7f67] to process ‘before’ execution of test method [public void com.rh.cores.architecture.tests.units.person.PersonTest.add_iranianNaturalPersonDateIsInvalid_preventSaving()] for test instance [com.rh.cores.architecture.tests.units.person.PersonTest@7f9e8421] org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.NoSuchMethodError: org.hibernate.Session.getFlushMode()Lorg/hibernate/FlushMode; at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) at […]

org.hibernate.loader.MultipleBagFetchException:无法同时获取多个行李

以下是我的代码在这里,我使用多个列表从数据库中获取数据。 从hql查询中获取数据时,它显示exception。 Pojo类 public class BillDetails implements java.io.Serializable { private Long billNo; // other fields @LazyCollection(LazyCollectionOption.FALSE) private List billPaidDetailses = new ArrayList(); private Set productReplacements = new HashSet(0); @LazyCollection(LazyCollectionOption.FALSE) private List billProductList = new ArrayList(); //getter and setter } hmb.xml文件 Hql查询 String hql = “select distinct bd,sum(bpds.amount) from BillDetails as bd ” + “left join […]

具有多个参数的JPA Criteria API

我需要创建一个使用带有多个参数的JPA Criteria API的搜索方法。 现在问题是不是每个参数都是必需的。 因此有些可能为null,并且它们不应包含在查询中。 我已经用CriteriaBuilder尝试了这个,但我看不出如何让它工作。 使用Hibernate Criteria API,这非常简单。 只需创建条件,然后添加限制。 Criteria criteria = session.createCriteria(someClass.class); if(someClass.getName() != null) { criteria.add(Restrictions.like(“name”, someClass.getName()); } 我怎么能用JPA的Criteria API实现同样的目标?

无法提交JPA事务:事务标记为rollbackOnly

我在我正在处理的一个应用程序中使用Spring和Hibernate,我遇到了处理事务的问题。 我有一个服务类,它从数据库中加载一些实体,修改它们的一些值,然后(当一切都有效时)将这些更改提交给数据库。 如果新值无效(我只能在设置后检查),我不想保留更改。 为了防止Spring / Hibernate保存更改,我在方法中抛出exception。 但是会导致以下错误: Could not commit JPA transaction: Transaction marked as rollbackOnly 这是服务: @Service class MyService { @Transactional(rollbackFor = MyCustomException.class) public void doSth() throws MyCustomException { //load entities from database //modify some of their values //check if they are valid if(invalid) { //if they arent valid, throw an exception throw new […]

JPA本机查询连接返回对象但是取消引用会抛出类强制转换exception

我正在使用JPQL Native查询来连接表,查询结果存储在List 。 public String getJoinJpqlNativeQuery() { String final SQL_JOIN = “SELECT v1.bitbit, v1.numnum, v1.someTime, t1.username, t1.anotherNum FROM MasatosanTest t1 JOIN MasatoView v1 ON v1.username = t1.username;” System.out.println(“get join jpql native query is being called ============================”); EntityManager em = null; List out = null; try { em = EmProvider.getDefaultManager(); Query query = em.createNativeQuery(SQL_JOIN); out = […]

Spring引用JPA在TABLE中插入大写名称和Hibernate

我有一个表实体映射为: @Entity public class ItemsToRegister implements Serializable{ @Id @Column(name = “ID_ITEM_TO_REGISTER”) @GeneratedValue(strategy = GenerationType.AUTO) private int id; ….. 当我尝试在数据库中插入新记录时,表名被小写翻译为:items_to_register,但我的表名是ITEMS_TO_REGISTER如何在不更改MySql配置的情况下修复我的问题? (my.cnf中) 我在我的application.properties文件中: spring.jpa.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect spring.jpa.hibernate.naming_strategy = org.hibernate.cfg.ImprovedNamingStrategy

Hibernate抛出奇怪的错误:未映射类

这是错误 org.hibernate.hql.ast.QuerySyntaxException: Payment is not mapped [select p from Payment p] 我不明白为什么会抛出这个错误,应该映射类,因为我会简要地向您展示。 我有一个非常基本的配置,如下所示: http : //docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html 我试图将映射定义添加到hibernate.cfg.xml中,我也尝试以编程方式添加它。 他们都没有工作。 谁能告诉我在这里我错过了什么? (这不是我第一次组建一个Hibernate项目) 这是hibernate.cfg.xml org.hibernate.dialect.MySQLDialect com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/paymentsdatabase xxx xxx true thread create <!– –> 数据库连接工作正常,我已经测试过了 这是我的HibernateUtil中的静态初始化程序 static { try { // Create the SessionFactory from standard (hibernate.cfg.xml) // config file. sessionFactory = new AnnotationConfiguration() .addPackage(“com.lsyh.swati.zk.model”) .addAnnotatedClass(Payment.class) .configure().buildSessionFactory(); } […]

为什么这个流没有返回元素?

我尝试将以下代码编写为流: AbstractDevice myDevice = null; for (AbstractDevice device : session.getWorkplace().getDevices()) { if (device.getPluginconfig().getPluginType().getId() == 1) { myDevice = device; } } 这段代码工作正常。 但是当我像这样重写它时它不再起作用了: myDevice = session.getWorkplace().getDevices().stream() .filter(s -> s.getPluginconfig().getPluginType().getId() == 1) .findFirst().get(); 我从流中返回的Optional没有值。 为什么? 编辑 当我尝试这个时(我仍然从getDevices()获得两个设备): List testList = session.getWorkplace().getDevices() .stream().collect(Collectors.toList()); testList为空。 所以我的设备List流似乎出了问题? 它是一个JavaEE应用程序,我从相应的实体获取我的设备: @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}) @JoinTable(name = “Workplace_AbstractDevice”, joinColumns = { […]

如何在Spring Boot项目中禁用Hibernatevalidation

我有一个spring boot项目,它有一个CrudRepository,一个Entity和一个Controller。 我基本上试图根据传递给Controller的数据来持久化实体。 为此,我使用的是spring-boot-starter-jpa 。 我的实体使用JSR-303注释进行注释, 在将数据传递给CrudRepository以进行持久化之前 ,在控制器中对其进行检查。 控制器方法: @RequestMapping(value = “users”, method = { RequestMethod.POST }) public SuccessfulResponse addUser(@Valid @RequestBody User user, BindingResult validation) { if (validation.hasErrors()) { throw new ValidationException(validation); } User saved = this.users.save(user); return new SuccessfulResponse(saved); } 实体: @Entity /* JPA */ public class User { @Id /* JPA */ @Column(name=”email_address”, […]

如何解决N + 1选择问题?

我无法理解如何在jpa或hibernate中避免n + 1选择。 从我读到的,有’左连接提取’,但我不确定它是否仍然适用于多个列表(oneToMany).. 有人可以向我解释一下,或者给我一个明确完整解释的链接吗? 如果这是一个菜鸟问题,我很抱歉,但我找不到关于这个问题的真正明确的文章或文档。 谢谢