开发者

Grails 2.0 service different behavior in Bootstrap when using a class to model M:M relationship

开发者 https://www.devze.com 2023-04-04 13:22 出处:网络
I\'m finding strange behavior when I use a service from BootStrap. Consider these domain classes AcademicUnit

I'm finding strange behavior when I use a service from BootStrap. Consider these domain classes

AcademicUnit Professor

UnitFaculty Student

I have a 1:M relationship between Student and AcademicUnit and an M:N between AcademicUnit and Professor that I'm modeling using the join class UnitFaculty. The UnitFaculty class uses the same basic approach as the UserRole class provided by Spring Security Core, so I don't think there's a problem with that approach.

I have services for querying the students and faculty from an academic unit.

ProfessorService StudentService

Conceptually they do the same thing, and they both work fine when the application is running.

I'm trying to pre-populate some data in Bootstrap. The problem occurs in ResearchCreation. The relevant snippet is:

private static void addASUEngineeringAffiliated(){
    def students = studentService.getASUEngineeringEducationStudents()
    students.each{
        def researcher = Researcher.findByName(it.name) ?:
            new Researcher(name:it.name, email:it.email).save(failOnError:true)
    }
    def faculty = professorService.getASUEngineeeringEducationFaculty()
    println "faculty is ${faculty}"
    faculty.each{
        def researcher = Researcher.findByName(it.name) ?:
            new Researcher(name:it.name, email:it.email).save(failOnError:true)
    }
}

When I start up the application, the println shows faculty is an empty list. But when that very same service method is called in a controller later, it returns the relevant faculty members, who were loaded in a step of the Bootstrap process before this one. The data should be available. It is for students, who were also loaded before.

Is this expected 开发者_开发知识库behavior, and I'm not understanding the ramifications of having an M:N with a composite key in the bootstrap process, or a bug?


I think your problem is Hibernate optimisations. Try using the argument flush:true on the save. See http://grails.org/doc/2.0.x/ref/Domain%20Classes/save.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号