問題タブ [open-session-in-view]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票する
1 に答える
574 参照

java - Single transaction per request with Spring MVC and Hibernate

I want to use Spring MVC and Hibernate. I don't want to use service layer or @Transactional attribute on all controller methods (or, rather, I want Spring to treat them all as transactional). So I want to start transaction when controller method starts to work with database and commit transaction when controller method returns ViewAndModel or rollback transaction if any error occured. Also I want view to support lazy hibernate loading, e.g. select data in an autocommit mode if html template requests that.

I'm aware that best practice involves creating a separate service layer with @Transactional attribute, but my application won't benefit from that additional complexity and I want to simplify code as much as possible.

I've learned that OpenSessionInViewInterceptor allows to continue using hibernate session in view, so that probably solves my second requirement. But how do I make all controller methods transactional?

Ideally I want an easy way to opt-out from this behaviour if I ever would need that. E.g. all methods are transactional, yet I can apply something like @NonTransactional and manage transactions more granularly.

0 投票する
1 に答える
599 参照

hibernate - Spring でエンティティを .save() する必要があるのはいつですか?

比較的大規模な Spring Boot プロジェクトでは、次の (非常に単純化された) イベント シーケンスを持つメソッドがあります。

CarPersonおよびEngineはすべて@Entityクラス (データベース オブジェクト) です。この例では、それらの実装は次のようになります。

上記のメソッドは、REST API メソッド内からのみ使用されます。また、構成プロパティで OSIV (Open Session In View) が有効になっています。

問題は、なぜperson.getCar()オブジェクトを保存する必要があるのか​​ということです。これは、Car同じリクエスト処理中に作成したオブジェクトと同じです。どの時点で一時的になり、永続化コンテキストによって管理されなくなりますか? OSIVが有効になっているため、すべての変更が自動的にフラッシュされると思いました。

このようなケースをどのように検出し、メソッドをいつ使用し、いつ無視できるかを正確に知るにはどうすればよいですか?.save()