spring-jpa JpaRepository#getOne()方法的不同行为

时间:2015-11-25 21:16:47

标签: spring hibernate jpa

spring-jpa with hibernate and mysql 5.7(platform-b​​om-1.1.4.RELEASE):

实体定义:



@Entity
@Table(name="project")
public class ProjectEntity extends MDMBaseDescriptiveEntity
{
}






public class ActivityEntity extends MDMBaseDescriptiveEntity
{

    // optional
    @Column(name="project_id")
    private Integer projectId;

    @ManyToOne(fetch=FetchType.EAGER)
    @JoinColumn(name="container_id")
    private ActivityEntity container;

    @OneToMany(mappedBy="container", cascade={CascadeType.ALL}, fetch=FetchType.EAGER)
    @OrderColumn(name="order")
    private List<ActivityEntity> activities = new ArrayList<ActivityEntity>();
    
    @Enumerated(EnumType.STRING)
    private Activity.Type type;

    @Enumerated(EnumType.STRING)
    private Activity.Mode mode;
    
    private String tag;
...
}
&#13;
&#13;
&#13;

存储库定义:

&#13;
&#13;
public interface IProjectDao extends JpaRepository<ProjectEntity, Integer>
{
}
&#13;
&#13;
&#13;

&#13;
&#13;
public interface IActivityDao extends JpaRepository<ActivityEntity, Integer>
{
    List<ActivityEntity> findByContainerId(int containerId);
}
&#13;
&#13;
&#13;

按ID访问不存在的实体:

  • ProjectEntity pe = projectDao.getOne(9999999); 作为上述调用的结果,预期pe == null,它是。

  • ActivityEntity ae = activityDao.getOne(9999999); 作为上述调用的结果,预期ae == null,但ae!= null,即使其所有属性都具有默认值。在ae上调用getter时抛出javax.persistence.EntityNotFoundException。

为什么行为不同?

0 个答案:

没有答案