首页 \ 问答 \ JPA(hibernate)不会在persist上插入,需要刷新(JPA (hibernate) does not insert on persist, needs to be flushed)

JPA(hibernate)不会在persist上插入,需要刷新(JPA (hibernate) does not insert on persist, needs to be flushed)

我有一个无状态bean来观察事件并保存记录:

@Stateless
public class Manager {
    @Inject
    Repository repository;

    Manager() {}

    @Inject
    Manager(Repository repository) {
        this.repository = repository;
    }

    public void EventHandler(@Observes MyEvent myEvent) {
        save(event.obj);
    }

    private save(Object object) {
        repository.add(object);
    }
}

我的存储库是这样的:

@Stateless
public class Repository {
    @PersistenceContext
    EntityManager em;

    Repository() {}

    public void add(Object object) {
        em.persist(object);
        // em.flush();  <---
    }
}

除非我取消注释,否则它不起作用我不明白的是为什么我需要冲洗! 交易不应该自动提交吗?

可能是因为我有一个EJB容器并且Observe启动的cdi事务从未实际结束但是做了一些奇怪的事情? 或者它结束但不提交因为他不了解EJB?


I have a stateless bean that observes an event and saves a record:

@Stateless
public class Manager {
    @Inject
    Repository repository;

    Manager() {}

    @Inject
    Manager(Repository repository) {
        this.repository = repository;
    }

    public void EventHandler(@Observes MyEvent myEvent) {
        save(event.obj);
    }

    private save(Object object) {
        repository.add(object);
    }
}

My repository is like this:

@Stateless
public class Repository {
    @PersistenceContext
    EntityManager em;

    Repository() {}

    public void add(Object object) {
        em.persist(object);
        // em.flush();  <---
    }
}

It doesn’t work unless I uncomment the line The thing I don’t understand is why do I need to flush! Shouldn’t the transaction commit automatically?

Could it be that I have an EJB container and the cdi transaction started by Observes never actually ends but do something weird? Or it ends but doesn’t commit because he doesn’t know about EJB?


原文:https://stackoverflow.com/questions/30551473
更新时间:2022-04-03 18:04

最满意答案

我会将导航栏的高度作为变量,这样无论使用什么设备都没关系。

$('a[href^="#"]').on('click', function(event) {
        var target = $( $(this).attr('href') ),
            navBarHeight = $(".nav").height();

        if( target.length ) {
            event.preventDefault();
            $('html, body').animate({
                scrollTop: (target.offset().top) - navBarHeight
            }, 1000);
        }

    });

显然,更改$(".nav")以使用正确的选择器。 您还可以通过向navBarHeight添加常量来添加偏移量。


I would get the height of the nav-bar as a variable, that way it doesn't matter what device is used.

$('a[href^="#"]').on('click', function(event) {
        var target = $( $(this).attr('href') ),
            navBarHeight = $(".nav").height();

        if( target.length ) {
            event.preventDefault();
            $('html, body').animate({
                scrollTop: (target.offset().top) - navBarHeight
            }, 1000);
        }

    });

Obviously, change $(".nav") to use the correct selector for your purpose. You could also add an offset by adding a constant to navBarHeight.

相关问答

更多