Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring security messing with Hibernate transactions
    text
    copied!<p>I am using Hibernate 4.2, Spring 3.2, and Spring Security 3.1 (latest version, not mixing spring versions). Spring Security was implemented recently at the end, and it is causing an issue.</p> <p>The problem is I have a class that initializes the Hibernate connected database on startup. It writes few Objects into the database. Simplified to make it easier to understand.</p> <pre><code>public class User { @Id String username; @ManyToMany List&lt;Present&gt; presents; } public class Present { @Id String present; } </code></pre> <p>The methods used to write to the database:</p> <pre><code>public class InsertTests { @Autowired private AuthService authService; public void addImages() { authService.addPresent("/presents/first.prs"); authService.addPresent("/presents/second.prs"); } public void addPeople() { // NOTE: The addUser adds a default batch present to the user. authService.addUser("Baby"); authService.addUser("Mommy"); authService.addUser("Tester"); } public void initData(){ addPresents(); addPeople(); } </code></pre> <p>}</p> <p>The xml configuration used:</p> <pre><code>&lt;bean id="insertTests" class="org.security.service.InsertTests" init-method="initData"/&gt; </code></pre> <p>Using hd2bm it creates 3 tables, User, User_Present, Present. Without Spring Security enabled everything is inserted into the database correctly, but when it is enabled in web.xml as such:</p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>Only the User and Presents that are added will be committed to database. The User_Presents table will be completely empty. The strange thing I found when debugging though, is that after the method just completes, the data DOES appear in the database. But afterwards spring calls a chain of contextDestroy which wipes the table. Adding the User+Present after the application have works though. Is there another way I should initialize a default method?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload