Skip to main content

Deisgn pattern question, seed Data initialization ?

3 replies [Last post]
anajjar
Offline
Joined: 2009-12-22

Hello all,

I'm designing a new project which employs java persistence and several stateless beans. My project requires some initial data in the database.

My question is, what is the usual practice on seed data initialization ? are they inserted manually through executing SQL commands and export dump ?

My idea initially was to write a JUnit in which I would create an embeddable ejb container and obtain my JpaController beans and use the EntityManager to persist initial data though java code. However, after I've tried that, it turned out that when the embeddable container is destroyed , the generated entities and persisted data are gone from the database.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Bobby Bissett

On Dec 22, 2009, at 11:28 PM, glassfish@javadesktop.org wrote:

> My idea initially was to write a JUnit in which I would create an
> embeddable ejb container and obtain my JpaController beans and use
> the EntityManager to persist initial data though java code.

With v3, you can have a singleton bean that is created on startup and
it can check the DB to see if the data is there. If not, it can insert
it using the code you've already written. It's not the best way to do
it in production since it runs at every startup (and hard-codes the
data), but it's high up on the cool technology factor. Here's a
skeleton of the code that I'm using to do the same thing -- it's
almost too simple to include.

Cheers,
Bobby

------------

//[package]

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
//[other imports]

@Singleton
@Startup
public class DataCreatorBean {

@PersistenceContext(unitName="your_name_here")
private EntityManager em;

@PostConstruct
private void createData() {
Query query = em.createNamedQuery("...");
List results = query.getResultList();
if (results.isEmpty()) {
// add the data here. you're already in a transaction so
just end with em.persist(something)
}
}

}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

anajjar
Offline
Joined: 2009-12-22

Nevermind, I have found an excellent way to achieve that.

I will use DBUnit to extract data from xml file and run a junit test that will insert those data

Dominik Dorn

Do you want to insert data for production of for test use?

I successfully used hibernates import.sql to create database with initial
data for test purposes.

For production use, I always prepared the db to the level it should be in
the
beginning and then exported this state. A new installation would then import
this state.

When testing against the database, please keep in mind that it is best
practice
to make your tests transactional, meaning a transaction.rollback in the
teardown
or your tests, effectively resetting the db to a known state. This also
prevents you
from writing tests that depend on each other (e.g. test1 inserts data, test2
reads
the data test1 inserted etc... very bad practice)

I always used springs AbstractTransactionalJunit4SpringContextTests for
this.. don't
know how to do this best in javaEE6 though.

Maybe some of the experts share their knowledge? How do you do TDD with
JavaEE6?

On Thu, Dec 24, 2009 at 6:34 PM, wrote:

> Nevermind, I have found an excellent way to achieve that.
>
> I will use DBUnit to extract data from xml file and run a junit test that
> will insert those data
> [Message sent by forum member 'anajjar' (a.najjar@me.com)]
>
> http://forums.java.net/jive/thread.jspa?messageID=377399
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

--
Dominik Dorn
http://dominikdorn.com
[att1.html]