我有以下关系:
@Entity
public class SomeEntity {
//...
@EmbeddedId
private SomeEntityIdentity id;
@OneToOne
@NotFound(action = NotFoundAction.EXCEPTION) //This is the important bit
@JoinColumns({
//...
})
private OtherEntity example;
//...
}
然后,我用Spring数据的findOne()
通过ID抢我的实体:
SomeEntityIdentity id = new SomeEntityIdentity();
id.setAttribute1(1);
id.setAttribute2(new BigDecimal(123));
return this.someEntityRepository.findOne(id);
问题是,如果没有异常被抛出OtherEntity
是找不到的,因为findOne()
简单地返回null。即使我设置@OneToOne(optional = false)
我仍然得到一个空findOne()
,那时我才除外OtherEntity
为空。
我相信,一个异常应该被抛出。有没有人有任何想法?
谢谢!
编辑:身份及以下库类。
@Embeddable
public class SomeEntityIdentity implements Serializable {
private int attribute1;
private BigDecimal attribute2;
public void setAttribute1(int attribute1) {
this.attribute1 = attribute1;
}
public void setAttribute2(BigDecimal attribute2) {
this.attribute2 = attribute2;
}
}
public interface SomeEntityRepository extends JpaRepository<SomeEntity, SomeEntityIdentity> {
}
原来是Hibernate和Spring数据版本之间的不兼容。
使用Hibernate这个项目是4.3.1.Final
充分利用JPA 2.1
功能; 但是spring-data-jpa
1.6.6.RELEASE
,这不支持休眠版本。
因为一切都正常工作(直到这个问题)我没有在第一次注意到这一点。当我试图升级spring-data-jpa
到休眠4.3
兼容的版本,我不能,因为spring-data-jpa
从休眠3跳转到休眠5 2.0.x
版本。这似乎也需要Java 8,所以这是一个没有去我。
最终降级到休眠3.6.10.Final
和一切开始工作正常。
TL; DR:经常检查Spring和其他图书馆之间的版本兼容性,即使没有明显的错误。
我只是说,Spring版本管理是共苦想完成。
工作依赖配置:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.6.6.RELEASE</version>
</dependency>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句