Take the 2-minute tour ×

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.

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.

public class User {
    @Id String username;
    @ManyToMany List<Present> presents;

public class Present {
    @Id String present;

The methods used to write to the database:

public class InsertTests {

private AuthService authService;

public void addImages() {

public void addPeople() {
    // NOTE: The addUser adds a default batch present to the user.

public void initData(){


The xml configuration used:

<bean id="insertTests" class="org.security.service.InsertTests" init-method="initData"/>

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:



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?

share|improve this question
This seems completely strange. What does your security configuration look like in your application context? I don't see any transaction management in insertTests, did you leave that out for brevity? Are you absolutely certain that simply commenting out the security filter causes everything to work? Do you have other filters whose order might be getting changed? To answer your last question, a safer way to initialize such default data might be to use Spring's Lifecycle interface. –  Pace Nov 27 '13 at 3:50
Pace, I did leave out the transnational management statements in the insertTests. The insertTests does work without the security filters enabled. As in once I commented it out from the web.xml, everything is working fine. I will look into the Lifecycle interface if I cannot find the issue with the contextDestroy method. –  milkphany Nov 27 '13 at 16:59
add comment


Your Answer



Browse other questions tagged or ask your own question.