Search code examples
hibernatejpa-2.0glassfish-3

Glassfish versioned execution of import.sql and update.sql


In JBoss we can execute import.sql when an app is deployed.

Can it be achieved in Glassfish? And also can we add several scripts for different version? For example in version 2.5, I add this specific user record.

I'm currently looking at DBMigration and LiquiBase, any other suggestions? Or which of the 2 is reliable base on experience?

Thanks,
czetsuya


Solution

  • Unfortunately, after several days of research I think the same functionality cannot be achieve in glassfish. Instead an alternative would be to create a @Startup, @Singleton annotated bean with @PostConstruct method to create database table rows on Glassfish startup.

    @Singleton
    @Startup
    public class InitData {
        @Inject
        private Logger log;
    
        @PersistenceContext
        private EntityManager em;
    
        @PostConstruct
        private void init() {
            log.debug("[dropship-ejb] init data");
    
            @SuppressWarnings("unchecked")
            List<Role> roles = em.createQuery("FROM Role r").getResultList();
    
            if (roles == null || roles.size() == 0)
                initData();
        }
    
        private void initData() {
            // insert roles
            createRole("member", "Member");
            createRole("affiliate", "Affiliate");
            createRole("backend", "Backend");
            createRole("admin", "Administrator");
        }
    
        private Role createRole(String name, String description) {
            Role r = new Role();
            r.setName(name);
            r.setDescription(description);
            em.persist(r);
    
            return r;
        }
    }