从hibernate 4.3.11迁移到5.2.10时,hibernate session.get不加载持久对象,而是使用EntityManager代替SessionFactory

时间:2018-05-23 05:57:35

标签: hibernate jpa entitymanager hibernate-entitymanager

我在hibernate 4.3.11.Final 和spring 4.3.1.RELEASE 中使用的项目中使用了多年的maniuplating和persisting对象。最近我迁移到hibernate 5.2.10.Final 并遇到了描述它的问题。我有两个域类,如下所示:

public class Post extends BaseEntity<Long> {
    private String title;
    private Set<PostComment> comments = new HashSet<>(0);

    // getters and setters removed for brevity
}

public class PostComment extends BaseEntity<Long> {
    private Post post;
    private String descripton;

    // getters and setters removed for brevity
}

public abstract class BaseEntity<T> implements Serializable {
    private T id;
}

这些是hibernate映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="" table="tbl_post">
        <id name="id" type="long" >
            <column name="ID"  />
            <generator class="sequence" >
                <param name="sequence">RGH.SEQ_POST</param>
            </generator>
        </id>

        <property name="title" column="title" type="string" not-null="true" />

        <set name="comments" inverse="true" cascade="save-update,merge">
            <key>
                <column name="post_id" not-null="true" />
            </key>
            <one-to-many class="com.rgh.PostComment" />
        </set>
    </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="" table="tbl_post_comment">
        <id name="id" type="long" >
            <column name="ID"  />
            <generator class="sequence" >
                <param name="sequence">RGH.SEQ_POST_COMMENT</param>
            </generator>
        </id>

        <property name="descripton" column="descripton" type="string" not-null="true" />

        <many-to-one name="post" column="post_id" entity-name="com.rgh.Post" not-null="true" />
    </class>
</hibernate-mapping>

PostService类中存在两种方法,其中一个主题持续Post实体及其PostComment&#39; s和另一个方法保存Post实体和日志计数PostCommment&#39; s。

@Service
public class PostService {
    @Autowired
    private PostRepository repo;

    @Autowired
    private PostCommentRepository commentRepo;

    @Transactional
    public Long save() {
        Post post = new Post();
        post.setTitle("sample post");
        repo.save(post);

        Set<PostComment> postComments = new HashSet<>();
        for (int i = 0 ; i < 3 ; i++) {
            PostComment postComment = new PostComment();
            postComment.setPost(post);
            postComment.setDescription("description " + i);
            commentRepo.save(postComment);
        }

        return post.getId();
    }

    @Transactional
    public int saveAndGetDetailsCount(Post entity) {
        entity.setTitle(entity.getTitle() + " updated!");
        repo.save(entity);

        Post post = repo.loadById(entity.getId());
        System.out.println(post.Comments().size());
        return post.Comments().size();
    }
}

PostRepositoryPostCommentRepository都从GenericRepository延伸,您可以在下面看到它:

@Repository
public class PostRepository extends GenericRepository<Post, Long> {
    @Override
    protected Class<Post> getDomainClass() {
        return Post.class;
    }
}

@Repository
public class PostCommentRepository extends GenericRepository<PostComment, Long> {
    @Override
    protected Class<PostComment> getDomainClass() {
        return PostComment.class;
    }
}

@Repository
public abstract class GenericRepository<T extends BaseEntity,PK extends Serializable> {

    protected Class<T> domainClass = getDomainClass();

    @Autowired
    private SessionFactory sessionFactory;

    public Session getSession() {
        try {
            return sessionFactory.getCurrentSession();
        } catch (Exception e) {
        }
        return sessionFactory.openSession();
    }

    public PK save(T Entity) {
        Session session = getSession();
        if(entity.getId() == null) {
            session.save(entity);
        }
        else {
            entity = (T)session.merge(entity);
            session.update(entity);
        }
        return entity.getId();
    }

    public T loadByEntityId(PK entityId) {
        Session session = getSession();
        return  (T) session.get(domainClass.getName(), entityId);
    }
}

当我调用save方法然后调用saveAndGetDetailsCount它更新实体然后执行select...查询以加载Post实体然后记录 3 并发表评论。

但是在我迁移到hibernate 5.2.10.Final 之后并应用了这些更改:

@Entity
@Table(schema = "RGH", name = "TBL_POST")
@SequenceGenerator(name = "sequence_db", sequenceName = "RGH.SEQ_POST", allocationSize = 1)
public class Post extends BaseEntity<Long> {

    @Column
    private String title;

    @OneToMany(mappedBy = "post", fetch = FetchType.LAZY)
    @Cascade(value = { CascadeType.SAVE_UPDATE, CascadeType.MERGE })
    private Set<PostComment> comments = new HashSet<>(0);

    // getters and setters removed for brevity
}

@Entity
@Table(schema = "RGH", name = "TBL_POST_COMMENT")
@SequenceGenerator(name = "sequence_db", sequenceName = "RGH.SEQ_POST_COMMENT", allocationSize = 1)
public class PostComment extends BaseEntity<Long> {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "POST_ID", nullable = false)
    private Post post;

    @Column
    private String descripton;

    // getters and setters removed for brevity
}

public abstract class BaseEntity<T> implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequence_db")
    private T id;
}

然后GenericRepository我使用EntityManager来获取Session课程,如下所示:

@Repository
public abstract class GenericRepository<T extends BaseEntity,PK extends Serializable> {

    protected Class<T> domainClass = getDomainClass();

    /* changed */
    @PersistenceContext
    private EntityManager em;

    public Session getSession() {
        /* changed */
        return  em.unwrap(Session.class);
    }

    public PK save(T Entity) {
        Session session = getSession();
        if(entity.getId() == null) {
            session.save(entity);
        }
        else {
            entity = (T)session.merge(entity);
            session.update(entity);
        }
        return entity.getId();
    }

    public T loadByEntityId(PK entityId) {
        Session session = getSession();
        return  (T) session.get(domainClass.getName(), entityId);
    }
}

现在,问题在于,当我运行以前的方法调用时,在saveAndGetDetailsCount中,hibernate不执行select...查询并将 0 记录为注释计数。< / p>

我想知道为什么会这样。

更新

也许你问为什么我没有使用EntityManager代替Session,我使用EntityManager时有一些限制。

更新

当我拨打saveAndGetDetailsCount时,entity参数仅包含idtitle,在下一行中,您可以看到我想加载comments }并获得它的大小。

2 个答案:

答案 0 :(得分:1)

如果实体已经附加到当前会话,为什么要生成查询?

另外,为什么要调用merge和更新?你应该使用其中一个。但不是两个。

答案 1 :(得分:1)

您的问题不在迁移到休眠5中。我认为您的问题出在事务中。当在同一事务中执行持久或合并调用时,loadById直到方法返回时才执行。保存后可以刷新以正确合并工作和对象加载。

将您的代码更改为此

@Repository
public abstract class GenericRepository<T extends BaseEntity,PK extends Serializable> {

    protected Class<T> domainClass = getDomainClass();

    /* changed */
    @PersistenceContext
    private EntityManager em;

    public Session getSession() {
        /* changed */
        return  em.unwrap(Session.class);
    }

    public PK save(T Entity) {
        Session session = getSession();
        if(entity.getId() == null) {
            em.persist(entity);
        }
        else {
            em.merge(entity);
            em.flush();
        }
        return entity.getId();
    }

    public T loadByEntityId(PK entityId) {
        Session session = getSession();
        return  (T) session.get(domainClass.getName(), entityId);
    }
}