Announcement Announcement Module
Collapse
No announcement yet.
Prepopulating a database on startup Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Prepopulating a database on startup

    Hi All,

    I'm trying to create a simple database pre-populator that checks (on app startup) whether certain data is present in the database, and if it isn't will add it.

    I've used Roo to create my domain objects and controllers, but I'm having trouble using my domain objects outside of my Roo-generated controllers.

    My pre-population bean doesn't actually do any pre-population at the moment - I'm simply trying to read stuff from the database for now. The code is below -

    Code:
    @Transactional
    @Component
    public class DataInitialisationService {
    
    	@PostConstruct
    	public void init() {	
    		List<SiteMember> members = SiteMember.findAllSiteMembers();
    		System.out.println(members);
    	}
    }
    The problem is that my domain object's entityManager() method (implicitly called by invoking findAllSiteMembers) is throwing an exception because Spring hasn't yet injected an entityManager into it!

    Having read a couple of forum posts (like this one http://forum.springsource.org/showth...ulate+database), I'm still confused as the guy who posted this actually seems to be getting an entityManager configured on his domain object.

    This is likely to be a daft question, but what's the "correct" way to access my Roo-generated domain objects outside of my Roo-generated controllers?

    Any suggestions would be much appreciated!

    Cheers,
    Richard.

  • #2
    In that post you linked, it specifically says that Transactions and @PostConstruct do NOT work together. Therefore there is no Transaction and therefore no EntityManager. I did not see in that thread anywhere it say that the Loader class had an EntityManager injected.

    The ApplicationListener approach was nice, but looks like it gets called twice, so you have to add logic to make it run just once.

    I wonder what happens if you declare your loader bean with xml, and use init-method="init".

    Then add @Transactional and see if that works.

    Mark

    Comment


    • #3
      Thanks for the prompt reply!

      The ApplicationListener approach works great.

      Incidentally, I did try creating an instance of my class and using the init-method bean attribute, but the init method appears to get invoked before the entityManager has been set on my domain objects.

      Comment


      • #4
        Originally posted by rcgeorge23 View Post
        Thanks for the prompt reply!

        The ApplicationListener approach works great.

        Incidentally, I did try creating an instance of my class and using the init-method bean attribute, but the init method appears to get invoked before the entityManager has been set on my domain objects.
        And I would have said, then the depends-on would say that you require the domain object, but are they beans? Are our Domain objects now Spring Beans? A Spring Bean that holds state? Something is wrong here, I am missing something here. Or maybe what I am missing is that when instances of the Entity Beans are created, then it gets the EntityManager.

        OK, I'm back, sorry, my brain is working overtime this morning for some reason.

        But in that link, one of the posters said that they tried depends-on and that didn't work.

        But maybe they tried it with @PostConstruct and @Transactional and it wouldn't have worked either way.

        Mark

        Comment

        Working...
        X